Create an AAR with multiple AARs/JARs
Asked Answered
B

3

12

I have seen questions (Android Studio combine 2 .aar into one and others) posted by various developers but I haven't seen a definitive response that enables me to create an AAR that includes 1 or more AARs or JARs (I can do with JARs since I don't need to share any resources; only classes). Here is the app.gradle for my library project:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.google.code.gson:gson:2.1'
    compile ('libs/eventbus.jar')
    compile project(':core-release')
    compile project(':midware-release')
}

Again, this app is a library project which needs two other library projects ('core-release', 'midware-release') and while I was able to generate one AAR file that I can use in my application, the application was unable to find the dependent library projects' classes so, I had to add the two library projects' AARs into my application.

Here is the app.gradle application project (without adding the JARs manually) which is unable to find the dependent projects' classes:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.app.sample"
        minSdkVersion 19
        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.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile files('libs/eventbus.jar')
    compile project(':sdk3-debug')
}

I don't think the library project's AAR file is pulling in the dependent projects (AAR or JAR) and hence the application is unable to find the classes.

I read about transitive dependency, but I was unable to find an example implementation which may help with my situation.

Binate answered 23/12, 2015 at 23:43 Comment(0)
D
1

This should fix your issue:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile files('libs/eventbus.jar')
    compile project(':sdk3-debug') { transitive = true }
}

Include the transitive flag at true.

Douai answered 28/12, 2015 at 0:4 Comment(12)
I tried but I am unable to get past No such property: transitive for class: org.gradle.api.internal.project.DefaultProject_Decorated error. Even switching to default gradle wrapper in Build Tools -> Gradle settings from local gradle distribution didn't helpBinate
Can you please try: compile (project(':sdk3-debug')) { transitive = true }Douai
This new format ensures gradle is happy now. However, my application project is unable to find the classes in the library project (midware) which is within the AAR file. The classes that are directly in the AAR file are accessible. As I had mentioned, my AAR file has two dependent projects (core and midware).Binate
Can you try to update the library project dependencies with the transitive flag and try again?Douai
Yes, I was able to by using this setting in my build.gradle for the library modules: compile ('com.example:myLibrary:versionX') { transitive = true }. Thank you @Sandro.Binate
Hum, so it is fixed?Douai
Yes, solved for my env. Now, I need to publish the libraries to a repository where other developers can have access.Binate
For that I would recommend you to use jitpack.io and please don't forget to mark my answer as correct :)Douai
HI Sandro, I have tried above mentioned method in dependencies compile (project(':getCredential')) { transitive = true } . But, still my classes.jar is not compiled properly while taking .aar file. can you give brief suggestion how to compile properlyEdwardedwardian
@SandroMachado : Does adding {transitive = true} give me access to the dependency used inside the aarDefiniendum
Yes, it includes the dependency to be downloaded.Douai
Seems out-dated: "Could not set unknown property 'transitive' for project"Musicology
P
4

I haven't seen a definitive response that enables me to create an AAR that includes 1 or more AARs or JARs.

Yes, I think because this topic is not limited to AAR or JAR, but how Maven manage dependency.

https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

while I was able to generate one AAR file that I can use in my application, the application was unable to find the dependent library projects' classes so, I had to add the two library projects' AARs into my application.

It's not your AAR responsibility to include your dependencies, your POM file should include information about dependencies.

https://maven.apache.org/pom.html

I don't think the library project's AAR file is pulling in the dependent projects (AAR or JAR) and hence the application is unable to find the classes.

Correct, you still need to include libraries dependency in your Application.

I assume you want your library can be used by Application, without specifying your library dependencies core-release and midware-release. I made a full explanation here android studio generate aar with dependency but here is what you need to do:

  1. Upload core-release and midware-release to your Maven repository
  2. Create a POM file for your library that include your dependencies

    <project>
       <modelVersion>4.0.0</modelVersion>
       <parent>...</parent>
       <artifactId>okhttp</artifactId>
       <name>OkHttp</name>
       <dependencies>
          <dependency>
             <groupId>com.example</groupId>
             <artifactId>core-release</artifactId>
          </dependency>
          <dependency>
             <groupId>com.example</groupId>
             <artifactId>midware-release</artifactId>
          </dependency>
       </dependencies>
       <build>...</build>
    </project>
    
  3. Publish your AAR with that POM file

    mvn deploy:deploy-file \
          -DgroupId=com.example \
          -DartifactId=your-library \
          -Dversion=1.0.1 \
          -Dpackaging=aar \
          -Dfile=your-library.aar \
          -DpomFile=path-to-your-pom.xml \
          -DgeneratePom=true \
          -DupdateReleaseInfo=true \
          -Durl="https://mavenUserName:[email protected]/repository/maven-releases/"
    

And then your Application can use your library. Gradle will download your library transitive dependencies automatically.

Prescience answered 25/1, 2018 at 9:4 Comment(0)
B
2

I was able to address the issue by following Stan Kurdziel 's suggestion: https://mcmap.net/q/50286/-multiple-aar-files and here are steps I took to arrive at a solution:

  • I created an AAR which I published to the local maven repository
  • Now this AAR was accessible in the application project
  • This AAR also had reference to the classes in the core and midware libraries. I followed Stan Kurdziel's approach #2:

Manually add the dependency on Library Project 2 to the Application Project - so that your Application has a dependency line for both Libraries. Depending on your specific situation this may or may not be a workable solution.

Hope this helps others that might run into similar issue.

Binate answered 19/1, 2016 at 21:28 Comment(0)
D
1

This should fix your issue:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile files('libs/eventbus.jar')
    compile project(':sdk3-debug') { transitive = true }
}

Include the transitive flag at true.

Douai answered 28/12, 2015 at 0:4 Comment(12)
I tried but I am unable to get past No such property: transitive for class: org.gradle.api.internal.project.DefaultProject_Decorated error. Even switching to default gradle wrapper in Build Tools -> Gradle settings from local gradle distribution didn't helpBinate
Can you please try: compile (project(':sdk3-debug')) { transitive = true }Douai
This new format ensures gradle is happy now. However, my application project is unable to find the classes in the library project (midware) which is within the AAR file. The classes that are directly in the AAR file are accessible. As I had mentioned, my AAR file has two dependent projects (core and midware).Binate
Can you try to update the library project dependencies with the transitive flag and try again?Douai
Yes, I was able to by using this setting in my build.gradle for the library modules: compile ('com.example:myLibrary:versionX') { transitive = true }. Thank you @Sandro.Binate
Hum, so it is fixed?Douai
Yes, solved for my env. Now, I need to publish the libraries to a repository where other developers can have access.Binate
For that I would recommend you to use jitpack.io and please don't forget to mark my answer as correct :)Douai
HI Sandro, I have tried above mentioned method in dependencies compile (project(':getCredential')) { transitive = true } . But, still my classes.jar is not compiled properly while taking .aar file. can you give brief suggestion how to compile properlyEdwardedwardian
@SandroMachado : Does adding {transitive = true} give me access to the dependency used inside the aarDefiniendum
Yes, it includes the dependency to be downloaded.Douai
Seems out-dated: "Could not set unknown property 'transitive' for project"Musicology

© 2022 - 2024 — McMap. All rights reserved.