You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle 3.0及以上版本如何将JAR依赖引入AAR库

Solution for Including JAR Dependencies into AAR with Gradle 3.0+

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 libs folder of your Android library module.
  • In your module’s build.gradle (or build.gradle.kts if you prefer Kotlin DSL), declare the dependency using either api or implementation with the files() 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 libs directory.

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 api or implementation intentionally: Use api if you want apps using your AAR to access the JAR’s classes. Use implementation if 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:22:54