Android架构导航组件和Proguard导致java.lang.ClassNotFoundException

时间:2018-05-19 15:12:55

标签: android android-fragments proguard android-proguard android-jetpack

当使用新的android导航组件运行支持proguard的构建时,找不到我的片段类。我收到了这个堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapppackage.path, PID: 24334
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapppackage.path/com.myapppackage.path.MainActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
 Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
 Caused by: java.lang.RuntimeException: Exception inflating com.myapppackage.path:navigation/nav_graph line 7
    at androidx.navigation.h.a(Unknown Source:124)
    at androidx.navigation.c.a(Unknown Source:4)
    at androidx.navigation.fragment.NavHostFragment.a(Unknown Source:88)
    at android.support.v4.app.g.k(Unknown Source:15)
    at android.support.v4.app.m.a(Unknown Source:382)
    at android.support.v4.app.m.b(Unknown Source:7)
    at android.support.v4.app.m.a(Unknown Source:74)
    at android.support.v4.app.m.onCreateView(Unknown Source:216)
    at android.support.v4.app.j.a(Unknown Source:4)
    at android.support.v4.app.h.a(Unknown Source:2)
    at android.support.v4.app.e.onCreateView(Unknown Source:0)
    at android.support.v4.app.h.onCreateView(Unknown Source:0)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.j.b(Unknown Source:23)
    at android.support.v7.app.c.setContentView(Unknown Source:4)
    at com.myapppackage.path.MainActivity.onCreate(Unknown Source:6)
    at android.app.Activity.performCreate(Activity.java:6999)
    at android.app.Activity.performCreate(Activity.java:6990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.myapppackage.path.ui.start.StartFragment
    at androidx.navigation.fragment.a$a.a(Unknown Source:58)
    at androidx.navigation.fragment.a$a.a(Unknown Source:19)
    at androidx.navigation.h.a(Unknown Source:16)
    at androidx.navigation.h.a(Unknown Source:133)
    at androidx.navigation.h.a(Unknown Source:31)
    at androidx.navigation.c.a(Unknown Source:4) 
    at androidx.navigation.fragment.NavHostFragment.a(Unknown Source:88) 
    at android.support.v4.app.g.k(Unknown Source:15) 
    at android.support.v4.app.m.a(Unknown Source:382) 
    at android.support.v4.app.m.b(Unknown Source:7) 
    at android.support.v4.app.m.a(Unknown Source:74) 
    at android.support.v4.app.m.onCreateView(Unknown Source:216) 
    at android.support.v4.app.j.a(Unknown Source:4) 
    at android.support.v4.app.h.a(Unknown Source:2) 
    at android.support.v4.app.e.onCreateView(Unknown Source:0) 
    at android.support.v4.app.h.onCreateView(Unknown Source:0) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at android.support.v7.app.j.b(Unknown Source:23) 
    at android.support.v7.app.c.setContentView(Unknown Source:4) 
    at com.myapppackage.path.MainActivity.onCreate(Unknown Source:6) 
    at android.app.Activity.performCreate(Activity.java:6999) 
    at android.app.Activity.performCreate(Activity.java:6990) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
 Caused by: java.lang.ClassNotFoundException: com.myapppackage.path.ui.start.StartFragment
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:453)
E/AndroidRuntime:     at androidx.navigation.fragment.a$a.a(Unknown Source:45)
            ... 38 more
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.myapppackage.path.ui.start.StartFragment" on path: DexPathList[[zip file "/data/app/com.myapppackage.path-yqk9eOoCoeyT3xg78oYcxg==/base.apk"],nativeLibraryDirectories=[/data/app/com.myapppackage.path-yqk9eOoCoeyT3xg78oYcxg==/lib/arm64, /system/lib64, /vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
            ... 41 more

它说它找不到我nav_graph.xml文件中定义的片段类:

Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.myapppackage.path.ui.start.StartFragment

我的nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    app:startDestination="@id/start">

    <fragment
        android:id="@+id/start"
        android:name="com.myapppackage.path.ui.start.StartFragment"
        android:label="start_fragment"
        tools:layout="@layout/start_fragment" />
</navigation>

正如您所看到的那样,片段的名称是在xml中设置的,这条路径在运行proguard后似乎无效。

到目前为止,我唯一的解决方法是在我的-keepnames文件中将每个片段类路径定义为proguard-rules.pro。例如:

-keepnames class com.myapppackage.path.ui.start.StartFragment

在此之后,混淆的编译运行正常而不会崩溃。

虽然我不确定这是否正确,但我做错了吗? 我们如何在没有为proguard规则中的每个片段定义规则的情况下将新导航架构组件用于我们的模糊构建?

我理解这个问题现在可能只是一个问题,因为导航片段组件仍处于alpha状态。

0 个答案:

没有答案