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
}
11
  • Are you using proguard ? Commented Dec 8, 2015 at 13:49
  • I edited my question with complete build.gradle file. It says there "minify" -> false. Commented Dec 8, 2015 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. github.com/emreaktrk/ContextualView/blob/master/library/… Commented Dec 8, 2015 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 :( Commented Dec 8, 2015 at 14:02
  • Isn't there some straight forward solution to this? I am new to Android (I come from iOS world). Commented Dec 8, 2015 at 14:07

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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}

1 Comment

Does this work with the assemble task when building only the .aar files?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.