Kunu
Kunu

Reputation: 5134

Error while creating own jar library for Android project

Currently I am working on basics of how to create a jar library in Android Studio.

I followed How to make a .jar out from an Android Studio project and how to create a jar file from android studio tutorials. And went through each step described in the first tutorial.

However when I am buildig and running this library project it is showing me following error

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

I looked into this issue in SO but those questions didn't helped me at all. It is not memory issue for me because all other applications are running pretty fine. Though my project didn't compile perfectly still I got the library jar file from my library folder and when I import that jar in other project as library I got same error message.

So I think I am missing some key point here. If anybody faced similar problem or can help me out please gimme your suggestions.

If you want me to post any of my code then tell me and I will do that, Thanks.

I just add a java library module and add a very simple Multiplication class there( Check below code). Then add that library module as library in my project. Library Code

public class Multiplication {

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

Activity Code

@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')
}

Complete Error Log

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

Though it not showing any error when I am compiling my project it showing me the above error I mentioned.

Upvotes: 16

Views: 4960

Answers (6)

yovi
yovi

Reputation: 1

I also got the same error while building my android solution (has couple of java modules) with Java 1.8 SDK. finally I have fixed it with Gradle Retrolamdba Plugin

Add the below classpath to the build.gradle file in the root project

classpath 'me.tatarka:gradle-retrolambda:3.2.4'

And the retrolamdba plug to the build.gradle located in the sub project/java module (jar library)

apply plugin: 'java'
apply plugin: 'me.tatarka.retrolambda'

Upvotes: -2

Ilya Tretyakov
Ilya Tretyakov

Reputation: 7010

Update 2

According to logs you build your *.jar with Java 1.8. The max version of java that Android supports is 1.7. So you have to set source code compatability to 1.7 and rebuild the jar-file.

If you are building this jar not from gradle (or Android Studio) you need to change your JDK version (File -> Project Structure -> SDK Location -> JDK Location).

For gradle module see "Update 1"


Update 1

For you java module (from which you are trying to get jar) put these lines:

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

Try to put next code to your gradle files for Android modules:

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

Upvotes: 5

N Kaushik
N Kaushik

Reputation: 2208

Reason for "non-zero exit value 1" error may be different.

1.Delete the library and next rebuild the project to check if it is working or not.

2.If it is working fine remove compile filetree option from the dependencies and rebuild it again

dependencies {
compile 'com.android.support:appcompat-v7:22.2.1'
compile files('D:/Work_New/MyApplication/libs/mylibjar.jar')
}
  1. If this problem is still there, create one simple project and import the jar library first to this project. Next add these other dependencies one by one to find out the problem.If the problem is not occuring again, copy all java files to this new project. Next copy "image" files one after another. Often drawable image files causes this problem "non-zero exit value 1"

Edit: Also add this inside android{ of your dependency:

dexOptions {
    preDexLibraries = true
}

Upvotes: 1

naXa stands with Ukraine
naXa stands with Ukraine

Reputation: 37993

Try to import your module (a java library) like this:

compile project(':MyLib');  // MyLib is a name of your module

Upvotes: 0

and_dev
and_dev

Reputation: 3861

I assume you have currently two projects in your workspace: 1) Android app project (marked in gradle file as "apply plugin: 'com.android.application'") 2) java library project ("apply plugin: 'java'") and they are both linked via the settings.gradle file.

You should be able to build the java project via 'assemble' which then outputs a .jar file in the build/libs folder (this can also be done via the 'jar' command of Gradle).

Maybe you can post a screenshot of your tree structure in Android Studio for some more help.

Upvotes: 0

Malte
Malte

Reputation: 604

In android you cannot use .jar-files as libraries from other android-projects. you have to import the android-project as a library and set the project to a lib-project. You have to import the android-lib-project into each other android-project you are using it in.

Thats how it is in eclipse, i guess in Android Studio it is a similar approach

Upvotes: 0

Related Questions