简体   繁体   中英

How to include .jar dependencies into final .aar library file? (Android Studio 1.5)

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
}

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.

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM