启用 Proguard 后无法实例化 Fragment

问题描述

我在启用 minified 和 proguard 后遇到运行时异常。它只发生在我使用深层链接导航到的动态特征模块中的片段上。 :app 模块中的另一个片段不会受到深层链接或使用底部视图导航的影响。这是日志猫:

2021-01-30 19:41:03.026 5152-5152/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.wiryadev.gamemade,PID: 5152
    androidx.fragment.app.Fragment$e: Unable to instantiate fragment com.wiryadev.gamemade.favorite.LibraryFragment: calling Fragment constructor caused an exception
        at androidx.fragment.app.Fragment.S(:566)
        at b.l.b.f.d(:57)
        at b.l.b.l$c.a(:390)
        at b.q.y.b.h(:132)
        at b.q.y.b.i(:162)
        at b.q.y.b.b(:58)
        at androidx.navigation.NavController.t(:1057)
        at androidx.navigation.NavController.o(:942)
        at androidx.navigation.NavController.n(:875)
        at b.q.z.b.c(:97)
        at b.q.z.b$a.a(:531)
        at com.google.android.material.bottomnavigation.BottomNavigationView$a.a(:243)
        at b.b.e.j.g.h(:834)
        at b.b.e.j.i.k(:158)
        at b.b.e.j.g.O(:985)
        at d.c.a.a.f.c$a.onClick(:127)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access$3500(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at androidx.fragment.app.Fragment.S(:548)
        at b.l.b.f.d(:57) 
        at b.l.b.l$c.a(:390) 
        at b.q.y.b.h(:132) 
        at b.q.y.b.i(:162) 
        at b.q.y.b.b(:58) 
        at androidx.navigation.NavController.t(:1057) 
        at androidx.navigation.NavController.o(:942) 
        at androidx.navigation.NavController.n(:875) 
        at b.q.z.b.c(:97) 
        at b.q.z.b$a.a(:531) 
        at com.google.android.material.bottomnavigation.BottomNavigationView$a.a(:243) 
        at b.b.e.j.g.h(:834) 
        at b.b.e.j.i.k(:158) 
        at b.b.e.j.g.O(:985) 
        at d.c.a.a.f.c$a.onClick(:127) 
        at android.view.View.performClick(View.java:7125) 
        at android.view.View.performClickInternal(View.java:7102) 
        at android.view.View.access$3500(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27336) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Lambda;
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
        at androidx.fragment.app.Fragment.S(:548) 
        at b.l.b.f.d(:57) 
        at b.l.b.l$c.a(:390) 
        at b.q.y.b.h(:132) 
        at b.q.y.b.i(:162) 
        at b.q.y.b.b(:58) 
        at androidx.navigation.NavController.t(:1057) 
        at androidx.navigation.NavController.o(:942) 
        at androidx.navigation.NavController.n(:875) 
        at b.q.z.b.c(:97) 
        at b.q.z.b$a.a(:531) 
        at com.google.android.material.bottomnavigation.BottomNavigationView$a.a(:243) 
        at b.b.e.j.g.h(:834) 
        at b.b.e.j.i.k(:158) 
        at b.b.e.j.g.O(:985) 
        at d.c.a.a.f.c$a.onClick(:127) 
        at android.view.View.performClick(View.java:7125) 
        at android.view.View.performClickInternal(View.java:7102) 
        at android.view.View.access$3500(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27336) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: java.lang.classNotFoundException: Didn't find class "kotlin.jvm.internal.Lambda" on path: DexPathList[[zip file "/data/app/com.wiryadev.gamemade-0WdQ4RQ7wK7O2BstfJCacQ==/base.apk",zip file "/data/app/com.wiryadev.gamemade-0WdQ4RQ7wK7O2BstfJCacQ==/split_favorite.apk"],nativeLibraryDirectories=[/data/app/com.wiryadev.gamemade-0WdQ4RQ7wK7O2BstfJCacQ==/lib/x86,/data/app/com.wiryadev.gamemade-0WdQ4RQ7wK7O2BstfJCacQ==/base.apk!/lib/x86,/data/app/com.wiryadev.gamemade-0WdQ4RQ7wK7O2BstfJCacQ==/split_favorite.apk!/lib/x86,/system/lib,/system/product/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.classLoader.loadClass(ClassLoader.java:379)
        at java.lang.classLoader.loadClass(ClassLoader.java:312)
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
        at androidx.fragment.app.Fragment.S(:548) 
        at b.l.b.f.d(:57) 
        at b.l.b.l$c.a(:390) 
        at b.q.y.b.h(:132) 
        at b.q.y.b.i(:162) 
        at b.q.y.b.b(:58) 
        at androidx.navigation.NavController.t(:1057) 
        at androidx.navigation.NavController.o(:942) 
        at androidx.navigation.NavController.n(:875) 
        at b.q.z.b.c(:97) 
        at b.q.z.b$a.a(:531) 
        at com.google.android.material.bottomnavigation.BottomNavigationView$a.a(:243) 
        at b.b.e.j.g.h(:834) 
        at b.b.e.j.i.k(:158) 
        at b.b.e.j.g.O(:985) 
        at d.c.a.a.f.c$a.onClick(:127) 
        at android.view.View.performClick(View.java:7125) 
        at android.view.View.performClickInternal(View.java:7102) 
        at android.view.View.access$3500(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27336) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
2021-01-30 19:41:03.133 2049-2141/? E/Inputdispatcher: channel '66c185c com.wiryadev.gamemade/com.wiryadev.gamemade.ui.main.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2021-01-30 19:41:03.390 2575-2631/? E/ModuleIdSetter: exception when setting module id
    java.lang.IllegalStateException: Unable to get current module info in ModuleManager created with non-module Context
        at com.google.android.chimera.config.ModuleManager.getCurrentModule(:com.google.android.gms@204714040@20.47.14 (120700-349456378):2)
        at agdf.a(:com.google.android.gms@204714040@20.47.14 (120700-349456378):4)
        at agdi.aa(:com.google.android.gms@204714040@20.47.14 (120700-349456378):1)
        at afzt.a(UnkNown Source:3)
        at siq.a(:com.google.android.gms@204714040@20.47.14 (120700-349456378):0)
        at seu.f(:com.google.android.gms@204714040@20.47.14 (120700-349456378):1)
        at ses.e(:com.google.android.gms@204714040@20.47.14 (120700-349456378):1)
        at shm.p(:com.google.android.gms@204714040@20.47.14 (120700-349456378):2)
        at shm.v(:com.google.android.gms@204714040@20.47.14 (120700-349456378):3)
        at shm.e(:com.google.android.gms@204714040@20.47.14 (120700-349456378):2)
        at shq.handleMessage(:com.google.android.gms@204714040@20.47.14 (120700-349456378):70)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at afso.iC(:com.google.android.gms@204714040@20.47.14 (120700-349456378):0)
        at afso.dispatchMessage(:com.google.android.gms@204714040@20.47.14 (120700-349456378):11)
        at android.os.Looper.loop(Looper.java:214)
        at android.os.HandlerThread.run(HandlerThread.java:67)

我尝试添加here 获得的这一行,但不起作用。

-keepnames class androidx.navigation.fragment.NavHostFragment

有人经历过吗?我该怎么办?

更新:

shared_depencies.gradle 跨模块实现:

dependencies {
    implementation "androidx.appcompat:appcompat:$appcompat_version"
    implementation "androidx.core:core-ktx:$core_ktx_version"
    implementation "androidx.constraintlayout:constraintlayout:$constraint_version"
    implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"

    testImplementation "junit:junit:$junit_version"
    androidTestImplementation "androidx.test.ext:junit:$androidx_junit_version"
    androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_version"

    implementation "androidx.multidex:multidex:$multidex_version"
    debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakcanary_version"

    // UI
    implementation "androidx.recyclerview:recyclerview:$recyclerview_version"
    implementation "com.google.android.material:material:$material_version"
    implementation "io.coil-kt:coil:$coil_version"
    implementation "com.airbnb.android:lottie:$lottie_version"
    implementation "com.facebook.shimmer:shimmer:$shimmer_version"

    // Hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-compiler:$hilt_version"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_jetpack_version"
}

共享依赖被应用:

apply from: '../shared_dependencies.gradle'

核心模块依赖:

dependencies {
    //noinspection DifferentStdlibGradLeversion
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    // Room
    api "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    androidTestImplementation "androidx.room:room-testing:$room_version"

    // Retrofit
    api "com.squareup.retrofit2:retrofit:$retrofit_version"
    api "com.squareup.retrofit2:converter-gson:$retrofit_version"
    api "com.squareup.okhttp3:logging-interceptor:$logging_interceptor_version"

    // Coroutine
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
    implementation "androidx.room:room-ktx:$room_version"

    // Lifecycle
    api "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    api "androidx.activity:activity-ktx:$activity_ktx_version"
    api "androidx.fragment:fragment-ktx:$fragment_ktx_version"

    // Security
    implementation "net.zetetic:android-database-sqlcipher:$sqlcipher_version"
    implementation "androidx.sqlite:sqlite-ktx:$sqlite_version"
}

应用模块依赖

dependencies {
    implementation project(":core")
    //noinspection DifferentStdlibGradLeversion
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

动态特性模块依赖:

dependencies {
    implementation project(":app")
    implementation project(":core")
    //noinspection DifferentStdlibGradLeversion
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

附加信息:

  • 如果 :app 模块也被混淆,就会发生这种情况
  • 如果只有 :core 模块被混淆了,没关系
  • 动态功能模块根本没有被混淆

解决方法

结果我必须更改我在动态功能模块片段中传递给 Dagger 的上下文。因此,在我的情况下,使用 requireContext() 代替 requireActivity().appicationContext。代码应该是这样的:

DaggerFavoriteComponent.builder()
    .context(requireActivity())
    .appDependencies(
        EntryPointAccessors.fromApplication(
            requireActivity().applicationContext,FavoriteModuleDependencies::class.java
        )
    )
    .build()
    .inject(this)