Share
Embed
Dynamic Feature Module in Practice / DroidKaigi 2020
Presentation Note: https://drive.google.com/open?id=1nm3JrA4cnEemywWFelskdK8Z7QvZsAf6
star_zero
March 04, 2020
Transcript
111 slides
-
Dynamic Feature Module in Practice DroidKaigi 2020 2020/02/21 - 実践
Dynamic Feature Module - -
About me •Kenji Abe •Cookpad Inc. •Google Developers Expert for
Android •Twitter: @STAR_ZERO -
Talk about •What's Dynamic Feature Module •How to create Dynamic
Feature Module •How to test Dynamic Feature Module •etc... -
Dynamic Feature Module •Dynamic delivery of modules using App Bundle
•Modules can be installed and removed later •Can reduce app size -
Delivery option •On-demand delivery ‣ Modules are not included at
install time ‣ Install modules later as needed •At-install delivery ‣ Modules are included at install time ‣ Can be removed later -
Delivery option •Conditional delivery ‣ Install modules only on devices
that match the conditions ‣ Device features, User country, API Level •Google Play Instant ‣ "Try Now" button on the Google Play Store ‣ I will not talk about it this time -
Use cases •Installing features that are not used by the
majority of users as needed ‣ Posting feature in the app that most user only browse ‣ Help and support features •Remove no longer needed feature ‣ Register user account ‣ Onboarding flow •etc... -
apply plugin: 'com.android.dynamic-feature' android { compileSdkVersion 29 defaultConfig { minSdkVersion
21 targetSdkVersion 29 } } dependencies { implementation project(':app') } -
<manifest package="com.example.dfm.feature"> <dist:module dist:instant="false" dist:title="@string/title_feature"> <dist:delivery> <dist:on-demand /> </dist:delivery> <dist:fusing
dist:include="true" /> </dist:module> </manifest> -
<manifest package="com.example.dfm.feature"> <dist:module dist:instant="false" dist:title="@string/title_feature"> <dist:delivery> <dist:on-demand /> </dist:delivery> <dist:fusing
dist:include="true" /> </dist:module> </manifest> On demand delivery -
<manifest package="com.example.dfm.feature"> <dist:module dist:instant="false" dist:title="@string/title_feature"> <dist:delivery> <dist:install-time /> </dist:delivery> <dist:fusing
dist:include="true" /> </dist:module> </manifest> Include at install time -
app feature_one feature_two Using Dynamic Feature Module dynamicFeatures = [":feature_one",
":feature_two"] implementation project(':app') implementation project(':app') -
<!-- AndroidManifest --> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:name="com.google.android.play.core.splitcompat.SplitCompatApplication"> <!-- ... -->
</application> </manifest> // Application class class App : SplitCompatApplication() { // ... } OR -
// in dynamic feature module class FeatureActivity : AppCompatActivity() {
override fun attachBaseContext(base: Context) { super.attachBaseContext(base) SplitCompat.installActivity(this) } // ... } -
// in app module class MainActivity : AppCompatActivity() { override
fun attachBaseContext(base: Context) { super.attachBaseContext(base) SplitCompat.installActivity(this) } // ... } -
val manager = SplitInstallManagerFactory.create(context) if (manager.installedModules.contains("feature")) { // Already Installed
} val request = SplitInstallRequest.newBuilder() .addModule("feature") .build() manager.startInstall(request) .addOnSuccessListener { /* Success install request */ } .addOnFailureListener { /* Failure install request */ } -
val manager = SplitInstallManagerFactory.create(context) if (manager.installedModules.contains("feature")) { // Already Installed
} val request = SplitInstallRequest.newBuilder() .addModule("feature") .build() manager.startInstall(request) .addOnSuccessListener { /* Success install request */ } .addOnFailureListener { /* Failure install request */ } SplitInstallManager -
val manager = SplitInstallManagerFactory.create(context) if (manager.installedModules.contains("feature")) { // Already Installed
} val request = SplitInstallRequest.newBuilder() .addModule("feature") .build() manager.startInstall(request) .addOnSuccessListener { /* Success install request */ } .addOnFailureListener { /* Failure install request */ } Check if module is already Installed -
val manager = SplitInstallManagerFactory.create(context) if (manager.installedModules.contains("feature")) { // Already Installed
} val request = SplitInstallRequest.newBuilder() .addModule("feature") .build() manager.startInstall(request) .addOnSuccessListener { /* Success install request */ } .addOnFailureListener { /* Failure install request */ } Specify module name to install -
val manager = SplitInstallManagerFactory.create(context) if (manager.installedModules.contains("feature")) { // Already Installed
} val request = SplitInstallRequest.newBuilder() .addModule("feature") .build() manager.startInstall(request) .addOnSuccessListener { /* Success start install */ } .addOnFailureListener { /* Failure start install */ } Start to install -
private val listener = SplitInstallStateUpdatedListener { state -> when (state.status())
{ SplitInstallSessionStatus.INSTALLED -> // ... // ... } } override fun onStart() { super.onStart() manager.registerListener(listener) } override fun onStop() { super.onStop() manager.unregisterListener(listener) } -
private val listener = SplitInstallStateUpdatedListener { state -> when (state.status())
{ SplitInstallSessionStatus.INSTALLED -> // ... // ... } } override fun onStart() { super.onStart() manager.registerListener(listener) } override fun onStop() { super.onStop() manager.unregisterListener(listener) } Observe installation state (SplitInstallSessionState#status) -
private val listener = SplitInstallStateUpdatedListener { state -> when (state.status())
{ SplitInstallSessionStatus.INSTALLED -> // ... // ... } } override fun onStart() { super.onStart() manager.registerListener(listener) } override fun onStop() { super.onStop() manager.unregisterListener(listener) } -
private val listener = SplitInstallStateUpdatedListener { state -> when (state.status())
{ SplitInstallSessionStatus.INSTALLED -> // ... // ... } } override fun onStart() { super.onStart() manager.registerListener(listener) } override fun onStop() { super.onStop() manager.unregisterListener(listener) } -
•PENDING •REQUIRES_USER_CONFIRMATION •DOWNLOADING •DOWNLOADED •INSTALLING •INSTALLED •FAILED •CANCELING •CANCELED SplitInstallStateUpdatedListener
{ state -> when (state.status()) { SplitInstallSessionStatus.DOWNLOADING -> { val total = state.totalBytesToDownload() val downloaded = state.bytesDownloaded() // Show progress } } } -
•PENDING •REQUIRES_USER_CONFIRMATION •DOWNLOADING •DOWNLOADED •INSTALLING •INSTALLED •FAILED •CANCELING •CANCELED Requires
user's confirmation if module size exceeds 10MB -
•PENDING •REQUIRES_USER_CONFIRMATION •DOWNLOADING •DOWNLOADED •INSTALLING •INSTALLED •FAILED •CANCELING •CANCELED SplitInstallStateUpdatedListener
{ state -> when (state.status()) { SplitInstallSessionStatus.REQUIRES_USER_CONFIRMATION -> { manager.startConfirmationDialogForResult( state, activity, REQUEST_CODE ) } } } -
•PENDING •REQUIRES_USER_CONFIRMATION •DOWNLOADING •DOWNLOADED •INSTALLING •INSTALLED •FAILED •CANCELING •CANCELED Module
Installation completed Can use module resources -
lifecycleScope.launch { try { manager.requestInstall(listOf("feature")) } catch (e: SplitInstallException) {
// ... } } Request for installation using Coroutines -
private val listener = SplitInstallStateUpdatedListener( onRequiresConfirmation = { state ->
// ... }, onInstalled = { state -> // ... } ) Observe installation state -
lifecycleScope.launch { manager.requestProgressFlow().collect { state -> when (state.status()) { SplitInstallSessionStatus.INSTALLED
-> { Timber.d("SplitInstallSessionStatus.INSTALLED") } // ... } } } Observe installation state using Flow -
Conditional delivery •Install modules only on devices that match the
conditions ‣ Hardware and software features ‣ User country ‣ API Level •Can be installed later even if conditions do not match -
<dist:module> <dist:delivery> <dist:install-time> <dist:conditions> <dist:user-countries dist:exclude="true"> <dist:country dist:code="JP" /> </dist:user-countries>
<dist:min-sdk dist:value="24"/> </dist:conditions> </dist:install-time> </dist:delivery> <dist:fusing dist:include="true" /> </dist:module> -
<dist:module> <dist:delivery> <dist:install-time> <dist:conditions> <dist:user-countries dist:exclude="true"> <dist:country dist:code="JP" /> </dist:user-countries>
<dist:min-sdk dist:value="24"/> </dist:conditions> </dist:install-time> </dist:delivery> <dist:fusing dist:include="true" /> </dist:module> Exclude Japan -
<dist:module> <dist:delivery> <dist:install-time> <dist:conditions> <dist:user-countries dist:exclude="true"> <dist:country dist:code="JP" /> </dist:user-countries>
<dist:min-sdk dist:value="24"/> </dist:conditions> </dist:install-time> </dist:delivery> <dist:fusing dist:include="true" /> </dist:module> API Level 24 or more -
Test / Debug •Google Play internal test •Internal App Sharing
•FakeSplitInstallManager -
Google Play internal test •Release build aab file is required
‣ Needs release signing and correct VersionCode •It takes time and effort •Can not debug -
Internal App Sharing •Can upload debug build aab ‣ Debug
signing, any VersionCode •Debugger can be attached •Easy and quick -
Internal App Sharing •Triple-T/gradle-play-publisher ‣ https://github.com/Triple-T/gradle-play-publisher ‣ Upload and launch
to Play Store by gradle command -
FakeSplitInstallManager •Offline testing of module installation •Use FakeSplitInstallManager instead of
SplitInstallManager •https://link.medium.com/J0eOwvXds4 -
Navigation Component DFM •Implement screen transitions effortlessly ‣ It also
installs the module •Customizable •Still alpha version only •http://goo.gle/dynamic-feature-nav -
<!-- navigation/nav_graph.xml --> <navigation> <fragment> <action android:id="@+id/action_on_demand" app:destination="@id/on_demand_activity" /> </fragment>
<activity android:id="@+id/on_demand_activity" android:name="com.example.feature.SampleActivity" app:moduleName="feature" /> </navigation> -
<!-- navigation/nav_graph.xml --> <navigation> <fragment> <action android:id="@+id/action_on_demand" app:destination="@id/on_demand_activity" /> </fragment>
<activity android:id="@+id/on_demand_activity" android:name="com.example.feature.SampleActivity" app:moduleName="feature" /> </navigation> -
class DynamicProgressFragment : AbstractProgressFragment() { override fun onCancelled() { }
override fun onFailed(errorCode: Int) { } override fun onProgress(status: Int, bytesDownloaded: Long, bytesTotal: Long) { } } -
findNavController().apply { val fragmentNavigator = navigatorProvider[DynamicFragmentNavigator::class] val graphNavigator = navigatorProvider[DynamicGraphNavigator::class]
graphNavigator.installDefaultProgressDestination { fragmentNavigator.createDestination().apply { className = DynamicProgressFragment::class.java.name id = R.id.dfn_progress_fragment } } navigate(R.id.action_on_demand) } -
if (installMonitor.isInstallRequired) { installMonitor.status.observe( lifecycleOwner, object: Observer<SplitInstallSessionState> { override fun
onChanged(sessionState: SplitInstallSessionState) { when (sessionState.status()) { SplitInstallSessionStatus.INSTALLED -> { findNavController().navigate(R.id.action_on_demand) } // ... } if (sessionState.hasTerminalStatus()) { installMonitor.status.removeObserver(this) } } } ) } -
if (installMonitor.isInstallRequired) { installMonitor.status.observe( lifecycleOwner, object: Observer<SplitInstallSessionState> { override fun
onChanged(sessionState: SplitInstallSessionState) { when (sessionState.status()) { SplitInstallSessionStatus.INSTALLED -> { findNavController().navigate(R.id.action_on_demand) } // ... } if (sessionState.hasTerminalStatus()) { installMonitor.status.removeObserver(this) } } } ) } ture if need to install module -
if (installMonitor.isInstallRequired) { installMonitor.status.observe( lifecycleOwner, object: Observer<SplitInstallSessionState> { override fun
onChanged(sessionState: SplitInstallSessionState) { when (sessionState.status()) { SplitInstallSessionStatus.INSTALLED -> { findNavController().navigate(R.id.action_on_demand) } // ... } if (sessionState.hasTerminalStatus()) { installMonitor.status.removeObserver(this) } } } ) } Observe installation state LiveData -
if (installMonitor.isInstallRequired) { installMonitor.status.observe( lifecycleOwner, object: Observer<SplitInstallSessionState> { override fun
onChanged(sessionState: SplitInstallSessionState) { when (sessionState.status()) { SplitInstallSessionStatus.INSTALLED -> { findNavController().navigate(R.id.action_on_demand) } // ... } if (sessionState.hasTerminalStatus()) { installMonitor.status.removeObserver(this) } } } ) } Navigate when installed -
if (installMonitor.isInstallRequired) { installMonitor.status.observe( lifecycleOwner, object: Observer<SplitInstallSessionState> { override fun
onChanged(sessionState: SplitInstallSessionState) { when (sessionState.status()) { SplitInstallSessionStatus.INSTALLED -> { findNavController().navigate(R.id.action_on_demand) } // ... } if (sessionState.hasTerminalStatus()) { installMonitor.status.removeObserver(this) } } } ) } Remove observer when installation completes -
Screen navigation •app module cannot directly refer to classes in
dynamic feature modules •Needs use reflection ‣ Be cautious when using Proguard / R8 •Navigation Component helps -
Specify module name •Only the module name is needed •For
example: features:sample ‣ Specify only "sample" val request = SplitInstallRequest.newBuilder() .addModule("sample").build() -
R classes •R classes are not merged, so packages are
different •Be careful of the packages when importing R classes -
<!-- app/string.xml --> <resources> <string name="base_string">Base</string> </resources> app com.example.app feature
com.example.app.feature <!-- feature/string.xml --> <resources> <string name="dynamic_string">Dynamic</string> </resources> com.example.app.feature.R com.example.app.R -
import com.example.app.feature.R val dynamic = getString(R.string.dynamic_string) val base = getString(R.string.base_string)
import com.example.app.feature.R val dynamic = getString(R.string.dynamic_string) val base = getString(com.example.app.R.string.base_string) ⭕ OK -
// in app module typealias appString = com.example.app.R.string // in
feature module import com.example.app.feature.R import com.example.app.appString val dynamic = getString(R.string.dynamic_string) val base = getString(appString.base_string) -
Conclusion •Dynamic Feature Module is not so hard •Useful tools
for Testing/Debugging are available •Dynamic Navigation Component is so useful •There are still bugs that you may encounter