4

我想在我的应用程序中实现新的应用程序内更新库,但我注意到它在重新创建/旋转时会触发我的活动中的内存泄漏。

这是我从 LeakCanary 获得的唯一细节:

泄漏金丝雀跟踪

显然,如果我从 In-App Update 库中删除代码,尤其是 addOnSuccessListener ,我将一无所有:

appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
        && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)){
            updateInfo.value = appUpdateInfo
            updateAvailable.value = true
        }else{
            updateInfo.value = null
            updateAvailable.value = false
        }
    }

根据这篇文章,我首先使用了一些 LiveData,但问题是一样的,所以我使用了一个完整的类来处理回调,使用 LiveData:

我的服务类:

class AppUpdateService {

    val updateAvailable: MutableLiveData<Boolean> by lazy { MutableLiveData<Boolean>() }
    val updateDownloaded: MutableLiveData<Boolean> by lazy { MutableLiveData<Boolean>() }
    val updateInfo: MutableLiveData<AppUpdateInfo> by lazy { MutableLiveData<AppUpdateInfo>() }

    fun checkForUpdate(appUpdateManager: AppUpdateManager){
        appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)){
                updateInfo.value = appUpdateInfo
                updateAvailable.value = true
            }else{
                updateInfo.value = null
                updateAvailable.value = false
            }
        }
    }

    fun checkUpdateOnResume(appUpdateManager: AppUpdateManager){
        appUpdateManager.appUpdateInfo.addOnSuccessListener {
            updateDownloaded.value = (it.installStatus() == InstallStatus.DOWNLOADED)
        }
    }
}

我的活动简化:

class MainActivity : BaseActivity(), InstallStateUpdatedListener {

    override fun contentViewID(): Int { return R.layout.activity_main }

    private val UPDATE_REQUEST_CODE = 8000

    private lateinit var appUpdateManager : AppUpdateManager

    private val appUpdateService = AppUpdateService()

    override fun onStateUpdate(state: InstallState?) {
        if(state?.installStatus() == InstallStatus.DOWNLOADED){ notifyUser() }
    }

    // Called in the onCreate()
    override fun setupView(){
        appUpdateManager = AppUpdateManagerFactory.create(this)
        appUpdateManager.registerListener(this)
        setupAppUpdateServiceObservers()
        // Check for Update
        appUpdateService.checkForUpdate(appUpdateManager)
    }

    private fun setupAppUpdateServiceObservers(){
        appUpdateService.updateAvailable.observe(this, Observer {
            if (it)
                requestUpdate(appUpdateService.updateInfo.value)
        })

        appUpdateService.updateDownloaded.observe(this, Observer {
            if (it)
                notifyUser()
        })
    }

    private fun requestUpdate(appUpdateInfo: AppUpdateInfo?){
        appUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.FLEXIBLE, this, UPDATE_REQUEST_CODE)
    }

    private fun notifyUser(){
        showSnackbar(getString(R.string.updated_downloaded), getString(R.string.restart)) {
            appUpdateManager.completeUpdate()
            appUpdateManager.unregisterListener(this)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == UPDATE_REQUEST_CODE) {
            if (resultCode != RESULT_OK) {
                Timber.d("Update flow failed! Result code: $resultCode")
            }
        }
    }

    override fun onDestroy() {
        appUpdateManager.unregisterListener(this)
        super.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        appUpdateService.checkUpdateOnResume(appUpdateManager)
    }

}

我真的不明白如何避免内存泄漏,因为 appUpdateManager 必须使用活动的上下文创建,而且它看起来是导致回调内存泄漏的原因。

有人已经实施它而没有这个问题吗?

4

3 回答 3

1

使用对上下文的弱引用可能会解决您的内存泄漏问题。在你的活动中写下:

WeakReference<Context> contextWeakReference = new WeakReference<Context>(this);

Context context = contextWeakReference.get();
if (context != null) {
   // Register using context here
}

有很多关于 WeakReference、Garbage Collection 和 Memory Leaks 的好文章可以阅读更多关于该主题的内容。

此外,不保证会调用 onDestroy()。当你启动另一个 Activity 时,会调用 onPause() 和 onStop() 方法而不是 onDestroy()。

当您点击返回按钮或调用 finish() 方法时,onDestroy() 会调用。因此,在 onPause() 或 onStop() 中取消注册 Listener。如果在 onDestroy() 方法中取消注册,可能会导致内存泄漏。

另一个想法是,由于 AppUpdateService 类不是 ViewModel 的子类,因此它不具备生命周期意识。我不确定,但是,您可能需要在活动的 onstop/onDestroy 中删除观察者并将它们添加到 onResume 中。(观察者对 LifecycleOwner 有很强的引用,这里是 activiy)为此,您需要定义观察者以便以后能够删除它们。就像是:

MutableLiveData<Boolean> someData = new MutableLiveData<>;

然后在 onResume 中:

someData = appUpdateService.updateAvailable;
someData.observe()

在 onStop 中:

someData.removeObservers()

这只是一个猜测,但是,我希望它会有所帮助。

于 2019-10-17T20:51:08.923 回答
1

感谢@Sina Farahzadi,我搜索并尝试了很多东西,发现问题出appUpdateManager.appUdateInfo在 Task 对象的调用上。

我发现解决内存泄漏的方法是使用 applicationContext 而不是活动的上下文。我不确定这是不是最好的解决方案,但这是我目前找到的解决方案。我已经在我的服务类中导出了所有内容,所以这是我的代码:

AppUpdateService.kt:

class AppUpdateService : InstallStateUpdatedListener {

    val updateAvailable: MutableLiveData<Boolean> by lazy { MutableLiveData<Boolean>() }
    val updateDownloaded: MutableLiveData<Boolean> by lazy { MutableLiveData<Boolean>() }
    val notifyUser: MutableLiveData<Boolean> by lazy { MutableLiveData<Boolean>() }
    val updateInfo: MutableLiveData<AppUpdateInfo> by lazy { MutableLiveData<AppUpdateInfo>() }

    private var appUpdateManager : AppUpdateManager? = null
    private var appUpdateInfoTask: Task<AppUpdateInfo>? = null

    override fun onStateUpdate(state: InstallState?) {
        notifyUser.value =  (state?.installStatus() == InstallStatus.DOWNLOADED)
    }

    fun setupAppUpdateManager(context: Context){
        appUpdateManager = AppUpdateManagerFactory.create(context)
        appUpdateManager?.registerListener(this)
        checkForUpdate()
    }

    fun onStopCalled(){
        appUpdateManager?.unregisterListener(this)
        appUpdateInfoTask = null
        appUpdateManager = null
    }

    fun checkForUpdate(){
        appUpdateInfoTask = appUpdateManager?.appUpdateInfo
        appUpdateInfoTask?.addOnSuccessListener { appUpdateInfo ->
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)){
                updateInfo.value = appUpdateInfo
                updateAvailable.value = true
            }else{
                updateInfo.value = null
                updateAvailable.value = false
            }
        }
    }

    fun startUpdate(activity: Activity, code: Int){
        appUpdateManager?.startUpdateFlowForResult(updateInfo.value, AppUpdateType.FLEXIBLE, activity, code)
    }

    fun updateComplete(){
        appUpdateManager?.completeUpdate()
        appUpdateManager?.unregisterListener(this)
    }

    fun checkUpdateOnResume(){
        appUpdateManager?.appUpdateInfo?.addOnSuccessListener {
            updateDownloaded.value = (it.installStatus() == InstallStatus.DOWNLOADED)
        }
    }

}

MainActivity 简化:

class MainActivity : BaseActivity(){

    override fun contentViewID(): Int { return R.layout.activity_main }

    private val UPDATE_REQUEST_CODE = 8000

    private var appUpdateService: AppUpdateService? = AppUpdateService()

    /**
     * Setup the view of the activity (navigation and menus)
     */
    override fun setupView(){
        val contextWeakReference = WeakReference<Context>(applicationContext)
        contextWeakReference.get()?.let {weakContext ->
            appUpdateService?.setupAppUpdateManager(weakContext)
        }
    }

    private fun setupAppUpdateServiceObservers(){
        appUpdateService?.updateAvailable?.observe(this, Observer {
            if (it)
                requestUpdate()
        })

        appUpdateService?.updateDownloaded?.observe(this, Observer {
            if (it)
                notifyUser()
        })

        appUpdateService?.notifyUser?.observe(this, Observer {
            if (it)
                notifyUser()
        })
    }

    private fun removeAppUpdateServiceObservers(){
        appUpdateService?.updateAvailable?.removeObservers(this)
        appUpdateService?.updateDownloaded?.removeObservers(this)
        appUpdateService?.notifyUser?.removeObservers(this)
    }

    private fun requestUpdate(){
        appUpdateService?.startUpdate(this, UPDATE_REQUEST_CODE)
    }

    private fun notifyUser(){
        showSnackbar(getString(R.string.updated_downloaded), getString(R.string.restart)) {
            appUpdateService?.updateComplete()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == UPDATE_REQUEST_CODE) {
            if (resultCode != RESULT_OK) {
                Timber.d("Update flow failed! Result code: $resultCode")
            }
        }
    }

    override fun onStop() {
        appUpdateService?.onStopCalled()
        removeAppUpdateServiceObservers()
        appUpdateService = null
        super.onStop()
    }

    override fun onResume() {
        super.onResume()
        setupAppUpdateServiceObservers()
        appUpdateService?.checkUpdateOnResume()
    }

}

现在,我会保持这种状态,并继续寻找另一种方式来做到这一点。让我知道是否有人有更好的方法来做到这一点。

于 2019-10-19T15:52:06.477 回答
0

使用这个助手类:

class GoogleUpdater(activity: FragmentActivity) : LifecycleObserver {
    private val appUpdateManager = AppUpdateManagerFactory.create(activity)
    private var installStateUpdatedListener: InstallStateUpdatedListener? = null
    private var wra = WeakReference(activity)
    private val activity get() = wra.get()

    init {
        activity.lifecycle.addObserver(this)
    }

    fun checkUpdate() {
        fun showCompleteUpdateDialog() {
            activity?.let { activity ->
                if (!activity.isFinishing)
                    AlertDialog.Builder(activity)
                            .setTitle(R.string.notification)
                            .setMessage(R.string.restart_to_complete_update)
                            .setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_notification)
                                    ?.apply {
                                        mutate()
                                        alpha = 127
                                    })
                            .setPositiveButton(R.string.yes) { _: DialogInterface?, _: Int -> appUpdateManager.completeUpdate() }
                            .setNegativeButton(R.string.no, null)
                            .create()
                            .apply { setCanceledOnTouchOutside(false) }
                            .show()
            }
        }

        installStateUpdatedListener = object : InstallStateUpdatedListener {
            override fun onStateUpdate(state: InstallState) {
                if (state.installStatus() == InstallStatus.DOWNLOADED)
                    showCompleteUpdateDialog()
                else if (state.installStatus() == InstallStatus.INSTALLED)
                    appUpdateManager.unregisterListener(this)
            }
        }.also { appUpdateManager.registerListener(it) }

        appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateInfo ->
            val clientVersionStalenessDays = appUpdateInfo.clientVersionStalenessDays()
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)
                    && clientVersionStalenessDays != null
                    && clientVersionStalenessDays >= DAYS_FOR_FLEXIBLE_UPDATE) {
                try {
                    activity?.let { activity ->
                        if (!activity.isFinishing)
                            appUpdateManager.startUpdateFlowForResult(
                                    appUpdateInfo,
                                    AppUpdateType.FLEXIBLE,
                                    activity,
                                    REQUEST_CODE_APP_UPDATE)
                    }
                } catch (e: SendIntentException) {
                    FirebaseCrashlytics.getInstance().recordException(e)
                }
            } else if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED)
                showCompleteUpdateDialog()
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    private fun onStop() {
        installStateUpdatedListener?.let { appUpdateManager.unregisterListener(it) }
    }

    companion object {
        const val REQUEST_CODE_APP_UPDATE = 11
        const val DAYS_FOR_FLEXIBLE_UPDATE = 1
    }
}

在活动中:

GoogleUpdater(this).apply { checkUpdate() }
于 2021-02-08T21:51:09.933 回答