2

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')
}
kandroidj
  • 13,784
  • 5
  • 64
  • 76
gfinotti
  • 613
  • 5
  • 21
  • Can you collapse your libs folder, screenshot the contents of the .jar file and paste it in the post? Also, you say the classes are not available. Have you made sure that they are public classes so you can call them from outside the packaged jar? – kandroidj Dec 05 '18 at 16:32
  • Can you paste the libs/ folder collapsed as a screenshot from the file explorer as I have below? – kandroidj Dec 06 '18 at 17:41
  • Any progress on this, Do you have it working yet? Anything more you can share to let us help you asnwer – kandroidj Dec 07 '18 at 15:55

2 Answers2

4

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();
    }
}
kandroidj
  • 13,784
  • 5
  • 64
  • 76
  • thanks for answer. The error still remains @kandroidj I edit the original post and paste the complete build.gradle file – gfinotti Dec 05 '18 at 16:18
0

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" }
    }
}
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Thanks for answer. My environment is Android Studio 2.3 (gradle 2.3.0) and I can´t update it. `implementation fileTree(dir: "libs", include: ["*.jar"])` is not available – gfinotti Dec 06 '18 at 15:31