Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an AAR that depends on multiple AARs

I am working to build an SDK that can be used in other application.

My project structure is as follows:

ProjectFolder
  |
  +--AndroidLibs
  |      |
  |      +--UI (android library - AAR)
  |      |
  |      +--Protocol (android library - AAR)
  |      |
  |      +--infra (android library - AAR)
  |
  +--SDK(depends on UI, Protocol and Infra) 
  |
  +--APP(depends on SDK)

As you can see, we have 3 different libraries that we work on, each one is a module in our system (infra, ui and protocol). Each one of them is creating an AAR.

Our SDK is a wrapper with some API calls to the lower layers.

We want to create one AAR that depends on all other AARs, but from some reason when we tried to run it, it says that he can't find the source code for some classes.

I found some questions related to this issue, but they didn't worked. Also tried to work with transitive dependencies, but the bottom line is the same - can't find the source code.

  1. android-studio-how-to-package-single-aar-from-multiple-library-projects: One answer that says there no solution for that (Google employee).
  2. create-an-aar-with-multiple-aars-jars: use the transitive dependencies.
  3. Create Mojo for creating AAR with all "aar" dependencies

Is there anything else we can do?

like image 452
Ofir A. Avatar asked Jul 18 '16 15:07

Ofir A.


People also ask

How do I add a dependency to AAR?

Add your AAR or JAR as a dependency To use your Android library's code in another app module, proceed as follows: Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Jar Dependency in the dropdown.

What is AAR in code?

Android Archive (AAR) plug-ins are bundles that include compiled Java and native (C/C++) code, resources, and an Android Manifest. The . aar file itself is a zip archive which contains all of the Assets. For more details, see Android Developer documentation on creating an Android Library.


2 Answers

From my answer here:

As far as I know you cannot include aars inside an aar. They don't have configuration files that state what dependencies they need. You can either

  1. Strip the source code from the libraries you are using and compile it with your aar. This will do the job if the UI/Protocal/Infra libraries are in-house and you are the only provider.

  2. Consider uploading to bintray or Maven Central Repository

Number two is more preferable since this way all your client has to do is to include a link such as compile 'com.abc.efg:version' to grab all the dependencies you configured. It is also a much better option because there are ways of dealing with version conflicts (ex. with exclude group).

Imagine if your client was using another sdk which was pulling in a different version of UI/Protocal/Infra. If your aar was given to them via the first method, they won't even be able to build the project at all due to version conflicts. However with the second version, they can simply do

compile ('com.abc.efg:version') { exclude group: 'com.companyName.ui' }

and be free from all that headache. A real life example is Facebook's SDK. It pulls in google's play-services, but people often already include that as a dependency for their project and run into problems like this.

like image 180
Bill Avatar answered Sep 21 '22 08:09

Bill


As your projects generally relies on both internal and third-party libraries. The internal libraries can be published on Artifactory repositories and resolve the dependencies over Artifactory with Gradle.

Its easy! Just go through the below articles,

  • http://jeroenmols.com/blog/2015/08/06/artifactory/
  • https://inthecheesefactory.com/blog/how-to-setup-private-maven-repository/en

This is highly scalable and its easy to maintain your code across multiple modules.

Hope this would help you!

like image 22
Karthi R Avatar answered Sep 20 '22 08:09

Karthi R