如何创建可在Android-Studio项目中使用的jar?

时间:2016-03-05 20:03:00

标签: java eclipse android-studio gradle jar

我的情况: 我创建了一个Android-Studio项目,其中包含来自Android-Studio的示例Activity。 我在eclipse(neon)中创建了一个带有following these steps的jar(不可执行),只有一个类,然后跟着steps of this answer来包含那个jar。 在我的Activity的onCreate()方法中,我通过调用它的构造函数来使用jar中的代码。

我的问题/问题: 我的问题是什么或我应该在哪里看?我是以错误的方式创建我的jar还是Android Studio中的某些东西我不知道?

Android Studio消息:

UNEXPECTED TOP-LEVEL EXCEPTION:
Error:com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/local/java/jdk1.8.0_72/bin/java'' finished with non-zero exit value 1

Android Studio Gradle控制台(使用' - stacktrace'选项,但我已将该消息缩短了一点):

20:15:12.502 [ERROR] [org.gradle.BuildExceptionReporter] FAILURE: Build failed with an exception.
20:15:12.502 [ERROR] [org.gradle.BuildExceptionReporter] 
20:15:12.502 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong:
20:15:12.502 [ERROR] [org.gradle.BuildExceptionReporter] Execution failed for task ':app:transformClassesWithDexForDebug'.
20:15:12.502 [ERROR] [org.gradle.BuildExceptionReporter] > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/local/java/jdk1.8.0_72/bin/java'' finished with non-zero exit value 1

...

20:15:12.502 [ERROR] [org.gradle.BuildExceptionReporter] 
20:15:12.502 [ERROR] [org.gradle.BuildExceptionReporter] * Exception is:
20:15:12.503 [ERROR] [org.gradle.BuildExceptionReporter] org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformClassesWithDexForDebug'.
20:15:12.503 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
20:15:12.503 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
20:15:12.505 [ERROR] [org.gradle.BuildExceptionReporter] Caused by: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/local/java/jdk1.8.0_72/bin/java'' finished with non-zero exit value 1
20:15:12.505 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.ide.common.internal.WaitableExecutor.waitForTasksWithQuickFail(WaitableExecutor.java:119)
20:15:12.505 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.build.gradle.internal.transforms.DexTransform.transform(DexTransform.java:349)
20:15:12.505 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:151)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:148)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.builder.profile.ThreadRecorder$1.record(ThreadRecorder.java:55)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.builder.profile.ThreadRecorder$1.record(ThreadRecorder.java:47)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:147)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$IncrementalTaskAction.doExecute(AnnotationProcessingTaskFactory.java:244)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:220)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$IncrementalTaskAction.execute(AnnotationProcessingTaskFactory.java:231)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:209)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    ... 70 more
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter] Caused by: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/local/java/jdk1.8.0_72/bin/java'' finished with non-zero exit value 1
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.build.gradle.internal.process.GradleProcessResult.assertNormalExitValue(GradleProcessResult.java:42)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.builder.core.AndroidBuilder.preDexLibrary(AndroidBuilder.java:1454)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.builder.internal.compiler.PreDexCache.preDexLibrary(PreDexCache.java:122)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.builder.core.AndroidBuilder.preDexLibrary(AndroidBuilder.java:1399)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.build.gradle.internal.transforms.DexTransform$PreDexTask.call(DexTransform.java:456)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.build.gradle.internal.transforms.DexTransform$PreDexTask.call(DexTransform.java:410)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter] Caused by: org.gradle.process.internal.ExecException: Process 'command '/usr/local/java/jdk1.8.0_72/bin/java'' finished with non-zero exit value 1
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:367)
20:15:12.506 [ERROR] [org.gradle.BuildExceptionReporter]    at com.android.build.gradle.internal.process.GradleProcessResult.assertNormalExitValue(GradleProcessResult.java:40)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/local/java/jdk1.8.0_72/bin/java'' finished with non-zero exit value 1

1 个答案:

答案 0 :(得分:1)

您尝试添加到.jar的{​​{1}}文件是使用您在Android Project中使用的java compiler编译的。{/ 1>。

可能存在您正在使用的情况Android Project根据您的StackTrace

java v1.8

但您正在使用的com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/local/java/jdk1.8.0_72/bin/java'' finished with non-zero exit value 1 是在jar lower version的任何java中编译的(例如 - jdk1.7或更低)。 eclipse中出现的这个问题被称为major - minor version Exception。请将older jar替换为任何new Jar。

我做了什么来解决我的问题:

  

Android Studio - >模块设置 - >属性 - >将“源兼容性”和“目标兼容性”更改为Java 1.7

     

在我的 Eclipse Project 中 - >构建路径 - >配置构建路径 - >将JRE系统库编辑为1.7

     

重新编译库并在Android Studio中使用该库,然后在Android Studio中清理并重建项目。