Skip to content

Commit

Permalink
ndk-build,cargo-apk: Default target_sdk_version to 29 or lower
Browse files Browse the repository at this point in the history
As discussed in [197] setting `target_sdk_version` to the "arbitrary"
highest available SDK version is nonsense.  This target version (unlike
`min_sdk_version` which defines the least set of symbols that should be
available) has real impact on the runtime of an application, in
particular the compatibility or stringency of rules Android applies to
your application.  Certain APIs may not work at all or be heavily
restricted on newer target versions because they are deemed too
dangerous, and Android expects the user has tested their app against
these limitations and is communicating this by setting
`target_sdk_version` to that particular value.  Hence this shouldn't
change purely based on the environment, even for the default.

To retain some backwards compatibility with previous `cargo-apk` we set
this to level 29 which is the least [required by Google Play] today, and
approximately what users with recent SDKs will be targeting already.
However, care has to be taken around ie. scoped storage, hence this is
considered a **Breaking** change.

[197]: #197 (comment)
[required by Google Play]: https://developer.android.com/distribute/best-practices/develop/target-sdk
  • Loading branch information
MarijnS95 committed Nov 22, 2021
1 parent 55539dc commit 523ffb1
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cargo-apk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

- **Breaking**: Default `target_sdk_version` to `29` or lower for more consistent
interaction with Android backwards compatibility and tighter rules:
https://developer.android.com/distribute/best-practices/develop/target-sdk

# 0.8.2 (2021-11-22)

- Fixed the library name in case of multiple build artifacts in the Android manifest.
Expand Down
2 changes: 1 addition & 1 deletion cargo-apk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ runtime_libs = "path/to/libs_folder"

# See https://developer.android.com/guide/topics/manifest/uses-sdk-element
#
# Defaults to a `min_sdk_version` of 23 and `target_sdk_version` is based on the ndk's default platform.
# Defaults to a `min_sdk_version` of 23 and `target_sdk_version` of 29 (or lower if the detected NDK doesn't support this).
[package.metadata.android.sdk]
min_sdk_version = 16
target_sdk_version = 29
Expand Down
6 changes: 3 additions & 3 deletions cargo-apk/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ impl<'a> ApkBuilder<'a> {
.android_manifest
.sdk
.target_sdk_version
.get_or_insert(ndk.default_platform());
.get_or_insert_with(|| ndk.default_target_platform());

manifest
.android_manifest
.application
.debuggable
.get_or_insert(*cmd.profile() == Profile::Dev);
.get_or_insert_with(|| *cmd.profile() == Profile::Dev);

Ok(Self {
cmd,
Expand Down Expand Up @@ -238,7 +238,7 @@ impl<'a> ApkBuilder<'a> {
.android_manifest
.sdk
.target_sdk_version
.unwrap_or_else(|| ndk.default_platform());
.unwrap_or_else(|| ndk.default_target_platform());
for target in &self.build_targets {
let mut cargo = cargo_ndk(&ndk, *target, target_sdk_version)?;
cargo.args(self.cmd.args());
Expand Down
4 changes: 4 additions & 0 deletions ndk-build/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

- **Breaking**: Default `target_sdk_version` to `29` or lower for more consistent
interaction with Android backwards compatibility and tighter rules:
https://developer.android.com/distribute/best-practices/develop/target-sdk

# 0.4.3 (2021-11-22)

- Provide NDK `build_tag` version from `source.properties` in the NDK root.
Expand Down
2 changes: 1 addition & 1 deletion ndk-build/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl ApkConfig {
.manifest
.sdk
.target_sdk_version
.unwrap_or_else(|| self.ndk.default_platform());
.unwrap_or_else(|| self.ndk.default_target_platform());
let mut aapt = self.build_tool(bin!("aapt"))?;
aapt.arg("package")
.arg("-f")
Expand Down
10 changes: 9 additions & 1 deletion ndk-build/src/ndk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,18 @@ impl Ndk {
Ok(Command::new(dunce::canonicalize(path)?))
}

pub fn default_platform(&self) -> u32 {
pub fn highest_supported_platform(&self) -> u32 {
self.platforms().iter().max().cloned().unwrap()
}

/// Returns platform `29` as currently [required by Google Play], or lower
/// when the detected SDK does not support it yet.
///
/// [required by Google Play]: https://developer.android.com/distribute/best-practices/develop/target-sdk
pub fn default_target_platform(&self) -> u32 {
self.highest_supported_platform().min(29)
}

pub fn platform_dir(&self, platform: u32) -> Result<PathBuf, NdkError> {
let dir = self
.sdk_path
Expand Down

0 comments on commit 523ffb1

Please sign in to comment.