【问题标题】:Error when using a jar in my project在我的项目中使用 jar 时出错
【发布时间】:2016-04-06 08:13:42
【问题描述】:

我使用 Java 1.8 创建我的 jar。

在Java项目中可以使用,但是在Android项目中,出现如下错误:

Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:1 error; aborting
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\Android-tool\JAVA\jdk1.8.0_77\bin\java.exe'' finished with non-zero exit value 1
Information:BUILD FAILED
Information:Total time: 6.468 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console

【问题讨论】:

标签: java android


【解决方案1】:

将这些东西添加到模块的build.gradle:

android {
    buildToolsVersion "24rc4"

    defaultConfig {
        jackOptions {
            enabled true
        }
    }
    dexOptions {
        incremental true
    }

    compileOptions {
        sourceCompatibility = org.gradle.api.JavaVersion.VERSION_1_8
        targetCompatibility = org.gradle.api.JavaVersion.VERSION_1_8
    }
}

【讨论】:

    【解决方案2】:

    陷入同样的​​境地。这就是我所做的。 1. 从此链接下载Jdk 1.7 Oracle Java 1.7

    1. 现在,下载并安装后,转到系统属性(我的电脑),然后转到高级系统属性。
    2. 转到环境变量并将路径 home 和 java home 更改为新安装的 jdk 1.7。

    现在应该可以正常工作了。如果没有,请按照以下步骤操作:

    1. 切换到 Android Studio,然后右键单击您的模块。转到模块设置/项目结构。
    2. 现在转到模块部分并指定源和目标与 1.7 的兼容性

    对我来说就像一个魅力。 附言。 - 抱歉这么晚才回复

    【讨论】:

      【解决方案3】:

      如果是多应用项目中的 Java 库,请使用库的 build.gardle 文件中提到的以下代码。

      apply plugin: 'java'
      sourceCompatibility = JavaVersion.VERSION_1_7
      targetCompatibility = JavaVersion.VERSION_1_7
      compileJava.options.bootClasspath = "D:/android/sdk/platforms/android-23/android.jar"
      ...
      dependencies {
          compile fileTree(dir: 'libs', include: ['*.jar'])
      ...
      }
      

      您需要将compileJava.options.bootClasspath 更改为android.jar 文件的本地路径。这适用于 Win10 上的 AS 2.2 中的我。 更多参考请访问here

      【讨论】:

        【解决方案4】:

        如果错误中自我解释:

        这是由已使用编译的库依赖项引起的 Java 8 或更高版本。如果您在 库子模块添加 targetCompatibility = '1.7' sourceCompatibility = '1.7' 到该子模块的 build.gradle 文件。

        【讨论】:

        • 意思是我不能用java 1.8?
        • 我在使用jdk1.8.0_45没有任何问题
        • 再次阅读错误'如果您在库子模块中使用'java' gradle插件添加......'
        • 我遇到了同样的问题,但我真的不知道应该在我的 grdale 模块中的哪个位置直接插入带有 sourceCompability 和 tartgetCompability 的行!
        猜你喜欢
        • 2022-09-23
        • 2014-01-12
        • 1970-01-01
        • 2016-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-15
        • 1970-01-01
        相关资源
        最近更新 更多