Skip to content

Commit

Permalink
cargo-apk: Use min_sdk_version to select compiler target (#197)
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 authored Nov 24, 2021
1 parent 317d711 commit 39d4701
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
3 changes: 3 additions & 0 deletions cargo-apk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Unreleased

- Use `min_sdk_version` to select compiler target instead of `target_sdk_version` ([#197](https://github.com/rust-windowing/android-ndk-rs/pull/197)).
See https://developer.android.com/ndk/guides/sdk-versions#minsdkversion for more details.

# 0.8.2 (2021-11-22)

- Fixed the library name in case of multiple build artifacts in the Android manifest.
Expand Down
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 39d4701

Please sign in to comment.