70

I am building android library project, which has a dependency on another internal library project.

I am wondering if there is a way to package a single AAR library, which already contains internal library inside it. I would like to share only 1 AAR library package to my application developers.

This is how my build.gradle files look currently, but currently they produce separate AAR files and both needs to be included in Application's build.gradle. As application is being built by another company, we need to share the final AAR file with them and not the complete library projects.

----- internalLib -------->>>>>>>>>>

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}
apply plugin: 'android-library'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion '18.1.1'
}

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
}

----- externalLib --------

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}
apply plugin: 'android-library'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion '18.1.1'
}

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile project(':internalLib')
}
Rahul Jain
  • 833
  • 1
  • 7
  • 6

4 Answers4

42

There is no mechanism to combine library. It's a bit complicated as you probably want to control which dependencies get merged (for instance you probably don't want to include support-v4 in there). Also you'd need to merge the resources and Android manifest.

At this time there's no way to easily hack something, unless you are sure the resources have no conflicts between the two res folders (for instance you could have strings_a.xml in one lib and strings_b.xml in the other lib). This way you can just "merge" the two res folders by copying them both into the same location (as opposed to do a merge at the android res level).

For the Manifest it'd be more complicated, but doable with some custom code.

Providing a built-in mechanism for this is very low on our priority so don't expect it anytime soon.

Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
  • @Xavier: Merging of resources and manifests is not that important for me at this time. Considering this do you think it will help if I start generating JAR instead of AAR from internalLib? As AAR can contain JAR inside it, in its libs directory. – Rahul Jain Dec 26 '13 at 06:41
  • 1
    If you have no resources and nothing in your manifest (ie no activity,service,...), and no permisssion, then you could use a jar yes. – Xavier Ducrohet Dec 26 '13 at 19:26
  • 2
    Now there is a Manifest merger and a resource merger. Is there something else aside from dependencies than can make this request complicated to include? Or is there any news about this? – Gaëtan Apr 16 '15 at 06:58
  • 29
    @XavierDucrohet Its mid 2015 now and we are still not able to create a single aar file from multiple library projects? – Teodor May 13 '15 at 13:18
  • @Teodor I have tried cooking up a script that works for me. Check out the answer at http://stackoverflow.com/questions/28605367/library-with-bundles-dependencies-fat-aar/31234074 – Adwiv Jul 05 '15 at 20:19
  • 38
    Another year has paased and there's still no proper way to build a library AAR that would collect sublibrary codes, resources and native libraries. Hopefully Google will address this issue soon. – Kung Foo Mar 31 '16 at 09:18
  • 1
    @XavierDucrohet still no news for this? Would it be possible to include AAR files inside an AAR files (like it is currently possible in the libs/ folder for jars)? – Gaëtan Jun 24 '16 at 09:05
  • Same question here that Gaetan asked. Anyone know if we can include aar files or any other format? – PGMacDesign Nov 15 '16 at 20:48
  • just I tried one way and found solution about that.use fat-aar. try this way, we can able to create aar using library,jars dependencies in android studio latest 2.2.2.. – harikrishnan Dec 16 '16 at 09:35
  • still no solution to the problem? – Maxgmer Nov 27 '17 at 10:27
  • 23
    4 years have passed since that answer... And still there is no official support for merging AARs... – DoDo Dec 27 '17 at 09:16
  • So, if you want to use something like this, the solution is to manually add the dependencies of the library you want to use. into your own project? If I generate a JAR would I face the same issue or with the JAR the dependencies would be automatically included in my project? – xarlymg89 Mar 02 '18 at 14:57
  • 6
    Still answers for this ? – pritam001 May 01 '18 at 12:33
  • 4
    Has anyone figured out this issue? – Roger Alien Feb 01 '22 at 18:20
1

For the sake you have to upload each library as separately on maven and use its implementation in parent library modules till the main library module. Only then when you publish your main library on maven will include your all child dependencies.

Muhammad Omer
  • 229
  • 2
  • 11
0

As far as we have only one option add aar as api dependency inside the module. For that we have to generate aar file and publish it to Maven and make it accessible by another module and consume it in app.

https://developer.android.com/studio/projects/android-library

As mentioned above android developer document.

The library module with source code is copied to your project, so you can actually edit the library code. If you want to maintain a single version of the library code, then this is probably not what you want and you should instead add the compiled AAR file as described above.

If there anything else we can do, please let us know by jot down in the command section.

Arul
  • 1,031
  • 13
  • 23
0

It is not supported

It is not recommended to include one library into another because it leads to a serious issues with managing versions and complexity of creating and supporting such solution.

You should stick to native approaches like dependency manager or rearchitect your codebase

[iOS Umbrella framework]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205