Android: NoClassDefFoundError only on release build

后端 未结 2 1532
广开言路
广开言路 2021-01-25 19:11

I\'m on Android Studio and trying to build my project on release mode.
Everything is OK in debug mode however below errors occur in release mode...



        
相关标签:
2条回答
  • 2021-01-25 19:40

    In my case it was working for all database tables, except one which was specifically passed as argument inside UI Navigation So, if you are using UI Navigation with serializable argument type as one of the roomdb table, make sure you are not mentioning the class name of it in the XML file (as that will be changed in release DEX/minification process.)

    XML file: Navigation.xml containing

    <navigation>
            <fragment> 
                <argument android:name="questionObj" app:argType="roomdb.db.question" /> // removed for Fixing
                <action
                android:id="@+id/action_q1_to_q2">
            </fragment> 
        </navigation>
    

    <argument ..... /> line was removed as a fix.

    bundle!!.getSerializable("questionObj") as Question?
    

    Should be used for passing

    Errors were helpful & accurate

    W/System.err: Caused by: java.lang.RuntimeException: Exception inflating navigation/navigation line 83
    W/System.err: Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: database.db.Question
    W/System.err: Caused by: java.lang.ClassNotFoundException: database.db.Question
    W/System.err: Caused by: java.lang.ClassNotFoundException: Didn't find class "database.db.Question" on path: DexPathList[[zip file "/data/app/appname-zu2-cBYoT7e345HeXbjQsgtpA==/base.apk"],nativeLibraryDirectories=[/data/app/appname-zu2-cBYoT7e345HeXbjQsgtpA==/lib/arm64, /system/lib64, /product/lib64]]
    I/PackageManager.DexOptimizer: Running dexopt (dexoptNeeded=-3) on: /data/app/appname-zu2-cBYoT7e345HeXbjQsgtpA==/base.apk pkg=appname isa=arm64 dexoptFlags=boot_complete,profile_guided,enable_hidden_api_checks targetFilter=speed-prof
    
    0 讨论(0)
  • 2021-01-25 19:55

    The problem is basically the following:

    buildTypes {
            release {
                **minifyEnabled false**
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    

    If you set minifyEnabled to false, it should fix your problem. If you want to use the minify feature then you can add progaurd rules to your build.gradle. For instance for the Butterknife library you have to add the following:

    -keep class butterknife.** { *; }
    -dontwarn butterknife.internal.**
    -keep class **$$ViewBinder { *; }
    
    -keepclasseswithmembernames class * {
        @butterknife.* <fields>;
    }
    
    -keepclasseswithmembernames class * {
        @butterknife.* <methods>;
    }
    

    Glad I could help

    0 讨论(0)
提交回复
热议问题