|
| 1 | +--- |
| 2 | +title: Android App Bundle |
| 3 | +description: Learn what is Android App Bundle and how to use it. |
| 4 | +position: 50 |
| 5 | +slug: android-app-bundle |
| 6 | +--- |
| 7 | + |
| 8 | +# Android App Bundle |
| 9 | +An Android App Bundle is a new publishing format that contains all the compiled code and resources of your app, but leaves the actual APK generation and signing to Google Play. The store then uses the app bundle to generate and serve optimized APKs based on the device configuration of the specific user. In general, the benefit of using Android App Bundles is that you no longer have to build, sign, and manage multiple APKs to support different devices, and users get smaller, more optimized downloads. For more information about the Android App Bundle, see the About Android App Bundles article in the official [Android Developer documentation](https://developer.android.com/guide/app-bundle/). |
| 10 | + |
| 11 | +## Available configurations |
| 12 | +By default, the 'arm64-v8a' CPU architecture is excluded from the build of NativeScript application. The following configuration will enable it, but there is a chance that on older devices with this architecture this might affect the startup times. |
| 13 | + |
| 14 | +``` |
| 15 | +android { |
| 16 | +.... |
| 17 | + defaultConfig { |
| 18 | + .... |
| 19 | + ndk { |
| 20 | + abiFilters.clear() |
| 21 | + } |
| 22 | + } |
| 23 | +.... |
| 24 | +``` |
| 25 | + |
| 26 | +## Additional optimizations |
| 27 | +The performance and size of the application can be improved even further by configuring the `nativescript-dev-webpack` plugin. |
| 28 | + |
| 29 | +The default file format that the `nativescript-dev-webpack` plugin produces when you execute `tns build android --bundle --env.uglify --env.snapshot --aab` is a `.blob` file. |
| 30 | + |
| 31 | +> Note: The snapshot generation feature is limited to macOS and Linux platforms due to inability to build the `mksnapshot` tool when running on Windows. Currently, the --env.snapshot flag is ignored on Windows. |
| 32 | +
|
| 33 | +For each of the architectures that you specify in your `webpack.config.js`, the plugin will produce a `snapshot.blob` file inside `assets/snapshots/${target_arch}/snapshot.blob`. Those files are **not** subject to Android App Bundle and you will find a corresponding `blob` for each architecture in the resulting `.apks`. This will increase the size of the resulting `.apks`. |
| 34 | + |
| 35 | +If you want to take advantage of Android App Bundle you will need to instruct the `nativescript-dev-webpack` plugin to produce a `.so` snapshot. For this purpose, you will need to have the Android NDK installed on your system. It is strongly recommended that the same version of the NDK is used to produce the snapshot file as the one used to compile the {N} runtime itself. Currently we use NDK r16b. |
| 36 | + |
| 37 | +And here are the necessary changes that you need to do in your `webpack.config.js` in order to enable `.so` snapshot file generation: |
| 38 | + |
| 39 | +``` |
| 40 | +if (env.snapshot) { |
| 41 | + plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({ |
| 42 | + chunk: "vendor", |
| 43 | + projectRoot: __dirname, |
| 44 | + webpackConfig: config, |
| 45 | + targetArchs: ["arm", "arm64", "ia32"], |
| 46 | + useLibs: true, |
| 47 | + androidNdkPath: "/Library/android-sdk-macosx/ndk-bundle" |
| 48 | + })); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +The two important switches to note here are `useLibs: true` (which instructs the plugin to produce a `.so` file) and `androidNdkPath` (make sure you point this to a folder containing Android NDK r12b). |
| 53 | + |
| 54 | +One final thing before building the application is to instruct gradle to actually include the resulting snapshot into the final apk. This can be done in your `App_Resources/Android/app.gradle`: |
| 55 | + |
| 56 | +``` |
| 57 | +sourceSets { |
| 58 | + main { |
| 59 | + jniLibs.srcDirs = ["$projectDir/libs/jni", "$projectDir/snapshot-build/build/ndk-build/libs"] |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## Testing the produced `.aab` file |
| 65 | +In order to test the `apk` files that Google Play will produce from the `.aab` for a specific device you will need to use the Android `bundletool` or upload to Google Play and use a test track. |
| 66 | + |
| 67 | +If you use the `bundletool` you should first generate an `.apks` file that will later be used to deploy on a device. |
| 68 | + |
| 69 | +``` |
| 70 | +java -jar <toolPath>/bundletool.jar build-apks --bundle=<somePath>/app.aab --output="<somePath>/my_app.apks" |
| 71 | +--ks=<path to keystore file> |
| 72 | +--ks-pass=pass:<keystore pass> |
| 73 | +--ks-key-alias=<key alias> |
| 74 | +--key-pass=pass:<key pass> |
| 75 | +``` |
| 76 | + |
| 77 | +Then you can install the application on a connected device by executing: |
| 78 | +``` |
| 79 | +java -jar <toolPath>/bundletool.jar install-apks --apks="somePath/my_app.apks" --device-id=<deviceId> |
| 80 | +``` |
| 81 | + |
| 82 | +You can find more information about using Android `bundletool` [here](https://developer.android.com/studio/command-line/bundletool). |
0 commit comments