Debian Codegraph @

docs/en/reference/platforms/android/gradle.md

raw
1 # Gradle project
2
3 <table class="host-platform-support-table">
4 <colgroup>
5 <col style="width: 11%" />
6 <col style="width: 10%" />
7 <col style="width: 7%" />
8 <col style="width: 5%" />
9 <col style="width: 6%" />
10 <col style="width: 5%" />
11 <col style="width: 5%" />
12 <col style="width: 7%" />
13 <col style="width: 11%" />
14 <col style="width: 7%" />
15 <col style="width: 10%" />
16 </colgroup>
17 <thead>
18 <tr>
19 <th colspan="11"><a href="../../../../reference/platforms">Host Platform Support</a></th>
20 </tr>
21 <tr>
22 <th colspan="2">macOS</th>
23 <th colspan="5">Windows</th>
24 <th colspan="4">Linux</th>
25 </tr>
26 <tr>
27 <th>x86‑64</th>
28 <th>arm64</th>
29 <th>x86</th>
30 <th colspan="2">x86‑64</th>
31 <th colspan="2">arm64</th>
32 <th>x86</th>
33 <th>x86‑64</th>
34 <th>arm</th>
35 <th>arm64</th>
36 </tr>
37 </thead>
38 <tbody>
39 <tr>
40 <td>{{ ci_tested }}</td>
41 <td>{{ maintainer_tested }}</td>
42 <td></td>
43 <td colspan="2">{{ ci_tested }}</td>
44 <td colspan="2"></td>
45 <td>{{ not_tested }}</td>
46 <td>{{ ci_tested }}</td>
47 <td>{{ not_tested }}</td>
48 <td>{{ not_tested }}</td>
49 </tr>
50 </tbody>
51 </table>
52
53 When generating an Android project, Briefcase produces a Gradle project.
54
55 ## Environment
56
57 Gradle requires an install of a Java 17 JDK and the Android SDK.
58
59 If the methods below fail to find an Android SDK or Java JDK, Briefcase will download and install an isolated copy in its data directory.
60
61 ### Java JDK
62
63 If you have an existing install of a Java 17 JDK, it will be used by Briefcase if the `JAVA_HOME` environment variable is set. On macOS, if `JAVA_HOME` is not set, Briefcase will use the `/usr/libexec/java_home` tool to find an existing JDK install.
64
65 ### Android SDK
66
67 If you have an existing install of the Android SDK, it will be used by Briefcase if the `ANDROID_HOME` environment variable is set. If `ANDROID_HOME` is not present in the environment, Briefcase will honor the deprecated `ANDROID_SDK_ROOT` environment variable. Additionally, an existing SDK install must have version 19.0 of Command-line Tools installed; this version can be installed in the SDK Manager in Android Studio.
68
69 ## Packaging format
70
71 Briefcase supports three packaging formats for an Android app:
72
73 1. An AAB bundle (the default output of `briefcase package android`, or by using `briefcase package android -p aab`); or
74 2. A Release APK (by using `briefcase package android -p apk`); or
75 3. A Debug APK (by using `briefcase package android -p debug-apk`).
76
77 ## Icon format
78
79 Android projects use `.png` format icons, in round, square and adaptive variants. An application must provide the icons in the following sizes, for 3 variants:
80
81 * `round`:
82 * 48px (`mdpi`; baseline resolution)
83 * 72px (`hdpi`; 1.5x scale)
84 * 96px (`xhdpi`; 2x scale)
85 * 144px (`xxhdpi`; 3x scale)
86 * 192px (`xxxhdpi`; 4x scale)
87 * `square`:
88 * 48px (`mdpi`; baseline resolution)
89 * 72px (`hdpi`; 1.5x scale)
90 * 96px (`xhdpi`; 2x scale)
91 * 144px (`xxhdpi`; 3x scale)
92 * 192px (`xxxhdpi`; 4x scale)
93 * 320px (`mdpi`; baseline resolution for splash screen)
94 * 480px (`hdpi`; 1.5x scale for splash screen)
95 * 640px (`xhdpi`; 2x scale for splash screen)
96 * 960px (`xxhdpi`; 3x scale for splash screen)
97 * 1280px (`xxxhdpi`; 4x scale for splash screen)
98 * `adaptive`:
99 * 108px (`mdpi`; baseline resolution; 66px drawable area)
100 * 162px (`hdpi`; 1.5x scale; 99px drawable area)
101 * 216px (`xhdpi`; 2x scale; 132px drawable area)
102 * 324px (`xxhdpi`; 3x scale; 198px drawable area)
103 * 432px (`xxxhdpi`; 4x scale; 264px drawable area)
104
105 The `round` and `square` icons should include their background color in the image. The `adaptive` icons should have a transparent background; the icon image should be centered in the overall image, and should not exceed the drawable area. The background color of the adaptive icon will be the value specified with [`splash_background_color`][].
106
107 The icon will also be used to populate the splash screen. You can specify a background color for the splash screen using the [`splash_background_color`][] configuration setting.
108
109 Android projects do not support installer images.
110
111 ## Colors
112
113 Android allows for some customization of the colors used by your app:
114
115 * [`base_theme`][] is used to set the base Android theme.
116 * [`accent_color`][] is used as a subtle highlight throughout your app to call attention to key elements. It's used on things like form labels and inputs.
117 * [`primary_color`][] is the main branding color of the app and is used to color the app bar in the main window.
118 * [`primary_color_dark`][] is used alongside the primary color to color the status bar at the top of the screen.
119 * [`splash_background_color`][] is the color of the splash background that displays while an app is loading.
120
121 ## Additional options
122
123 The following options can be provided at the command line when producing Android projects:
124
125 ### run
126
127 #### `-d <device>` / `--device <device>`
128
129 The device or emulator to target. Can be specified as:
130
131 * `@` followed by an AVD name (e.g., `@beePhone`); or
132
133 * a device ID (a hexadecimal identifier associated with a specific hardware device); or
134
135 * a JSON dictionary specifying the properties of a device that will be created. This dictionary must have, at a minimum, an AVD name:
136
137 ```console
138 $ briefcase run -d '{"avd":"new-device"}'
139 ```
140
141 You may also specify:
142
143
144 * `device_type` (e.g., `pixel`) * the type of device to emulate
145 * `skin` (e.g., `pixel_3a`) * the skin to apply to the emulator
146 * `system_image` (e.g.,
147
148 `system-images;android-31;default;arm64-v8a`) * the Android system image to use in the emulator.
149
150 If any of these attributes are *not* specified, they will fall back to reasonable defaults.
151
152
153 #### `--Xemulator=<value>`
154
155 A configuration argument to be passed to the emulator on startup. For example, to start the emulator in "headless" mode (i.e., without a display window), specify `--Xemulator=-no-window`. See [the Android documentation](https://developer.android.com/studio/run/emulator-commandline) for details on the full list of options that can be provided.
156
157 You may specify multiple `--Xemulator` arguments; each one specifies a single argument to pass to the emulator, in the order they are specified.
158
159 #### `--shutdown-on-exit`
160
161 Instruct Briefcase to shut down the emulator when the run finishes. This is especially useful if you are running in headless mode, as the emulator will continue to run in the background, but there will be no visual manifestation that it is running. It may also be useful as a cleanup mechanism when running in a CI configuration.
162
163 #### `--revoke-permission=<permission>`
164
165 Revoke a runtime permission of the running app before starting it. This is useful in tests to get a reproducible starting state when the permission handling should also be tested.
166
167 For example to revoke the Bluetooth scan permission, specify `--revoke-permission=android.permission.BLUETOOTH_SCAN`. A full list of available permissions is defined in the [Android documentation](https://developer.android.com/reference/android/Manifest.permission). Only the ones that are marked as `Protection level: dangerous` are relevant.
168
169 You may specify multiple `--revoke-permission` arguments; each one specifies one permission.
170
171 #### `--forward-port=<port>`
172
173 Forward a port via ADB from the host to the Android device. This is useful when a network service is running on the Android app that you want to connect to from the host.
174
175 You may specify multiple `--forward-port` arguments; each one specifies a single port.
176
177 #### `--reverse-port=<port>`
178
179 Reverse a port via ADB from the Android device to the host. This is useful when a network service is running on the host that you want to connect to from the Android app.
180
181 You may specify multiple `--reverse-port` arguments; each one specifies a single port.
182
183 ## Application configuration
184
185 The following options can be added to the `tool.briefcase.app.<appname>.android` section of your `pyproject.toml` file.
186
187 ### `android_manifest_attrs_extra_content`
188
189 Additional attributes that will be added verbatim to the `<manifest>` declaration of the `AndroidManifest.xml` of your app.
190
191 ### `android_manifest_extra_content`
192
193 Additional content that will be added verbatim just before the closing `</manifest>` declaration of the `AndroidManifest.xml` of your app.
194
195 ### `android_manifest_application_attrs_extra_content`
196
197 Additional attributes that will be added verbatim to the `<application>` declaration of the `AndroidManifest.xml` of your app.
198
199 ### `android_manifest_application_extra_content`
200
201 Additional content that will be added verbatim just before the closing `</application>` declaration of the `AndroidManifest.xml` of your app.
202
203 ### `android_manifest_activity_attrs_extra_content`
204
205 Additional attributes that will be added verbatim to the `<activity>` declaration of the `AndroidManifest.xml` of your app.
206
207 ### `android_manifest_activity_extra_content`
208
209 Additional content that will be added verbatim just before the closing `</activity>` declaration of the `AndroidManifest.xml` of your app.
210
211 ### `base_theme`
212
213 The base theme for the application. Defaults to `Theme.AppCompat.Light.DarkActionBar`
214
215 <a name="android.build_gradle_dependencies"><!-- needed for compatibility with 0.3.25 docs build -->
216
217 ### `build_gradle_dependencies`
218
219 The list of libraries that should be linked into the Android application. Each library should be a versioned Maven package specifier. If unspecified, three libraries will be linked into the app:
220
221 * `androidx.appcompat:appcompat:1.0.2`
222 * `androidx.constraintlayout:constraintlayout:1.1.3`
223 * `androidx.swiperefreshlayout:swiperefreshlayout:1.1.0`
224
225 ### `build_gradle_extra_content`
226
227 A string providing additional Gradle settings to use when building your app. This will be added verbatim to the end of your `app/build.gradle` file.
228
229 ### `feature`
230
231 A property whose sub-properties define the features that will be marked as required by the final app. Each entry will be converted into a `<uses-feature>` declaration in your app's `AndroidManifest.xml`, with the feature name matching the name of the sub-attribute.
232
233 For example, specifying:
234
235 ```python
236 feature."android.hardware.bluetooth" = true
237 ```
238
239 will result in an `AndroidManifest.xml` declaration of:
240
241 ```python
242 <uses-feature android:name="android.hardware.bluetooth" android:required="true">
243 ```
244
245 The use of some cross-platform permissions will imply the addition of features; see [the discussion on Android permissions][android-permissions] for more details.
246
247 ### `min_os_version`
248
249 The minimum API level that the app will support (i.e., the `minSdkVersion` for the app). This is *not* the Android version; it is the underlying API level. For example, Android 9 uses an API level of 28; if you wanted to specify Android 9 as your minimum supported version, you would define `min_os_version = "28"`.
250
251 ### `permission`
252
253 A property whose sub-properties define the platform-specific permissions that will be marked as required by the final app. Each entry will be converted into a `<uses-permission>` declaration in your app's `AndroidManifest.xml`, with the feature name matching the name of the sub-attribute.
254
255 For example, specifying:
256
257 ```toml
258 permission."android.permission.HIGH_SAMPLING_RATE_SENSORS" = {}
259 ```
260
261 will result in an `AndroidManifest.xml` declaration of:
262
263 ```xml
264 <uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS">
265 ```
266
267 Using a dictionary as a value allows you to specify additional attributes that detail, for example, the API version or combined constraints.
268
269 For example, specifying:
270
271 ```toml
272 permission."android.permission.BLUETOOTH_ADMIN" = {"android:maxSdkVersion"= "30"}
273 permission."android.permission.BLUETOOTH_SCAN" = {"android:usesPermissionFlags"= "neverForLocation"}
274 ```
275
276 will result in an `AndroidManifest.xml` declaration of:
277
278 ```xml
279 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/>
280 <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation"/>
281 ```
282
283 ### `target_os_version`
284
285 The API level that the app will target. This controls the version of the Android SDK that is used to build your app (by setting the `compileSdkVersion` for your app), and the forwards compatibility behavioral changes your app will enable (by setting the `targetSdkVersion` setting). This is *not* the Android version; it is the underlying API level. For example, Android 15 uses an API level of 35; if you wanted to specify Android 15 as your target API level, you would define `target_os_version = "35"`.
286
287 ### `version_code`
288
289 In addition to a version number, Android projects require a version "code". This code is an integer version of your version number that *must* increase with every new release pushed to the Play Store.
290
291 Briefcase will attempt to generate a version code by combining the version number with the build number. It does this by using each part of the main version number (padded to 3 digits if necessary) and the build number as 2 significant digits of the final version code:
292
293 * Version `1.0`, build 1 becomes `1000001` (i.e, `1`, `00`, `00`, `01`)
294 * Version `1.2`, build 37 becomes `1020037` (i.e., `1`, `02`, `00`, `37`)
295 * Version `1.2.37`, build 42 becomes `1023742` (i.e, `1`, `02`, `37`, `42`)
296 * Version `2020.6`, build 4 becomes `2020060004` (i.e., `2020`, `06`, `00`, `04`)
297
298 If you want to manually specify a version code by defining `version_code` in your application configuration. If provided, this value will override any auto-generated value.
299
300 ## Permissions { #android-permissions }
301
302 Briefcase cross platform permissions map to `<uses-permission>` declarations in the app's `AppManifest.xml`:
303
304 * [`permission.bluetooth`][permissionbluetooth]: `android.permission.BLUETOOTH` and other (see below for details)
305 * [`permission.camera`][permissioncamera]: `android.permission.CAMERA`
306 * [`permission.microphone`][permissioncamera]: `android.permission.RECORD_AUDIO`
307 * [`permission.coarse_location`][permissioncoarse_location]: `android.permission.ACCESS_COARSE_LOCATION`
308 * [`permission.fine_location`][permissionfine_location]: `android.permission.ACCESS_FINE_LOCATION`
309 * [`permission.background_location`][permissionbackground_location]: `android.permission.ACCESS_BACKGROUND_LOCATION`
310 * [`permission.photo_library`][permissionphoto_library]: `android.permission.READ_MEDIA_VISUAL_USER_SELECTED`
311
312 Every application will be automatically granted the `android.permission.INTERNET` and `android.permission.NETWORK_STATE` permissions.
313
314 Specifying a [`permission.bluetooth`][permissionbluetooth] permission will result in the following `<uses-permission>` declarations in the app's `AppManifest.xml`:
315
316 * `android.permission.ACCESS_COARSE_LOCATION`, with an attribute declaration of `android:maxSdkVersion="30"`. If `permission.coarse_location` is defined, the attribute declaration will be omitted
317 * `android.permission.ACCESS_FINE_LOCATION`, with an attribute declaration of `android:maxSdkVersion="30"`. If `permission.coarse_location` is defined, the attribute declaration will be omitted
318 * `android.permission.BLUETOOTH`, with an attribute declaration of `android:maxSdkVersion="30"`
319 * `android.permission.BLUETOOTH_ADMIN"`, with an attribute declaration of `android:maxSdkVersion="30"`
320 * `android.permission.BLUETOOTH_CONNECT`
321 * `android.permission.BLUETOOTH_SCAN`, with an attribute declaration of `android:usesPermissionFlags="neverForLocation"`. If `permission.fine_location` or `permission.coarse_location` is defined, the attribute declaration will be omitted.
322
323 Specifying a [`permission.camera`][permissioncamera] permission will result in the following non-required [`feature`][] definitions being implicitly added to your app:
324
325 * `android.hardware.camera`,
326 * `android.hardware.camera.any`,
327 * `android.hardware.camera.front`,
328 * `android.hardware.camera.external` and
329 * `android.hardware.camera.autofocus`.
330
331 Specifying the [`permission.coarse_location`][permissioncoarse_location], [`permission.fine_location`][permissionfine_location] or [`permission.background_location`][permissionbackground_location] permissions will result in the following non-required [`feature`][] declarations being implicitly added to your app:
332
333 * `android.hardware.location.network`
334 * `android.hardware.location.gps`
335
336 This is done to ensure that an app is not prevented from installing if the device doesn't have the given features. You can make the feature explicitly required by manually defining these feature requirements. For example, to make GPS hardware required, you could add the following to the Android section of your `pyproject.toml`:
337
338 ```toml
339 feature."android.hardware.location.gps" = True
340 ```
341
342 ## Platform quirks
343
344 ### Availability of third-party packages { #android-third-party-packages }
345
346 Briefcase is able to use third-party packages in Android apps. As long as the package is available on PyPI, or you can provide a wheel file for the package, it can be added to the [`requires`][] declaration in your `pyproject.toml` file and used by your app at runtime.
347
348 If the package is pure Python (i.e., it does not contain a binary library), that's all you need to do. To check whether a package is pure Python, look at the PyPI downloads page for the project; if the wheels provided are have a `-py3-none-any.whl` suffix, then they are pure Python wheels. If the wheels have version and platform-specific extensions (e.g., `-cp311-cp311-macosx_11_0_universal2.whl`), then the wheel contains a binary component.
349
350 If the package contains a binary component, that wheel needs to be compiled for Android. PyPI allows projects to upload Android-compatible wheels (identified by suffixes like `-cp314-cp314-android_24_arm64_v8a.whl`). However, at this time, most projects do not provide Android-compatible wheels.
351
352 This is expected to improve over time. In the meantime, Briefcase uses a [secondary repository](https://chaquo.com/pypi-13.1/) to provide pre-compiled Android wheels. This repository is maintained by the BeeWare project, and as a result, it does not have binary wheels for *every* package that is available on PyPI, or even every *version* of every package that is on PyPI. If you see any of the following messages when building an app for Android, then the package (or this version of it) probably isn't supported yet:
353
354 * "Could not find a version that satisfies the requirement"
355 * "No matching distributions available for your environment"
356
357 For advice on how to deal with this situation, see the [Chaquopy FAQ](https://chaquo.com/chaquopy/doc/current/faq.html#faq-pip).
358
359 ### Signing of `briefcase package` artefacts
360
361 While it is possible to use <span class="title-ref">briefcase package android</span> to produce an APK or AAB file for distribution, the file is *not* usable as-is. It must be signed regardless of whether you're distributing your app through the Play Store, or via loading the APK directly. For details on how to manually sign your code, see the instructions on [signing an Android App Bundle][sign-the-android-app-bundle].