仅当minifyEnabled和LifeCycle v 2.1.0为真时,创建ViewModel时应用程序崩溃

3
我的应用程序在启动时崩溃,使用lazy{}创建视图模型时出现LinkageError。只有在以下情况下才会崩溃:
  1. build.gradle中设置了minifyEnabled=true,并且
  2. 我使用生命周期组件的版本2.1.0。如果使用lifecycle-2.0.0minifyEnabled一起使用,则可以正常工作。
    def lifecycle_version = '2.1.0'
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"

此外,崩溃只会在一个视图模型中发生。在这个视图模型之前被触碰的同一活动中的其他视图模型并不会导致应用程序崩溃。


崩溃发生在这里的第二行:

    private val searchStackViewModel by lazy {
        ViewModelProviders.of(this)[SearchStateViewModel::class.java]
    }

SearchStateViewModel 是:

class SearchStateViewModel : ViewModel() {

    // Live data that initialises to empty stack with SearchStack.init
    private val privateStack = MutableLiveData<SearchStack>().apply {
        value = SearchStack()
    }

    // Observable view of search stack so it can't be directly modified
    internal val stateStack : LiveData<SearchStack> = privateStack

    /**
     * Add state to stack
     */
    fun add(searchState: SearchState) {
        val current = privateStack.value ?: SearchStack()
        current.add(searchState)
        privateStack.value = current
    }

    /**
     * Clear stack
     */
    fun clear() {
        val current = privateStack.value ?: SearchStack()
        current.clear()
        privateStack.value = current
    }

    /**
     * Clear stack, then add current state as the only state
     */
    fun clearThenAdd(searchState: SearchState) {
        val current = privateStack.value ?: SearchStack()
        current.clear()
        current.add(searchState)
        privateStack.value = current
    }

    /**
     * Get currentState search state, without changing the stack
     */
    fun currentState(): SearchState {
        return privateStack.value?.last() ?: SearchState()
    }

    /**
     * Return currentState search state, and remove it from the stack
     */
    fun pop(): SearchState {
        val current = privateStack.value ?: SearchStack()
        val poppedState = current.pop()
        privateStack.value = current
        return poppedState
    }
}

SearchStack 只是一个 ArrayList:

class SearchStack : ArrayList<SearchState>() {

    init {
        add(SearchState())
    }


    fun pop(): SearchState = if (lastIndex > 0) removeAt(lastIndex) else last()


    override fun clear() {
        super.clear()
        add(SearchState())
    }


    override fun add(element: SearchState): Boolean {
        if (element == lastOrNull())
            return false
        return super.add(element)
    }
}

SearchState 是一个数据类:

@Parcelize
data class SearchState(
        val searchTerm: String = "",
        val isComplete: Boolean? = null,
        val dueOnly: Boolean = false,
        val aliveOnly: Boolean = true,
        val priority: Char? = null,
        val project: String? = null,
        val priorityMatchType: PriorityMatchType? = null,
        val name: String = "",
        val hideThresholdTasks: Boolean = true,
        val sortOrder: Int = -1,
        val sortOrderString: String? = null
                      ) : Parcelable {

    enum class PriorityMatchType {
        GREATOR,
        LESSOR,
        EXACT
    }

    enum class TaskState {
        DUE,
        PENDING,
        COMPLETED,
        ALL
    }

堆栈跟踪:

FATAL EXCEPTION: main
    Process: net.c306.ttsuper, PID: 7380
    java.lang.LinkageError: i.a.a.o.b
1 >>    at net.c306.ttsuper.view.ui.MainActivity$v.b(SourceFile:182)
        at net.c306.ttsuper.view.ui.MainActivity$v.b(SourceFile:159)
        at g.j.a(SourceFile:74)
        at net.c306.ttsuper.view.ui.MainActivity.F(SourceFile)
2 >>    at net.c306.ttsuper.view.ui.MainActivity.c(SourceFile:1839)
        at net.c306.ttsuper.view.ui.MainActivity.a(SourceFile:1993)
        at net.c306.ttsuper.view.ui.MainActivity.a(SourceFile:1967)
        at net.c306.ttsuper.view.ui.MainActivity.onCreate(SourceFile:386)
        at android.app.Activity.performCreate(Activity.java:6251)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

标有(1)的行是ViewModel的惰性创建,在这里发生了崩溃。
标有(2)的行是ViewModel的第一次访问,它启动了惰性创建:

val lastState = searchStackViewModel.currentState()

发布崩溃堆栈跟踪 - Marcin Orlowski
@MarcinOrlowski 已添加堆栈跟踪。 - Adi B
有人能解释一下,为什么会发生这种情况吗?因为我们在这里没有使用覆盖关键字吗?此外,对于我来说,这只发生在启用了Proguard的发布版本中。 - Minion
1个回答

8

我可能已经解决了这个问题。原来在 lifecycle-2.0.0 中,ViewModel 类有一个公共方法 clear()

    @MainThread
    final void clear() {
        mCleared = true;
        // Since clear() is final, this method is still called on mock objects
        // and in those cases, mBagOfTags is null. It'll always be empty though
        // because setTagIfAbsent and getTag are not final so we can skip
        // clearing it
        if (mBagOfTags != null) {
            synchronized (mBagOfTags) {
                for (Object value : mBagOfTags.values()) {
                    // see comment for the similar call in setTagIfAbsent
                    closeWithRuntimeException(value);
                }
            }
        }
        onCleared();
    }

我的SearchStackViewModel还有一个完全无关的clear()方法。

    /**
     * Clear stack
     */
    fun clear() {
        val current = privateStack.value ?: SearchStack()
        current.clear()
        privateStack.value = current
    }

minifyEnabled开启时,出现了两者之间的冲突,因此导致了链接错误。我重命名了我的方法,崩溃问题得到了解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接