gfinotti
gfinotti

Reputation: 673

Android Studio: add a custom jar as dependency

I have a java REST project that communicates with an Android Project, using DTO classes.

Initially, I had created DTO classes duplicated in both projects, but now I have a new Java/JAR project with this common DTO classes and I want to use this library in both projects (Rest and Android).

I pasted the JAR file in app/libs folder in Android Studio.

I added this file as a Jar dependency in Android Studio.

The build.gradle file contains the jar's reference compile files('libs/my-custom-jar-1.0.0.jar')

dependencies {
    compile files('libs/my-custom-jar-1.0.0.jar')
    compile fileTree(include: ['*.jar'], dir: 'libs')
}

I followed the steps described here but doesn't work. The classes packaged in the JAR file are not available.

I also try to "Invalidate Cache and Restart" Android Studio

[]'s

Edit

The file build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestCompile 'com.google.code.findbugs:jsr305:3.0.2'
    compile project(':com.custom.dialog')
    compile project(':zxing_standalone')
    compile 'com.android.support:appcompat-v7:26.0.2'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.xxxxxxxx.android:library:1.0.23@aar', { transitive = true }
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'com.squareup.okhttp3:okhttp:3.9.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
    compile 'com.android.support:design:26.0.2'
    compile 'commons-io:commons-io:2.0.1'
    compile 'org.apache.commons:commons-lang3:3.5'
    compile files('libs/my-custom-jar-1.0.0.jar')
}

Upvotes: 2

Views: 1673

Answers (2)

Martin Zeitler
Martin Zeitler

Reputation: 76579

A) only add this line:

implementation fileTree(dir: "libs", include: ["*.jar"])

and remove this line:

compile files('libs/my-custom-jar-1.0.0.jar')

when adding both lines, you'd end up with duplicate dependencies.

B) and this one local AAR dependency:

compile 'com.xxxxxxxx.android:library:1.0.23@aar', { transitive = true }

needs the libs directory defined as flatDir repository in the root project's build.gradle:

allprojects {
    repositories {
        ...
        flatDir { dirs "libs" }
    }
}

Upvotes: 0

kandroidj
kandroidj

Reputation: 13922

Make sure that you have copied the jar file into your project but you are referencing a very old post. There is no need to add the additional line for the jar file as the fileTree method will include anything with .jar extension from libs folder.

I would update my dependencies to this:

dependencies {
     implementation fileTree(dir: 'libs', include: ['*.jar'])
}

Then do a gradle sync and your files should be available to you.

EDIT The real issue though is the order of arguments. You want to pass the directory as the first argument. fileTree builds a ConfigurableFileTree so the first argument needs to be the base directory.

UPDATE Here is what my project looks like after building a .jar and pasting it into my project, using the above dependencies and syncing gradle. libs folder with .jar

And this is accessible from my activity code:

public class MainActivity extends AppCompatActivity {

    PersonDAO personDao;

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

        personDao = new PersonDAO("John", "Smith", 50);
    }

    public void doSomething(View view){
        Toast.makeText(this, getString(R.string.greeting, personDao.getFirstName()), Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 4

Related Questions