Skip to content

Commit

Permalink
cargo-apk: Use min_sdk_version to select compiler target
Browse files Browse the repository at this point in the history
According to [1] minSdkVersion is used to determine the compiler target
and ultimately limit what API is available at compile-time.  This is
for the most part because using/linking newer API will result in runtime
linker errors on these older platforms.

In other words, using the target sdk version which (as of writing, but
that will change in the near future too) defaults to the highest version
supported by the detected SDK makes it impossible to detect
compatibility issues (symbols that are unavailable) with your app on
older Android APIs despite setting min_sdk_version.

[1]: https://developer.android.com/ndk/guides/sdk-versions#minsdkversion
  • Loading branch information
MarijnS95 committed Nov 22, 2021
1 parent 7c4da9e commit c58c6b1
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions cargo-apk/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,7 @@ impl<'a> ApkBuilder<'a> {
pub fn check(&self) -> Result<(), Error> {
for target in &self.build_targets {
let triple = target.rust_triple();
let target_sdk_version = self
.manifest
.android_manifest
.sdk
.target_sdk_version
.unwrap();
let mut cargo = cargo_ndk(&self.ndk, *target, target_sdk_version)?;
let mut cargo = cargo_ndk(&self.ndk, *target, self.min_sdk_version())?;
cargo.arg("check");
if self.cmd.target().is_none() {
cargo.arg("--target").arg(triple);
Expand Down Expand Up @@ -158,9 +152,7 @@ impl<'a> ApkBuilder<'a> {
.join(artifact)
.join(artifact.file_name(CrateType::Cdylib, triple));

let target_sdk_version = config.manifest.sdk.target_sdk_version.unwrap();

let mut cargo = cargo_ndk(&config.ndk, *target, target_sdk_version)?;
let mut cargo = cargo_ndk(&config.ndk, *target, self.min_sdk_version())?;
cargo.arg("rustc");
if self.cmd.target().is_none() {
cargo.arg("--target").arg(triple);
Expand Down Expand Up @@ -233,19 +225,27 @@ impl<'a> ApkBuilder<'a> {

pub fn default(&self) -> Result<(), Error> {
let ndk = Ndk::from_env()?;
let target_sdk_version = self
.manifest
.android_manifest
.sdk
.target_sdk_version
.unwrap_or_else(|| ndk.default_platform());
for target in &self.build_targets {
let mut cargo = cargo_ndk(&ndk, *target, target_sdk_version)?;
let mut cargo = cargo_ndk(&ndk, *target, self.min_sdk_version())?;
cargo.args(self.cmd.args());
if !cargo.status()?.success() {
return Err(NdkError::CmdFailed(cargo).into());
}
}
Ok(())
}

/// Returns `minSdkVersion` for use in compiler target selection:
/// <https://developer.android.com/ndk/guides/sdk-versions#minsdkversion>
///
/// Has a lower bound of `23` to retain backwards compatibility with
/// the previous default.
fn min_sdk_version(&self) -> u32 {
self.manifest
.android_manifest
.sdk
.min_sdk_version
.unwrap_or(23)
.max(23)
}
}

0 comments on commit c58c6b1

Please sign in to comment.