From 7443087a302111116b87fc824fdabf0328b0ce3c Mon Sep 17 00:00:00 2001 From: SomberNight Date: Wed, 12 Oct 2022 14:19:18 +0000 Subject: [PATCH 1/2] build: add --android-target-sdk-version arg (defaults to android_api) Currently p4a has a variable named `android_api` (set by e.g. buildozer via the `app.android.api` config key). In `AndroidManifest.xml`, both `compileSdkVersion` and `targetSdkVersion` are set to `android_api`. This PR allows setting the targetSdkVersion and compileSdkVersion independently, by adding a `--android-target-sdk-version` build arg. `compileSdkVersion` is a compilation setting used by e.g. the NDK at build-time, while `targetSdkVersion` is just a field to set in the manifest, only used at runtime (mainly in the Java world). (see e.g. https://stackoverflow.com/a/26694276 ) --- pythonforandroid/bootstraps/common/build/build.py | 9 +++++++++ .../bootstraps/common/build/templates/build.tmpl.gradle | 2 +- .../sdl2/build/templates/AndroidManifest.tmpl.xml | 2 +- .../build/templates/AndroidManifest.tmpl.xml | 2 +- .../build/templates/AndroidManifest.tmpl.xml | 2 +- .../webview/build/templates/AndroidManifest.tmpl.xml | 2 +- 6 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pythonforandroid/bootstraps/common/build/build.py b/pythonforandroid/bootstraps/common/build/build.py index 6885a333df..74899069f3 100644 --- a/pythonforandroid/bootstraps/common/build/build.py +++ b/pythonforandroid/bootstraps/common/build/build.py @@ -497,6 +497,12 @@ def make_package(args): sdk_dir = fileh.read().strip() sdk_dir = sdk_dir[8:] + if args.android_target_sdk_version == -1: + args.android_target_sdk_version = android_api + if args.android_target_sdk_version != android_api: + print(f'WARNING: android_target_sdk_version ({args.android_target_sdk_version}) ' + f'was set explicitly, and it does not match android_api ({android_api}).') + # Try to build with the newest available build tools ignored = {".DS_Store", ".ds_store"} build_tools_versions = [x for x in listdir(join(sdk_dir, 'build-tools')) if x not in ignored] @@ -805,6 +811,9 @@ def parse_args_and_make_package(args=None): action='store_true', help=('Allow the --minsdk argument to be different from ' 'the discovered ndk_api in the dist')) + ap.add_argument('--android-target-sdk-version', dest='android_target_sdk_version', + default=-1, type=int, + help='targetSdkVersion to put in manifest. Matches android-api by default.') ap.add_argument('--intent-filters', dest='intent_filters', help=('Add intent-filters xml rules to the ' 'AndroidManifest.xml file. The argument is a ' diff --git a/pythonforandroid/bootstraps/common/build/templates/build.tmpl.gradle b/pythonforandroid/bootstraps/common/build/templates/build.tmpl.gradle index bb000393a4..2f944dfd93 100644 --- a/pythonforandroid/bootstraps/common/build/templates/build.tmpl.gradle +++ b/pythonforandroid/bootstraps/common/build/templates/build.tmpl.gradle @@ -33,7 +33,7 @@ android { buildToolsVersion '{{ build_tools_version }}' defaultConfig { minSdkVersion {{ args.min_sdk_version }} - targetSdkVersion {{ android_api }} + targetSdkVersion {{ args.android_target_sdk_version }} versionCode {{ args.numeric_version }} versionName '{{ args.version }}' manifestPlaceholders = {{ args.manifest_placeholders}} diff --git a/pythonforandroid/bootstraps/sdl2/build/templates/AndroidManifest.tmpl.xml b/pythonforandroid/bootstraps/sdl2/build/templates/AndroidManifest.tmpl.xml index 27b2f19433..e1c3899e59 100644 --- a/pythonforandroid/bootstraps/sdl2/build/templates/AndroidManifest.tmpl.xml +++ b/pythonforandroid/bootstraps/sdl2/build/templates/AndroidManifest.tmpl.xml @@ -19,7 +19,7 @@ /> - + diff --git a/pythonforandroid/bootstraps/service_library/build/templates/AndroidManifest.tmpl.xml b/pythonforandroid/bootstraps/service_library/build/templates/AndroidManifest.tmpl.xml index f667651780..91eb1a5dca 100644 --- a/pythonforandroid/bootstraps/service_library/build/templates/AndroidManifest.tmpl.xml +++ b/pythonforandroid/bootstraps/service_library/build/templates/AndroidManifest.tmpl.xml @@ -5,7 +5,7 @@ android:versionName="{{ args.version }}"> - + {% for name in service_names %} diff --git a/pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml b/pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml index d19ed32931..701dd887b5 100644 --- a/pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml +++ b/pythonforandroid/bootstraps/service_only/build/templates/AndroidManifest.tmpl.xml @@ -16,7 +16,7 @@ /> - + {% for perm in args.permissions %} diff --git a/pythonforandroid/bootstraps/webview/build/templates/AndroidManifest.tmpl.xml b/pythonforandroid/bootstraps/webview/build/templates/AndroidManifest.tmpl.xml index f77533b1e6..a7e495b1fd 100644 --- a/pythonforandroid/bootstraps/webview/build/templates/AndroidManifest.tmpl.xml +++ b/pythonforandroid/bootstraps/webview/build/templates/AndroidManifest.tmpl.xml @@ -16,7 +16,7 @@ /> - + From aff85a6d2bb3f272b91628c72d9ab194eea12a1b Mon Sep 17 00:00:00 2001 From: SomberNight Date: Fri, 14 Oct 2022 18:25:57 +0000 Subject: [PATCH 2/2] fix tests --- tests/test_build.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_build.py b/tests/test_build.py index 6d30f996e7..2bb119c23a 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -62,6 +62,8 @@ class TestTemplates(unittest.TestCase): def test_android_manifest_xml(self): args = mock.Mock() args.min_sdk_version = 12 + android_api = 1234 + args.android_target_sdk_version = android_api args.build_mode = 'debug' args.native_services = ['abcd', ] args.permissions = [] @@ -74,7 +76,7 @@ def test_android_manifest_xml(self): "args": args, "service": False, "service_names": [], - "android_api": 1234, + "android_api": android_api, "debug": "debug" in args.build_mode, "native_services": args.native_services }