Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/android-build-variants.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cargo-mobile2": patch
---

Added `Device::run_with_application_id_suffix` to support running different build variants. This relates to the `applicationIdSuffix` as defined in Android development,
described [in these docs](https://developer.android.com/build/build-variants#build-types).
9 changes: 8 additions & 1 deletion src/android/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ pub enum Command {
help = "Specifies which activtiy to launch"
)]
activity: Option<String>,
#[structopt(
long = "application-id-suffix",
help = "Optional suffix for the application ID (e.g. \".debug\")"
)]
application_id_suffix: Option<String>,
},
#[structopt(name = "st", about = "Displays a detailed stacktrace for a device")]
Stacktrace,
Expand Down Expand Up @@ -301,12 +306,13 @@ impl Exec for Input {
filter: cli::Filter { filter },
reinstall_deps: cli::ReinstallDeps { reinstall_deps },
activity,
application_id_suffix,
} => with_config(non_interactive, wrapper, |config, metadata, env| {
let build_app_bundle = metadata.asset_packs().is_some();
ensure_init(config)?;
device_prompt(env)
.map_err(Error::DevicePromptFailed)?
.run(
.run_with_application_id_suffix(
config,
env,
noise_level,
Expand All @@ -320,6 +326,7 @@ impl Exec for Input {
.unwrap_or(DEFAULT_ACTIVITY)
.to_string()
}),
application_id_suffix,
)
.and_then(|h| {
h.wait().map(|_| ()).map_err(|err| RunError::CommandFailed {
Expand Down
107 changes: 106 additions & 1 deletion src/android/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@ impl<'a> Device<'a> {
Ok(())
}

/**
* This is the legacy function that doesn't support applicationIdSuffix, which
* is required for running different build variants (such as debug and release).
*
* It is kept for backwards compatibility.
*/
#[allow(clippy::too_many_arguments)]
pub fn run(
&self,
Expand All @@ -381,6 +387,32 @@ impl<'a> Device<'a> {
build_app_bundle: bool,
reinstall_deps: bool,
activity: String,
) -> Result<duct::Handle, RunError> {
return self.run_with_application_id_suffix(
config,
env,
noise_level,
profile,
filter_level,
build_app_bundle,
reinstall_deps,
activity,
None,
);
}

#[allow(clippy::too_many_arguments)]
pub fn run_with_application_id_suffix(
&self,
config: &Config,
env: &Env,
noise_level: NoiseLevel,
profile: Profile,
filter_level: Option<FilterLevel>,
build_app_bundle: bool,
reinstall_deps: bool,
activity: String,
application_id_suffix: Option<String>,
) -> Result<duct::Handle, RunError> {
if build_app_bundle {
bundletool::install(reinstall_deps).map_err(RunError::BundletoolInstallFailed)?;
Expand All @@ -402,7 +434,12 @@ impl<'a> Device<'a> {
self.install_apk(config, env, profile)
.map_err(RunError::ApkInstallFailed)?;
}
let activity = format!("{}/{}", config.app().identifier(), activity);

let activity = Device::resolve_activity_name(
config.app().identifier().to_string(),
application_id_suffix,
activity,
);
let activity_ = activity.clone();
let cmd = self
.adb(env)
Expand Down Expand Up @@ -518,4 +555,72 @@ impl<'a> Device<'a> {
}
Ok(())
}

/**
* The activity name is the fully qualified class name of the activity to launch.
* Here are the expected formats for the activity name: of the release and debug variants
*
* Even though the actual logic of this method is very simple, it is kept
* separate for clarity and for unit testing.
*
* Release variants have 2 acceptable formats:
* * `com.example.app/.MainActivity`
* * `com.example.app/com.example.app.MainActivity`
*
* Debug variants have 1 acceptable format:
* * `com.example.app.debug/com.example.app.MainActivity`
*/
fn resolve_activity_name(
Comment thread
FabianLars marked this conversation as resolved.
app_identifier: String,
application_id_suffix: Option<String>,
activity: String,
) -> String {
return format!(
"{app_identifier}{}/{activity}",
application_id_suffix.unwrap_or_default()
);
}
}

#[cfg(test)]
mod test {
use super::*;
use rstest::rstest;
#[rstest(
app_identifier,
application_id_suffix,
activity,
expected,
case(
"com.example.app",
Some(".debug"),
"com.example.app.MainActivity",
"com.example.app.debug/com.example.app.MainActivity"
),
case(
"com.example.app",
None,
".MainActivity",
"com.example.app/.MainActivity"
),
case(
"com.example.app",
None,
"com.example.app.MainActivity",
"com.example.app/com.example.app.MainActivity"
)
)]
fn test_resolve_activity_name(
app_identifier: String,
application_id_suffix: Option<&str>,
activity: String,
expected: String,
) {
let activity = Device::resolve_activity_name(
app_identifier,
application_id_suffix.map(|s| s.to_string()),
activity,
);
assert_eq!(activity, expected);
}
Comment thread
FabianLars marked this conversation as resolved.
}
Loading