Lukacs Berki | 99389fd | 2015-07-20 08:36:28 +0000 | [diff] [blame] | 1 | In order to build these examples, add the following two rules to the top-level `WORKSPACE` file (two directories above this file): |
| 2 | |
| 3 | ```python |
| 4 | android_sdk_repository( |
| 5 | name="androidsdk", |
John Field | debf73a | 2015-07-27 14:59:33 +0000 | [diff] [blame] | 6 | path="<full path to your Android SDK>", |
| 7 | api_level=<api level>, |
Adam Michael | aa4ca94 | 2016-12-21 23:06:37 +0000 | [diff] [blame] | 8 | ) |
Alex Humesky | f397120 | 2015-07-22 19:01:49 +0000 | [diff] [blame] | 9 | |
Lukacs Berki | 99389fd | 2015-07-20 08:36:28 +0000 | [diff] [blame] | 10 | android_ndk_repository( |
| 11 | name="androidndk", |
| 12 | path="<path to your Android NDK>", |
Adam Michael | aa4ca94 | 2016-12-21 23:06:37 +0000 | [diff] [blame] | 13 | api_level=<api_level>, |
| 14 | ) |
Lukacs Berki | 99389fd | 2015-07-20 08:36:28 +0000 | [diff] [blame] | 15 | ``` |
Adam Michael | aa4ca94 | 2016-12-21 23:06:37 +0000 | [diff] [blame] | 16 | |
| 17 | For the `android_sdk_repository` rule, the value of `api_level` corresponds to |
| 18 | a directory in the SDK containing the specific version of `android.jar` to |
| 19 | compile against. For example, if `path = "/Users/xyzzy/Library/Android/sdk"` and |
| 20 | `api_level = 21`, then the directory |
| 21 | `/Users/xyzzy/Library/Android/sdk/platforms/android-21` must exist. |
Lukacs Berki | 99389fd | 2015-07-20 08:36:28 +0000 | [diff] [blame] | 22 | |
John Field | debf73a | 2015-07-27 14:59:33 +0000 | [diff] [blame] | 23 | Similarly, for the `android_ndk_repository` rule, the value of the `api_level` |
| 24 | attribute corresponds to a directory containing the NDK libraries for that |
| 25 | API level. For example, if |
| 26 | `path=/Users/xyzzy/Library/Android/android-ndk-r10e` and |
| 27 | `api_level=21`, then you your NDK must contain the directory |
| 28 | `/Users/xyzzy/Library/Android/android-ndk-r10e/platforms/android-21`. |
| 29 | |
Adam Michael | aa4ca94 | 2016-12-21 23:06:37 +0000 | [diff] [blame] | 30 | The example `android_binary` depends on |
| 31 | `@androidsdk//com.android.support:appcompat-v7-25.0.0`, so you will need to |
| 32 | install the Google Support Libraries version 25.0.0 from the Android SDK |
| 33 | Manager. |
| 34 | |
John Field | debf73a | 2015-07-27 14:59:33 +0000 | [diff] [blame] | 35 | The following command can be used to build the example app: |
Lukacs Berki | 99389fd | 2015-07-20 08:36:28 +0000 | [diff] [blame] | 36 | |
| 37 | ``` |
Googler | 1afdaf4 | 2022-06-27 10:38:16 -0700 | [diff] [blame] | 38 | bazel build //examples/android/java/bazel:hello_world --java_language_version=8 |
Lukacs Berki | 99389fd | 2015-07-20 08:36:28 +0000 | [diff] [blame] | 39 | ``` |
| 40 | |
Lukacs Berki | 99389fd | 2015-07-20 08:36:28 +0000 | [diff] [blame] | 41 | We also have a nice way to speed up the edit-compile-install development cycle for physical Android devices and emulators: Bazel knows what code changed since the last build, and can use this knowledge to install only the changed code to the device. This currently works with L devices and changes to Java code and Android resources. To try this out, take an `android_binary` rule and: |
Alex Humesky | f397120 | 2015-07-22 19:01:49 +0000 | [diff] [blame] | 42 | |
Lukacs Berki | 99389fd | 2015-07-20 08:36:28 +0000 | [diff] [blame] | 43 | * Set the `proguard_specs` attribute to `[]` (the empty list) or just omit it altogether |
| 44 | * Set the `multidex` attribute to `native` |
| 45 | * Set the `dex_shards` attribute to a number between 2 and 200. This controls the size of chunks the code is split into. As this number is increased, compilation and installation becomes faster but app startup becomes slower. A good initial guess is 10. |
| 46 | * Connect your device over USB to your workstation and enable USB debugging on it |
Lukacs Berki | 841cf45 | 2015-07-20 09:24:12 +0000 | [diff] [blame] | 47 | * Run `bazel mobile-install <android_binary rule>` |
Lukacs Berki | 99389fd | 2015-07-20 08:36:28 +0000 | [diff] [blame] | 48 | * Edit Java code or Android resources |
Damien Martin-Guillerez | 217e3ad | 2015-08-25 09:18:36 +0000 | [diff] [blame] | 49 | * Run `bazel mobile-install --incremental <android_binary rule>` |
Lukacs Berki | 99389fd | 2015-07-20 08:36:28 +0000 | [diff] [blame] | 50 | |
| 51 | Note that if you change anything other than Java code or Android resources (C++ code or something on the device), you must omit the `--incremental` command line option. Yes, we know that this is also clunky and we are working on improving it. |