Skip to content

Commit 8a93ad9

Browse files
committed
ndk-build,cargo-apk: Default target_sdk_version to 30 or lower
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 30 which is the least [required by Google Play] today, and exactly what users will have been targeting using NDK r22 (assuming the SDK for this `platform` was installed as well) since SDK version 31 support with NDK r23 only [arrived just last week]. [197]: #197 (comment) [required by Google Play]: https://developer.android.com/distribute/best-practices/develop/target-sdk [arrived just last week]: #189
1 parent 2d554da commit 8a93ad9

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

cargo-apk/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
- Use `min_sdk_version` to select compiler target instead of `target_sdk_version` ([#197](https://github.com/rust-windowing/android-ndk-rs/pull/197)).
44
See https://developer.android.com/ndk/guides/sdk-versions#minsdkversion for more details.
5+
- Default `target_sdk_version` to `30` or lower (instead of the highest supported SDK version by the detected NDK toolchain)
6+
for more consistent interaction with Android backwards compatibility handling and its increasingly strict usage rules:
7+
https://developer.android.com/distribute/best-practices/develop/target-sdk
58

69
# 0.8.2 (2021-11-22)
710

cargo-apk/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ runtime_libs = "path/to/libs_folder"
4949

5050
# See https://developer.android.com/guide/topics/manifest/uses-sdk-element
5151
#
52-
# Defaults to a `min_sdk_version` of 23 and `target_sdk_version` is based on the ndk's default platform.
52+
# Defaults to a `min_sdk_version` of 23 and `target_sdk_version` of 30 (or lower if the detected NDK doesn't support this).
5353
[package.metadata.android.sdk]
54-
min_sdk_version = 16
55-
target_sdk_version = 29
54+
min_sdk_version = 23
55+
target_sdk_version = 30
5656
max_sdk_version = 29
5757

5858
# See https://developer.android.com/guide/topics/manifest/uses-feature-element

cargo-apk/src/apk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ impl<'a> ApkBuilder<'a> {
5959
.android_manifest
6060
.sdk
6161
.target_sdk_version
62-
.get_or_insert(ndk.default_platform());
62+
.get_or_insert_with(|| ndk.default_target_platform());
6363

6464
manifest
6565
.android_manifest
6666
.application
6767
.debuggable
68-
.get_or_insert(*cmd.profile() == Profile::Dev);
68+
.get_or_insert_with(|| *cmd.profile() == Profile::Dev);
6969

7070
Ok(Self {
7171
cmd,

ndk-build/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
- Default `target_sdk_version` to `30` or lower (instead of the highest supported SDK version by the detected NDK toolchain)
4+
for more consistent interaction with Android backwards compatibility handling and its increasingly strict usage rules:
5+
https://developer.android.com/distribute/best-practices/develop/target-sdk
6+
37
# 0.4.3 (2021-11-22)
48

59
- Provide NDK `build_tag` version from `source.properties` in the NDK root.
@@ -23,7 +27,7 @@
2327

2428
# 0.2.0 (2021-04-20)
2529

26-
- **Breaking:** refactored `Manifest` into a proper (de)serialization struct. `Manifest` now closely matches [`an android manifest file`](https://developer.android.com/guide/topics/manifest/manifest-element).
30+
- **Breaking:** refactored `Manifest` into a proper (de)serialization struct. `Manifest` now closely matches [an android manifest file](https://developer.android.com/guide/topics/manifest/manifest-element).
2731
- **Breaking:** removed `Config` in favor of using the new `Manifest` struct directly. Instead of using `Config::from_config` to create a `Manifest`, now you instantiate `Manifest` directly using, almost all, the same values.
2832

2933
# 0.1.4 (2020-11-25)

ndk-build/src/apk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl ApkConfig {
4040
.manifest
4141
.sdk
4242
.target_sdk_version
43-
.unwrap_or_else(|| self.ndk.default_platform());
43+
.unwrap_or_else(|| self.ndk.default_target_platform());
4444
let mut aapt = self.build_tool(bin!("aapt"))?;
4545
aapt.arg("package")
4646
.arg("-f")

ndk-build/src/ndk.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,18 @@ impl Ndk {
159159
Ok(Command::new(dunce::canonicalize(path)?))
160160
}
161161

162-
pub fn default_platform(&self) -> u32 {
162+
pub fn highest_supported_platform(&self) -> u32 {
163163
self.platforms().iter().max().cloned().unwrap()
164164
}
165165

166+
/// Returns platform `30` as currently [required by Google Play], or lower
167+
/// when the detected SDK does not support it yet.
168+
///
169+
/// [required by Google Play]: https://developer.android.com/distribute/best-practices/develop/target-sdk
170+
pub fn default_target_platform(&self) -> u32 {
171+
self.highest_supported_platform().min(30)
172+
}
173+
166174
pub fn platform_dir(&self, platform: u32) -> Result<PathBuf, NdkError> {
167175
let dir = self
168176
.sdk_path

0 commit comments

Comments
 (0)