NoClassDefFoundError for code in an Java library on Android

前端 未结 24 1648
梦如初夏
梦如初夏 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 15:48

    Try go to

    • Right click project
    • Java build path
    • Source
    • right side -> Add folder button
    • selected libs folder and OK

    I hope it helps.

    0 讨论(0)
  • 2020-11-22 15:49

    I encountered this NoClassDefFoundError when I used java.nio.charset.StandardCharsets.

    The reason is that StandardCharsets has not existed until JRE 1.7. If I make the java compile version set to 1.7, Eclipse complained that "Android requires compiler compliance level 5.0 or 6.0". So, I fixed it by right-click the project name->Android Tools->Fix Project Properties. It is compiled with JRE1.6.

    However, because StandardCharsets has not existed until 1.7. It reported NoClassDefFoundError when I ran it.

    I has not come to realize this until after trying a lot of other methods including reinstalling JDK. The real reason is clearly told by the meaning of NoClassDefFoundError: The class cannot be found at run time although it passed compilation.

    General conclusion is that as long as Android does not work with JRE 1.7, if you use any new feature provided since 1.7, you will encounter this error.

    My solution is that I copied those source code into my code!

    0 讨论(0)
  • 2020-11-22 15:50

    This error is also generated when you make an app that uses the Google API (such as Maps) but run it on a device that targets the Android API.

    0 讨论(0)
  • 2020-11-22 15:51

    I had the same issue, I did the following to fix the problem.

    1. Go to "Properties" of the project.
    2. Select "Java Build Path"
    3. Select "Order and Export" Tab
    4. You should see the selected project's "src" and "gen" paths and dependencies here.
    5. The order how they listed were first "src" and then "gen" path
    6. I switch them, so that "gen" folder is build before the "src"

    gen - automated code in project (from dependencies and references)
    src - source code in project

    There was no need to restart the Eclipse. It just started working.

    Honestly I have never tried "Android Tools > Fix Project Properties", sometimes it might be doing the same thing. I do not know, I just did above after seen the error message, thinking something is wrong with the build paths.


    Edit


    Later on it was not sufficient, I was getting the error again. Then I "checked" all the dependencies listed in that view. Now it works again. So far so good. I will keep this updated if it fails again.

    FYI: in my last attempt, I tried "Android Tools > Fix Project Properties", but it didn't work out for me.

    0 讨论(0)
  • 2020-11-22 15:51

    I am guessing that you don't specify javac's target when creating the common library, so javac automatically uses the latest available target, which is likely 1.7 (Java7) or 1.8 (Java8).

    It has already been stated that

    Android requires compiler compliance level 5.0 or 6.0

    dx of Android's build tools < 19.0.0 isn't able to convert Java7 (or higher) bytecode to Dalvik bytecode.

    So either use a build tools version >= 19.0.0 or use javac with -target 6, by modifying for example your ant build.xml like this:

    <javac
    srcdir="src/"
    destdir="build/"
    target="6"
    />
    
    0 讨论(0)
  • 2020-11-22 15:53

    Try going to Project -> Properties -> Java Build Path -> Order & Export and ensure Android Private Libraries are checked for your project and for all other library projects you are using. i got the solution by following below link NoClassDefFoundError Android Project?

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