|
| 1 | +Dynamic code loading sample |
| 2 | +============ |
| 3 | + |
| 4 | +This sample demonstrates how to load and use classes from dynamic feature modules. |
| 5 | + |
| 6 | +Introduction |
| 7 | +------------ |
| 8 | +Dynamic feature modules (DFMs) are modularized parts of your Android app. You will encounter them when you switch |
| 9 | +to using Android App Bundles and want to enable on-demand or conditional feature delivery. |
| 10 | + |
| 11 | +You can read more about app bundles at http://g.co/androidappbundle |
| 12 | + |
| 13 | +Contrary to regular library modules, DFMs depend on the base module (`com.android.application`) of your app. |
| 14 | +Because of this, classes defined in DFMs are not visible from the app module at compile time and need to be accessed |
| 15 | +through reflection. |
| 16 | + |
| 17 | +This sample demonstrates three different approaches to safely access code from the feature module |
| 18 | +using at most 1 reflection call. |
| 19 | + |
| 20 | +Common code is included in the `main` sourceSets, which contains the basic UI and ViewModel for this sample. |
| 21 | +This also contains the `StorageFeature` interface (defined in the `app` module), |
| 22 | +which will be implemented in the `storage` dynamic feature module. |
| 23 | + |
| 24 | +The other sourceSets contain code that obtains an instance of the concrete implementation from the DFM |
| 25 | +using three approaches, split into the following product flavors: |
| 26 | + |
| 27 | + `reflect/` -> uses a straight reflection call |
| 28 | + `serviceLoader/` -> uses the [ServiceLoader](https://developer.android.com/reference/java/util/ServiceLoader) mechanism |
| 29 | + `dagger/` -> uses Dagger 2, to enable easy instantiation of complex object graphs from the DFM |
| 30 | + |
| 31 | +Please note that all approaches use 1 reflect call to instantiate the `StorageFeature.Provider`, |
| 32 | +although in the `serviceLoader` and `dagger` approaches they're somewhat hidden from the user. |
| 33 | + |
| 34 | +Additionally, when compiling the `serviceLoader` flavor using recent versions of R8, |
| 35 | +the optimizer does away with dynamic class lookup and reflection, and the resulting DEX code uses direct class |
| 36 | +instantiation. |
| 37 | + |
| 38 | +Pre-requisites |
| 39 | +-------------- |
| 40 | + |
| 41 | +* Android Studio 3.4 |
| 42 | +* Access to Play Console in order to test the on-demand features |
| 43 | + |
| 44 | +Getting Started |
| 45 | +--------------- |
| 46 | + |
| 47 | +Use Android Studio to open the project and check out the various Build Variants available. |
| 48 | + |
| 49 | +Alternatively, build from the commandline using `./gradlew reflectDebug` |
| 50 | +or `./gradlew serviceLoaderDebug` or `./gradlew daggerDebug` |
| 51 | + |
| 52 | +Support |
| 53 | +------- |
| 54 | + |
| 55 | +If you've found an error in this sample, please file an issue: |
| 56 | +https://github.com/googlesamples/android-dynamic-code-loading/issues |
| 57 | + |
| 58 | +Patches are encouraged, and may be submitted by forking this project and |
| 59 | +submitting a pull request through GitHub. |
| 60 | + |
| 61 | +License |
| 62 | +------- |
| 63 | + |
| 64 | +``` |
| 65 | +Copyright 2019 Google LLC. |
| 66 | +
|
| 67 | +Licensed to the Apache Software Foundation (ASF) under one or more contributor |
| 68 | +license agreements. See the NOTICE file distributed with this work for |
| 69 | +additional information regarding copyright ownership. The ASF licenses this |
| 70 | +file to you under the Apache License, Version 2.0 (the "License"); you may not |
| 71 | +use this file except in compliance with the License. You may obtain a copy of |
| 72 | +the License at |
| 73 | +
|
| 74 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 75 | +
|
| 76 | +Unless required by applicable law or agreed to in writing, software |
| 77 | +distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 78 | +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 79 | +License for the specific language governing permissions and limitations under |
| 80 | +the License. |
| 81 | +``` |
0 commit comments