为安卓项目创建自己的jar库时出错
目前,我正在研究如何在Android Studio中创建jar库的基础知识。
我遵循了如何从Android Studio项目中制作.jar以及如何从Android Studio教程中创建jar文件。并完成了第一个教程中描述的每个步骤。
但是,当我构建并运行此库项目时,它会显示以下错误
错误:任务“: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'' 以非零退出值 1 完成
我在SO中研究了这个问题,但这些问题对我根本没有帮助。这对我来说不是内存问题,因为所有其他应用程序都运行良好。虽然我的项目没有完全编译,但我从我的库文件夹中获得了库jar文件,当我将该jar作为库导入其他项目时,我收到了相同的错误消息。
所以我认为我在这里错过了一些关键点。如果有人遇到类似的问题或可以帮助我,请给我你的建议。
如果你想让我发布我的任何代码,然后告诉我,我会这样做,谢谢。
我只是添加一个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
虽然当我编译我的项目时它没有显示任何错误,但它向我显示了我提到的上述错误。