Dx bad class file magic (cafebabe) or version (0033.0000) with ADK14

前端 未结 18 2006
攒了一身酷
攒了一身酷 2020-11-27 05:00

Since moving to ADK14, I have been unable to build new apks for release on my Windows 7 system.

Building fails with \"conversion to dalvik format failed with error 1

18条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 05:54

    Dx bad class file magic (cafebabe) or version (0033.0000)

    This is a check from the build tools about

    1. Is this a java class file. (Java class files have 'cafebabe' at the beginning)
    2. Is it an unsupported version. This message says 0033.0000 (Java SE 1.7) is unsupported

    So this particular message says

    Reading this class file (it has 'cafebabe' magic), I got confused by version of JVM 1.7

    The java language and build tools are maintained by Oracle, and the dex/dalvik/Android Runtime (ART) tools are maintained by google/android. So when oracle generates a new java, there is a period of time when the java generated by the latest sdk, is incompatible with the java supported by the build tools.

    wikipedia : Java class file has a table showing the versions of java and their version number

    +------------+-----------+
    |Java SE 1.9 | 0035.0000 |
    |Java SE 1.8 | 0034.0000 |
    |Java SE 1.7 | 0033.0000 |
    |Java SE 1.6 | 0032.0000 |
    +------------+-----------+
    

    So in this case, the "bad version" - is 1.7. The latest (May 2016) tool chain supports 1.6 and 1.7, but not 1.8.

    How to resolve

    There are 2 mechanisms which can be used to resolve this issue.

    1. Change the compatibility
    2. Change the SDK

    Change the compatibility

    When compiling on java you can tell the compiler to target an earlier SDK. With Android studio at the moment (2.0), that is done by changing.

    File/Project Structure
    

    Click on each module, and set Source Compatibility to the required java version.

    In raw gradle, that means inserting into build.gradle something like :-

    android {
       ...
       compileOptions {
          sourceCompatibility JavaVersion.VERSION_1_6
          targetCompatibility JavaVersion.VERSION_1_6
        }
    

    For a java project, the build.gradle needs

    sourceCompatibility= 1.6
    targetCompatibility= 1.6
    

    Change the SDK

    The sdk for earlier java builds can be downloaded, and you can download and install the earlier SDK. This is a limited time fix. Oracle are continuously improving java, removing bugs, and the older SDKs are not being updated.

    The SDK is updated by changing the SDK from the left hand side of project structure

提交回复
热议问题