android - 动态功能 Activity 未加载,卡在安装中

标签 android generics app-bundle android-app-bundle

我正在尝试为我的项目实现应用程序包。在启动动态模块 Activity 时,它正在下载模块并尝试安装该模块,但无法完全安装它。它被卡住了,如下图所示。 enter image description here

以下链接供引用。 https://github.com/googlesamples/android-dynamic-features

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import com.google.android.play.core.splitinstall.*
import com.google.android.play.core.splitinstall.model.SplitInstallSessionStatus
import com.lenskart.app.R

private const val modulePackageName = "com.test.feature"

private const val htoClassname = "$modulePackageName.hto.HtoActivity"

private const val atHomeClassname = "$modulePackageName.athome.AtHomeActivity"

class BundleActivity : AppCompatActivity() {

    private val listener = SplitInstallStateUpdatedListener { state ->
        val multiInstall = state.moduleNames().size > 1
        state.moduleNames().forEach { name ->
            when (state.status()) {
                SplitInstallSessionStatus.DOWNLOADING -> {
                    displayLoadingState(state, "Downloading $name")
                }
                SplitInstallSessionStatus.REQUIRES_USER_CONFIRMATION -> {
                    startIntentSender(state.resolutionIntent().intentSender, null, 0, 0, 0)
                }
                SplitInstallSessionStatus.INSTALLED -> {
                    displayLoadingState(state, "Installed $name")
                    onSuccessfulLoad(name, launch = !multiInstall)
                }

                SplitInstallSessionStatus.INSTALLING -> displayLoadingState(state, "Installing $name")
                SplitInstallSessionStatus.FAILED -> {
                    Log.e(TAG, "Error: ${state.errorCode()} for module ${state.moduleNames()}")
                }

                SplitInstallSessionStatus.CANCELED -> displayLoadingState(state, "Cancelled $name")
                SplitInstallSessionStatus.UNKNOWN -> displayLoadingState(state, "Unknown thingy $name")
                SplitInstallSessionStatus.PENDING -> displayLoadingState(state, "Pending $name")

            }
        }
    }

    private val moduleHto by lazy { "hto" }
    private val moduleAtHome by lazy { "athome" }

    private lateinit var manager: SplitInstallManager

    private lateinit var progressBar: ProgressBar
    private lateinit var progressText: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bundle)
        manager = SplitInstallManagerFactory.create(this)
        initializeViews()
        loadAndLaunchModule(moduleAtHome)
    }

    override fun onResume() {
        manager.registerListener(listener)
        super.onResume()
    }

    override fun onPause() {
        manager.unregisterListener(listener)
        super.onPause()
    }

    private fun loadAndLaunchModule(name: String) {
        updateProgressMessage("Loading module $name")
        if (manager.installedModules.contains(name)) {
            updateProgressMessage("Already installed")
            onSuccessfulLoad(name, launch = true)
            return
        }
        val request = SplitInstallRequest.newBuilder()
                .addModule(name)
                .build()

        manager.startInstall(request)

        updateProgressMessage("Starting install for $name")
    }

    private fun onSuccessfulLoad(moduleName: String, launch: Boolean) {
        if (true) {
            when (moduleName) {
                moduleHto -> launchActivity(htoClassname)
                moduleAtHome -> launchActivity(atHomeClassname)
            }
        }

        displayButtons()
    }

    private fun launchActivity(className: String) {
        Intent().setClassName(packageName, className)
                .also {
                    startActivity(it)
                }
    }

    private fun displayLoadingState(state: SplitInstallSessionState, message: String) {
        displayProgress()

        progressBar.max = state.totalBytesToDownload().toInt()
        progressBar.progress = state.bytesDownloaded().toInt()

        updateProgressMessage(message)
    }

    private fun initializeViews() {
        progressBar = findViewById(R.id.progress_bar)
        progressText = findViewById(R.id.progress_text)
    }

    private fun updateProgressMessage(message: String) {
        if (progressText.visibility != View.VISIBLE &&
                progressBar.visibility != View.VISIBLE) displayProgress()
        progressText.text = message
    }

    private fun displayProgress() {
        progressText.visibility = View.VISIBLE
        progressBar.visibility = View.VISIBLE
    }

    private fun displayButtons() {
        progressText.visibility = View.GONE
        progressBar.visibility = View.GONE
    }

}

private const val TAG = "DynamicFeatures"

最佳答案

根据文档,您还需要启用SplitCompat。您有 3 个选择:

  • 选项 1:通过在 AndroidManifest.xml 中定义 SplitCompatApplication 作为默认应用程序

    <application
        ...
        android:name="com.google.android.play.core.splitcompat.SplitCompatApplication" >
    </application>
    
  • 选项 2:让您当前的应用程序类扩展 SplitCompatApplication。

    public class MyApplication extends SplitCompatApplication {
        ...
    }
    
  • 选项 3:覆盖应用程序中的 AttachBaseContext 方法。

    @Override
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        // Emulates installation of future on demand modules using SplitCompat.
        SplitCompat.install(this);
    }
    

Source

关于android - 动态功能 Activity 未加载,卡在安装中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52480833/

相关文章:

java - Sharedpreferences 在 onCreateOptionsMenu 中不起作用

Android 将 <String> 设置为数组

java - 泛型方法类型参数声明

scala - 为什么 Scala 在未指定类型参数时推断底部类型?

c# - 如何在没有 System.UnauthorizedAccessException 的情况下将文件从应用程序包复制到另一个目录?

macos - 可执行文件是否总是在 MacOS 上打开终端窗口?

android - 尝试使用 Retrofit 在没有正文的情况下进行 POST,但因 GSON 解析异常而失败

android - Firebase 规则如何在其父级被删除时防止添加或更新值

Java 泛型 - 泛型方法无法编译

java - 如何从运行在 OS X、Java 7 上的 Java .app 包中访问资源文件?