Skip to content

Commit

Permalink
cargo-apk: Support new Artifact format for [lib], [[bin]] and `…
Browse files Browse the repository at this point in the history
…[[example]]` renames

rust-mobile/cargo-subcommand#17
  • Loading branch information
MarijnS95 committed Jun 27, 2023
1 parent 4786350 commit 2a3439b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ members = [
"examples",
"cargo-apk",
]

[patch.crates-io]
# Unreleased, including https://github.com/rust-mobile/cargo-subcommand/pull/17
cargo-subcommand = { git = "https://github.com/rust-mobile/cargo-subcommand.git", rev = "281b281" }
4 changes: 3 additions & 1 deletion cargo-apk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ Tool for creating Android packages.
## Installation

From crates.io:

```console
$ cargo install cargo-apk
```

From source:

```console
$ cargo install --path .
```
Expand Down Expand Up @@ -80,7 +82,7 @@ runtime_libs = "path/to/libs_folder"
# and keystore password respectively. The profile portion follows the same rules
# as `<cfg>`, it is the uppercased profile name with `-` replaced with `_`.
#
# If present they take precedence over the signing information in the manifest.
# If present they take precedence over the signing information in the manifest.
[package.metadata.android.signing.<profile>]
path = "relative/or/absolute/path/to/my.keystore"
keystore_password = "android"
Expand Down
20 changes: 11 additions & 9 deletions cargo-apk/src/apk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::Error;
use crate::manifest::{Inheritable, Manifest, Root};
use cargo_subcommand::{Artifact, CrateType, Profile, Subcommand};
use cargo_subcommand::{Artifact, ArtifactType, CrateType, Profile, Subcommand};
use ndk_build::apk::{Apk, ApkConfig};
use ndk_build::cargo::{cargo_ndk, VersionCode};
use ndk_build::dylibs::get_libs_search_paths;
Expand Down Expand Up @@ -162,19 +162,21 @@ impl<'a> ApkBuilder<'a> {
let mut manifest = self.manifest.android_manifest.clone();

if manifest.package.is_empty() {
manifest.package = match artifact {
Artifact::Root(name) => format!("rust.{}", name.replace('-', "_")),
Artifact::Example(name) => format!("rust.example.{}", name.replace('-', "_")),
let name = artifact.name.replace('-', "_");
manifest.package = match artifact.r#type {
ArtifactType::Lib => format!("rust.{}", name),
ArtifactType::Bin => format!("rust.{}", name),
ArtifactType::Example => format!("rust.example.{}", name),
};
}

if manifest.application.label.is_empty() {
manifest.application.label = artifact.name().to_string();
manifest.application.label = artifact.name.to_string();
}

manifest.application.activity.meta_data.push(MetaData {
name: "android.app.lib_name".to_string(),
value: artifact.name().replace('-', "_"),
value: artifact.name.replace('-', "_"),
});

let crate_path = self.cmd.manifest().parent().expect("invalid manifest path");
Expand All @@ -200,11 +202,11 @@ impl<'a> ApkBuilder<'a> {
.manifest
.apk_name
.clone()
.unwrap_or_else(|| artifact.name().to_string());
.unwrap_or_else(|| artifact.name.to_string());

let config = ApkConfig {
ndk: self.ndk.clone(),
build_dir: self.build_dir.join(artifact),
build_dir: self.build_dir.join(artifact.build_dir()),
apk_name,
assets,
resources,
Expand Down Expand Up @@ -331,7 +333,7 @@ impl<'a> ApkBuilder<'a> {
let apk = self.build(artifact)?;
apk.install(self.device_serial.as_deref())?;

let target_dir = self.build_dir.join(artifact);
let target_dir = self.build_dir.join(artifact.build_dir());
self.ndk.ndk_gdb(
target_dir,
"android.app.NativeActivity",
Expand Down
17 changes: 13 additions & 4 deletions cargo-apk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ fn split_apk_and_cargo_args(input: Vec<String>) -> (Args, Vec<String>) {
(args, split_args.cargo_args)
}

fn iterator_single_item<T>(mut iter: impl Iterator<Item = T>) -> Option<T> {
let first_item = iter.next()?;
if iter.next().is_some() {
None
} else {
Some(first_item)
}
}

fn main() -> anyhow::Result<()> {
env_logger::init();
let Cmd {
Expand Down Expand Up @@ -160,14 +169,14 @@ fn main() -> anyhow::Result<()> {
ApkSubCmd::Run { args, no_logcat } => {
let cmd = Subcommand::new(args.subcommand_args)?;
let builder = ApkBuilder::from_subcommand(&cmd, args.device)?;
anyhow::ensure!(cmd.artifacts().len() == 1, Error::invalid_args());
builder.run(&cmd.artifacts()[0], no_logcat)?;
let artifact = iterator_single_item(cmd.artifacts()).ok_or(Error::invalid_args())?;
builder.run(artifact, no_logcat)?;
}
ApkSubCmd::Gdb { args } => {
let cmd = Subcommand::new(args.subcommand_args)?;
let builder = ApkBuilder::from_subcommand(&cmd, args.device)?;
anyhow::ensure!(cmd.artifacts().len() == 1, Error::invalid_args());
builder.gdb(&cmd.artifacts()[0])?;
let artifact = iterator_single_item(cmd.artifacts()).ok_or(Error::invalid_args())?;
builder.gdb(artifact)?;
}
ApkSubCmd::Version => {
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
Expand Down

0 comments on commit 2a3439b

Please sign in to comment.