Giter VIP home page Giter VIP logo

android-dynamic-code-loading's Introduction

Dynamic code loading sample

This sample demonstrates how to load and use classes from dynamic feature modules.

Introduction

Dynamic feature modules (DFMs) are modularized parts of your Android app. You will encounter them when you switch to using Android App Bundles and want to enable on-demand or conditional feature delivery.

You can read more about app bundles at http://g.co/androidappbundle

Contrary to regular library modules, DFMs depend on the base module (com.android.application) of your app. Because of this, classes defined in DFMs are not visible from the app module at compile time and need to be accessed through reflection.

This sample demonstrates three different approaches to safely access code from the feature module using at most 1 reflection call.

Common code is included in the main sourceSets, which contains the basic UI and ViewModel for this sample. This also contains the StorageFeature interface (defined in the app module), which will be implemented in the storage dynamic feature module.

The other sourceSets contain code that obtains an instance of the concrete implementation from the DFM using three approaches, split into the following product flavors:

reflect/ -> uses a straight reflection call serviceLoader/ -> uses the ServiceLoader mechanism dagger/ -> uses Dagger 2, to enable easy instantiation of complex object graphs from the DFM

Please note that all approaches use 1 reflect call to instantiate the StorageFeature.Provider, although in the serviceLoader and dagger approaches they're somewhat hidden from the user.

Additionally, when compiling the serviceLoader flavor using recent versions of R8, the optimizer does away with dynamic class lookup and reflection, and the resulting DEX code uses direct class instantiation.

Pre-requisites

  • Android Studio 3.4
  • Access to Play Console in order to test the on-demand features

Getting Started

Use Android Studio to open the project and check out the various Build Variants available.

Alternatively, build from the commandline using ./gradlew reflectDebug or ./gradlew serviceLoaderDebug or ./gradlew daggerDebug

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-dynamic-code-loading/issues

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub.

License

Copyright 2019 Google LLC.

Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements.  See the NOTICE file distributed with this work for
additional information regarding copyright ownership.  The ASF licenses this
file to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License.  You may obtain a copy of
the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
License for the specific language governing permissions and limitations under
the License.

android-dynamic-code-loading's People

Contributors

wojtek-kalicinski avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.