NoClassDefFoundError for code in an Java library on Android

前端 未结 24 1611
梦如初夏
梦如初夏 2020-11-22 15:19

I am experiencing an error quite often among my users. The app crashes during startup. When the MainActivity is supposed to be loaded the VM apparently cannot find the class

相关标签:
24条回答
  • 2020-11-22 16:01

    On Android Studio:

    1) Add multiDexEnabled = true in your default Config

    2) Add compile com.android.support:multidex:1.0.0 in your dependencies

    3) Application class extend MultiDexApplication instead of just Application

    0 讨论(0)
  • 2020-11-22 16:03

    Activity again added to manifest.I tried in my manifest then app worked.

     <activity
            android:name="com.xxx.xxx.MainActivity"
    
    0 讨论(0)
  • 2020-11-22 16:05

    Other idea. For example, you have class derived from "android.support.v4.app.Fragment".

    However you made a mistake and inherited it from "android.app.Fragment". Then you will have this error on the Android 2 devices.

    0 讨论(0)
  • 2020-11-22 16:06

    I'm currently using SDK 20.0.3 and none of the previous solutions worked for me.

    I was able to get things to work by

    • Right clicking the Android project
    • Then selecting Build Path -> Link Source to bring up the Link Source dialog.
    • Then I specified the 'src' folder of the Java project and gave it a name other than the default of 'src' (e.g. 'src2').
    • You can now right-click -> delete the new 'src2' folder.

    This brought this created Java class files that were compiled for the Dalvik VM instead of the Java VM when the Android project was built. This allowed the Java class files in the Android Library jar file to go thru dexing when the Android project .apk was created. Now when the app was run the Java project classes were found instead of a Java.lang.NoClassDefFoundError being thrown.

    If you want to use a jar file instead of linking the source, then you will need to create a Library Android project. (An Android Project with 'is library' checked in Properties -> Android.) You will then need to either link the source of the Java Project to the Android Library project as described above or copy the source files from the 'src' folder of the Java Project to the 'src' folder of the Android Library project. Build the Android Library project. Then you will be able copy the Android Project jar file that was created into the 'libs' folder of the Android folder because the class files in it were compiled for the Davlik VM instead of the Java VM when the Android project was built. This allows the Java class files in the Android Library jar file to go thru dexing when the Android project .apk is created. Now when the app is run the Java project classes will be found instead of a Java.lang.NoClassDefFoundError being thrown.

    0 讨论(0)
  • 2020-11-22 16:09

    Just in case it helps someone, I faced the same issue. My jar file was under the libs directory and the jar was added to the build path. Still it was throwing that exception.

    I just cleaned the project and it worked for me.

    Eclipse -> Project -> Clean -> Select the project and clean

    0 讨论(0)
  • 2020-11-22 16:09

    For me, the issue was that the referenced library was built using java 7. I solved this by rebuilding using the 1.6 JDK.

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