Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Dynamic code loading sample
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek-kalicinski authored and Wojtek Kaliciński committed May 6, 2019
0 parents commit a214ad7
Show file tree
Hide file tree
Showing 53 changed files with 2,412 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
.DS_Store
build/
/captures
.externalNativeBuild
/.idea
/keystore.properties
36 changes: 36 additions & 0 deletions .google/packaging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2019 Google LLC
#
# Licensed 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.
#
# GOOGLE SAMPLE PACKAGING DATA
#
# This file is used by Google as part of our samples packaging process.
# End users may safely ignore this file. It has no relevance to other systems.
---
# Values: {DRAFT | PUBLISHED | INTERNAL | DEPRECATED | SUPERCEDED}
status: PUBLISHED

# Optional, put additional explanation here for DEPRECATED or SUPERCEDED.
# statusNote:

technologies: [Android]
categories: [Dynamic Features]
languages: [Kotlin]
solutions: [Mobile]

github: googlesamples/android-dynamic-code-loading

# Values: BEGINNER | INTERMEDIATE | ADVANCED | EXPERT
level: ADVANCED

license: apache2
36 changes: 36 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# How to become a contributor and submit your own code

## Contributor License Agreements

We'd love to accept your sample apps and patches! Before we can take them, we
have to jump a couple of legal hurdles.

Please fill out either the individual or corporate Contributor License Agreement
(CLA).

* If you are an individual writing original source code and you're sure you
own the intellectual property, then you'll need to sign an [individual CLA]
(https://developers.google.com/open-source/cla/individual).
* If you work for a company that wants to allow you to contribute your work,
then you'll need to sign a [corporate CLA]
(https://developers.google.com/open-source/cla/corporate).

Follow either of the two links above to access the appropriate CLA and
instructions for how to sign and return it. Once we receive it, we'll be able to
accept your pull requests.

## Contributing A Patch

1. Submit an issue describing your proposed change to the repo in question.
1. The repo owner will respond to your issue promptly.
1. If your proposed change is accepted, and you haven't already done so, sign a
Contributor License Agreement (see details above).
1. Fork the desired repo, develop and test your code changes.
1. Ensure that your code adheres to the existing style in the sample to which
you are contributing. Refer to the
[Google Cloud Platform Samples Style Guide]
(https://github.com/GoogleCloudPlatform/Template/wiki/style.html) for the
recommended coding standards for this organization.
1. Ensure that your code has an appropriate set of unit tests which all pass.
1. Submit a pull request.

653 changes: 653 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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](https://developer.android.com/reference/java/util/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.
```
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
68 changes: 68 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.google.android.samples.dynamiccodeloading"
minSdkVersion 21
targetSdkVersion 28
versionCode 33
versionName "1.0.33"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
debugR8 {
initWith(debug)
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}

release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
}

flavorDimensions "codeLoading"

productFlavors {
reflect {
dimension "codeLoading"
}

serviceLoader {
dimension "codeLoading"
}

dagger {
dimension "codeLoading"
}
}

dynamicFeatures = [":storage"]
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

api 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

implementation "com.google.android.play:core:1.4.1"

daggerApi 'com.google.dagger:dagger:2.16'
api 'com.google.dagger:dagger-android-support:2.16'

kapt 'com.google.dagger:dagger-compiler:2.16'

implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha04'
androidTestImplementation 'androidx.test:rules:1.2.0-alpha04'
}
41 changes: 41 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
-keepattributes SourceFile,LineNumberTable
#-keepattributes *Annotation*
#

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile


-keep class com.google.android.samples.storage.StorageFeatureImpl {
com.google.android.samples.storage.StorageFeatureImpl$Provider Provider;
}

-keep class com.google.android.samples.storage.StorageFeatureImpl$Provider {
*;
}

# There was a bug, but it's gone now. Uncomment on earlier R8 versions (?)
#-keep interface com.google.android.samples.dynamiccodeloading.StorageFeature {
# *;
#}

-keep class kotlin.Metadata {
*;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2019 Google LLC
*
* Licensed 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.
*
*/
package com.google.android.samples.dynamiccodeloading

import android.preference.PreferenceManager
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import org.junit.Before
import org.junit.Rule
import org.junit.Test


@LargeTest
class MainActivityTest {

@Rule
@JvmField
var mActivityTestRule = ActivityTestRule(MainActivity::class.java, false, false)


/**
* Clear sharedpreferences before each run
*/
@Before
fun before() {
PreferenceManager.getDefaultSharedPreferences(
InstrumentationRegistry.getInstrumentation().getTargetContext().applicationContext
).edit().clear().commit()
}

/**
* This is an (overly) simplified test that can be used locally with all APK splits installed.
* It doesn't test Play Core APIs (on-demand delivery) since this is impossible without going through the Play Store
*/
@Test
fun mainActivityTest() {
mActivityTestRule.launchActivity(null)
val appCompatButton = onView(withId(R.id.incrementButton))
appCompatButton.perform(click())
appCompatButton.perform(click())
appCompatButton.perform(click())

onView(withId(R.id.saveButton)).perform(click())

InstrumentationRegistry.getInstrumentation().runOnMainSync(Runnable { mActivityTestRule.activity.recreate() })

val textView = onView(withId(R.id.counterText))
textView.check(matches(withText("3")))
}
}
25 changes: 25 additions & 0 deletions app/src/dagger/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2019 Google LLC
~
~ Licensed 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.
~
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="com.google.android.samples.dynamiccodeloading">

<application
tools:replace="android:name"
android:name=".MyApplication"
tools:ignore="AllowBackup,GoogleAppIndexingWarning"/>

</manifest>
Loading

0 comments on commit a214ad7

Please sign in to comment.