Gradle 3.0及以上版本如何将JAR依赖引入AAR库
Hey there, I totally get the frustration—when Gradle swapped out compile for api/implementation back in version 3.0+, a lot of old AAR packaging tricks stopped working. Let’s walk through the updated, reliable ways to bundle JAR dependencies into your Android library AAR, covering both local and remote JAR scenarios.
1. For Local JAR Files (Stored in libs/ Directory)
This is the simpler case—here’s how to make sure your local JAR gets bundled into the AAR:
- Drop your JAR file directly into the
libsfolder of your Android library module. - In your module’s
build.gradle(orbuild.gradle.ktsif you prefer Kotlin DSL), declare the dependency using eitherapiorimplementationwith thefiles()method:dependencies { // Use 'api' if you want the JAR's classes to be accessible to apps using your AAR // Use 'implementation' if the JAR is only needed internally by your library api files('libs/your-dependency.jar') } - Gradle will automatically include this local JAR in the generated AAR. To verify, just unzip the built AAR file—you’ll find the JAR tucked inside its own
libsdirectory.
2. For Remote Maven Dependencies
If your JAR is hosted on Maven Central, a private repo, or any remote repository, api/implementation won’t bundle it into the AAR by default (it only adds a reference to the dependency). Here are two solid workarounds:
Option A: Copy Remote JAR to Local libs First
Add a custom task to pull the remote JAR into your libs folder during the build, then include it as a local dependency:
// Task to download and copy the remote JAR to our libs folder task copyRemoteJarToLibs(type: Copy) { // Replace 'com.example:your-remote-dep:1.0.0' with your actual dependency coordinates def targetJar = configurations.api.find { it.name.contains('your-remote-dep') } from targetJar into 'libs' } // Ensure the copy task runs before the build starts preBuild.dependsOn(copyRemoteJarToLibs) dependencies { // Exclude the remote dependency to avoid duplicate references api('com.example:your-remote-dep:1.0.0') { exclude group: 'com.example', module: 'your-remote-dep' } // Include the locally copied JAR instead api files('libs/your-remote-dep-1.0.0.jar') }
Option B: Directly Bundle Remote JAR into AAR
Skip the local copy and modify the AAR packaging process directly using Gradle’s variant API:
android { libraryVariants.all { variant -> variant.outputs.each { output -> // Replace 'com.example:your-remote-dep:1.0.0' with your dependency def targetJar = configurations.api.find { it.name.contains('your-remote-dep') } output.packageLibrary.from(targetJar) { into 'libs' } } } } dependencies { api 'com.example:your-remote-dep:1.0.0' }
This approach injects the remote JAR straight into the AAR without cluttering your local libs folder.
Key Things to Remember
- Choose
apiorimplementationintentionally: Useapiif you want apps using your AAR to access the JAR’s classes. Useimplementationif the JAR is strictly internal to your library—note that the JAR will still be bundled in the AAR, its classes just won’t be exposed to the app’s code. - Watch for duplicate dependencies: If apps using your AAR might already include the same JAR, bundling it could cause runtime conflicts. It’s good practice to document this dependency so users can exclude it from their builds if needed.
内容的提问来源于stack exchange,提问作者Ashish Kshirsagar

