我在我的应用程序中使用动态功能模块,这是我的App
课程 -
@HiltAndroidApp
class DogApp : SplitCompatApplication() {
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
SplitCompat.install(this)
}
}
这是我的MainActivity
-
private var sessionId: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fetchModules()
}
private fun fetchModules() {
val splitInstallManager = SplitInstallManagerFactory.create(this)
val request = SplitInstallRequest
.newBuilder()
.addModule("dogProfile")
.build()
val listener =
SplitInstallStateUpdatedListener { splitInstallSessionState ->
if (splitInstallSessionState.sessionId() == sessionId) {
when (splitInstallSessionState.status()) {
SplitInstallSessionStatus.INSTALLED -> {
Log.e("hi", "Installed")
}
SplitInstallSessionStatus.DOWNLOADING -> {
val totalBytes = splitInstallSessionState.totalBytesToDownload()
val progress = splitInstallSessionState.bytesDownloaded()
Log.e("hi", "Downloading$totalBytes...$progress")
}
SplitInstallSessionStatus.INSTALLING -> {
Log.e("hi", "Installing")
}
}
}
}
splitInstallManager.registerListener(listener)
splitInstallManager.startInstall(request)
.addOnFailureListener { e -> Log.e("hi", "Exception: $e") }
.addOnSuccessListener {
Log.e("hi", "Success")
sessionId = it
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navHostFragment.navController.navigate(R.id.homeView)
}
}
当我构建 apk 并在我的设备/模拟器上运行时,我看到永远存在的“安装模块”并且我看到了日志 -
Installed
Success
这是我永远看到的屏幕-
我在做什么错误?