Is there a way to make gradle bind all the dependency classes to output aar file?
Yes, there is a way to do this, I have encountered this problem before. But it will be a long story, carry me.
Background
When you specify the dependency in the build.gradle file, you tell gradle to load the artifact (AAR / JAR) from the Maven repository. So gradle needs two things:
- Maven Storage Location
- addiction itself
You specify the Maven repository at the top level of build.gradle
repositories { jcenter() google() }
and indicate the dependency on the project level build.gradle
dependencies { compile 'com.example:mylibrary:1.0.1' }
How does gradle / Maven know that mylibrary needs other dependencies? From your Dependency POM file .
POM example
Let me use Square OkHttp as an example, you can find this artifact at mvnrepository.com.

OkHttp has a POM file. You can click this link to show the full XML, I show you the interesting part, which is a block of dependencies.
<project> <modelVersion>4.0.0</modelVersion> <parent>...</parent> <artifactId>okhttp</artifactId> <name>OkHttp</name> <dependencies> <dependency> <groupId>com.squareup.okio</groupId> <artifactId>okio</artifactId> </dependency> <dependency> <groupId>com.google.android</groupId> <artifactId>android</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>jsr305</artifactId> <scope>provided</scope> </dependency> </dependencies> <build>...</build> </project>
OkHttp requires dependencies on other artifacts. Okio, Android and jsr305. But when you include OkHttp in your project, you do not need to include Okio, Android and jsr305 in your project. Why is this?
Because gradle / Maven will search the POM files and load the dependencies for you. More NoClassDefFoundError .
Your library project
Back to your question, how to "combine" your library project into a single .aar? Here are the steps:
- Publish your dependencies (your JAR or AAR) in the Maven repository.
There is a high probability that your dependencies already exist in mvnrepository, if it does not exist, you can send your dependencies to bintray, mvnrepository or host your own Maven repository.
- Create a POM file for AAR.
As in the OkHttp dependency block, you put library dependencies in it.
- Submit your AAR library to the Maven repository.
You can use mvndeploy to submit your library to the Maven repository.
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: mavenPassword@nexus.example.com /repository/maven-releases/"
If you do not want to do steps 2 and 3 manually, as you can assume, "there is a gradle plugin for this!" Plugin android-maven-publish , credit for: wupdigital to facilitate this process.
Now you can use the gradle command to publish your library:
gradle yourlibrary:assembleRelease yourlibrary:publishMavenReleaseAarPublicationToMavenRepository
How to share your library project
The next time your teammate asks for your library, you can give them two things:
- Maven repository containing your library and its dependencies
- Your group, artifact, and your library name
What is it! No need to specify dependencies for your peers.
Greetings 🥂