2

I have Android Studio library project which has it's own Gradle dependencies:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
}

The problem is with those two external libraries (retrofit & converter-gson). Result of this library project is .aar file. But when I use this .aar file in a separate project I get unresolved class error:

java.lang.NoClassDefFoundError: Failed resolution of: Lretrofit/Retrofit$Builder;

Is there a way to include those gradle dependencies from my library project into the final .aar file?

I tried this answer but it didn't work for me.

EDIT: My whole build.gradle file from library project.

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
}

EDIT 2: This is the part of the build.gradle script in my app project which uses the library which was generated before as an .aar file.

3 versions and none of them work (none of them download automatically dependencies form the library)

(note, I am adding this .aar file into the lib folder of my app project and adding the flatDir instruction into it's gradle.build). I am not doing File->New->NewModule->ImportExistingJAR/AAR

repositories {
    flatDir {
        dirs 'libs'
    }
}

1.

compile(name:'mylibrary-release', ext:'aar')

2.

compile(name:'mylibrary-release', ext:'aar') {
    transitive = true
}

3.

compile(':mylibrary-release@aar') {
    transitive = true
}
Community
  • 1
  • 1
vale4674
  • 4,161
  • 13
  • 47
  • 72
  • Are you using proguard ? – Emre Aktürk Dec 08 '15 at 13:49
  • I edited my question with complete build.gradle file. It says there "minify" -> false. – vale4674 Dec 08 '15 at 13:56
  • Well, i have no idea that what causes this but you can give it a chance to this gradle which is written by me. https://github.com/emreaktrk/ContextualView/blob/master/library/build.gradle – Emre Aktürk Dec 08 '15 at 14:02
  • Another solution is just write your library as you want and give it a chance to jitpacks.io to give you an aar, So that autmation can work better :D Jitpacks.io only works with github :( – Emre Aktürk Dec 08 '15 at 14:02
  • Isn't there some straight forward solution to this? I am new to Android (I come from iOS world). – vale4674 Dec 08 '15 at 14:07
  • Probably there would, but i am all that i can help :( – Emre Aktürk Dec 08 '15 at 14:09
  • Bundling jars with an aar is a terrible idea. `compile` dependencies of the library should be automatically downloaded when you try to use this library. Have you tried rebuilding the project (clean & build)? Can you post the app module's build.gradle please? – Eugen Pechanec Dec 08 '15 at 16:05
  • See this post http://stackoverflow.com/questions/30157575/why-should-i-include-a-gradle-dependency-as-aar/30157801#30157801 I believe you're addding the dependency with the `@aar` suffix. Focus on the `transitive=true` part. – Eugen Pechanec Dec 08 '15 at 16:06
  • @EugenPechanec I edited my question with 3 examples of how I tried it and none of them are working. I also tried clean&build and again, those dependencies from the library are not downloaded in my app project. – vale4674 Dec 09 '15 at 09:15
  • You're adding it as a module dependency. Drop the `@aar` suffix altogether. Just `compile ':mylibrary-release'` without anything else would be perfect. – Eugen Pechanec Dec 09 '15 at 09:18
  • @EugenPechanecif If I do `compile(':mylibrary-release') {transitive = true}` I get this error: `Failed to resolve: :mylibrary-release:` – vale4674 Dec 09 '15 at 10:36

2 Answers2

1

Your AAR library is not the problem. Libraries are not supposed to include their dependencies as it may cause version conflicts further down.

The reason your app project is not transitively loading your library's dependencies is the flatdir. Here's the relevant part in the docs (https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver):

This type of repository does not support any meta-data formats like Ivy XML or Maven POM files. Instead, Gradle will dynamically generate a module descriptor (without any dependency information) based on the presence of artifacts.

The correct approach would be to host your library in a public or private maven repository.

wamfous
  • 1,849
  • 1
  • 12
  • 11
0

The accepted answer from How to include JAR dependency into an AAR library works. You need to add an evaluation listener, like this:

afterEvaluate {project -> project.tasks.preBuild.dependsOn copyLibs}
r1k0
  • 1,406
  • 1
  • 11
  • 26