問題
debug 模式運行沒有問題,release模式打包后,頁面閃退,報錯如下:
java.lang.LinkageError: Method void j.r.a.i.f.c.clear() overrides final method in class Landroidx/lifecycle/ViewModel; (declaration of 'j.r.a.i.f.c' appears in base.apk!classes3.dex)
一開始懷疑是混淆的問題,但是沒有發現問題,后來通過 注釋-排查 的方法,發現是命名的問題:
package androidx.lifecycle;
import androidx.annotation.MainThread;
import androidx.annotation.Nullable;
import java.io.Closeable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public abstract class ViewModel {
// Can't use ConcurrentHashMap, because it can lose values on old apis (see b/37042460)
@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();
}
}
lifecycle 的 ViewModel 中,有clear方法,我在業務代碼中也起相同名字方法,它還以為我是要繼承...overrides final method...
解決
原代碼:
class TaskNoticeViewModel : GraphqlViewModel() {
// 清空消息
fun clear() {
cleanCall?.cancel()
cleanCall = CleanTaskNoticeMutation.builder()
.build()
.requestOnlineAuth({
logError("清空消息成功 ${it?.CleanMessage()}")
},{
logError("清空消息失敗")
false
})
}
}
更改方法名即可
class TaskNoticeViewModel : GraphqlViewModel() {
// 清空消息
fun clearMessage() {
cleanCall?.cancel()
cleanCall = CleanTaskNoticeMutation.builder()
.build()
.requestOnlineAuth({
logError("清空消息成功 ${it?.CleanMessage()}")
},{
logError("清空消息失敗")
false
})
}
}