为Android项目创建自己的jar库时出错该怎么办?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (67)

当我buildig并运行这个库项目时,它显示我下面的错误:

错误:任务':app:preDexDebug'的执行失败。com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException:进程'命令'C:\ Program Files \ Java \ jdk1.8.0_60 \ bin \ java.exe'零退出值1

我只是添加一个java库模块并在Multiplication那里添加一个非常简单的类(请参阅下面的代码)。然后在我的项目中添加该库模块作为库:

public class Multiplication {

    public double multiply(int num1, int num2){
        return num1*num2;
    }
}

活动代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    edt1 = (EditText)findViewById(R.id.et1);
    edt2 = (EditText)findViewById(R.id.et2);
    btn  = (Button)findViewById(R.id.btn);
    tv = (TextView)findViewById(R.id.tvRes);

    multiplication = new Multiplication();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            double res = multiplication.multiply(Integer.parseInt(edt1.getText().toString()),Integer.parseInt(edt2.getText().toString()));

            tv.setText(Double.toString(res));
        }
    });

的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 11
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile files('D:/Work_New/MyApplication/libs/mylibjar.jar')
}

完整的错误日志:

Information:Gradle tasks [:app:assembleDebug]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:mylibjar:compileJava UP-TO-DATE
:mylibjar:processResources UP-TO-DATE
:mylibjar:classes UP-TO-DATE
:mylibjar:jar UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72221Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42221Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJava UP-TO-DATE
:app:preDexDebug
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
    at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
    at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
    at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
    at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
    at com.android.dx.command.dexer.Main.processClass(Main.java:704)
    at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673)
    at com.android.dx.command.dexer.Main.access$300(Main.java:83)
    at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:602)
    at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
    at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
    at com.android.dx.command.dexer.Main.processOne(Main.java:632)
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:510)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:280)
    at com.android.dx.command.dexer.Main.run(Main.java:246)
    at com.android.dx.command.dexer.Main.main(Main.java:215)
    at com.android.dx.command.Main.main(Main.java:106)
...while parsing com/MultiplicationTest/Multiplication.class
1 error; aborting
Error:Execution failed for task ':app:preDexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_60\bin\java.exe'' finished with non-zero exit value 1
提问于
用户回答回答于

更新2

根据日志,您可以使用Java 1.8构建* .jar。Android支持的最大Java版本是1.7。所以你必须将源代码兼容性设置为1.7并重建jar文件。

如果您不是通过gradle(或Android Studio)构建此jar文件,则需要更改JDK版本(文件 - >项目结构 - > SDK位置 - > JDK位置)。

对于gradle模块,请参阅“更新1”

更新1

对于你的Java模块(你想从中获取jar)放置这些行:

apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7

尝试将下一个代码放到Android模块的gradle文件中:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}
用户回答回答于

"non-zero exit value 1"错误原因可能不同。

1.删​​除库,然后重建项目以检查它是否工作。

2.如果工作正常,请compile filetree从中删除选项dependencies并重新构建:

dependencies {
compile 'com.android.support:appcompat-v7:22.2.1'
compile files('D:/Work_New/MyApplication/libs/mylibjar.jar')
}
  1. 如果这个问题仍然存在,那么创建一个简单的项目并首先将jar库导入到该项目中。接下来,逐个添加这些其他依赖项以找出问题。如果问题不再发生,请将所有java文件复制到此新项目中。接下来"image"一个接一个地复制文件。通常可绘制的图像文件导致这个问题"non-zero exit value 1"

编辑:在android{你的依赖中添加这个内容:

dexOptions {
    preDexLibraries = true
}

扫码关注云+社区

领取腾讯云代金券