How to download dynamic module ?

If you don’t know how to create dynamic feature module please visit this link.

If you already have your module ready than we will check how to download the module in our application and use it. So let’s start :-
First you have to add Android Play Core  library in your project :
api "com.google.android.play:core:${versions.playcore}"

After that sync project and add below line in your manifest file :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:dist="http://schemas.android.com/apk/distribution"
    package="com.google.android.samples.dynamicfeatures">
    <dist:module dist:instant="true" />
  <Application/>
</manifest>

Once you have done with your manifest file than you have to install splitCompact in your project. So for that you have to add the following code in your project Application class.
class AppApplication : SplitCompatApplication(){
    override fun onCreate() {
                 super.onCreate()
                  SplitCompat.install(this)
    }
}
Add below code in your activity class to download the module :

class MainActivity :
    BaseSplitActivity()

lateinit var manager: SplitInstallManager

override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(newBase)
        SplitCompat.install(this)
}

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        manager = SplitInstallManagerFactory.create(this)
    // Create Manager 
}

    Private fun makeRequest(){
        // Check module is already download or not
if (manager.installedModules.contains("tutorials")) {
            onSuccessLoadModule()
            return
        }

// Create request to install a module by name.
val request = SplitInstallRequest.newBuilder()
            .addModule("tutorials")
            .build()


       
/ Load and install the requested language.
manager.startInstall(request)
}

 // Listener can be registered even without directly triggering a download.
    override fun onResume() {
        manager.registerListener(mSplitManagerListener)
        super.onResume()
    }

    // Make sure to dispose of the listener once it's no longer needed.
    override fun onPause() {
        manager.unregisterListener(mSplitManagerListener)
        super.onPause()
    }

    /** To handle callback from manager for requested modules */
    private val mSplitManagerListener =
        SplitInstallStateUpdatedListener { state ->

            when (state.status()) {
                SplitInstallSessionStatus.DOWNLOADING -> {
                    //  In order to see this, the application has to be uploaded to the Play Store.
                }
                SplitInstallSessionStatus.REQUIRES_USER_CONFIRMATION -> {
                    /*
                      This may occur when attempting to download a sufficiently large module.

                      In order to see this, the application has to be uploaded to the Play Store.
                      Then features can be requested until the confirmation path is triggered.
                     */
                    manager.startConfirmationDialogForResult(state, this, 0)
                }
                SplitInstallSessionStatus.INSTALLED -> {
                    onSuccessLoadModule()
                }

                SplitInstallSessionStatus.FAILED -> {
                    Toast.makeText(this, "Failed to download", Toast.LENGTH_LONG)
                        .show()
                }
            }

        private fun onSuccessLoadModule() {
// Start you activity from here
    Intent().setClassName(BuildConfig.APPLICATION_ID, TUTORIAL_ACTIVITY)
            .also {
        startActivity(it)
}         }

}
 

Comments