如何制作包含build.gradle(app)所有依赖的本地.aar文件?开启transitive无效
Absolutely, bundling all your app module’s declared dependencies into a single local .aar is totally feasible. Let’s first clarify why your transitive=true attempt didn’t yield the results you wanted, then dive into actionable solutions.
Why the Transitive Flag Didn’t Work for You
The transitive property controls whether Gradle resolves dependencies of dependencies during the build process—but it doesn’t embed those dependencies into the .aar itself. Plus, the app module is designed to generate an APK, not an AAR. Even if you set transitive=true there, it won’t affect AAR output because the app module doesn’t produce one.
Solution 1: Create a Fat AAR with Embedded Dependencies
If you want a single .aar that contains all your code and dependencies (so other projects don’t need to add those dependencies separately), you’ll need to customize the build process of a library module (since only library modules produce AARs). Here’s how:
Step 1: Set Up a Library Module
- In Android Studio, go to
File > New > New Module > Android Libraryand create a new module. - Move all your code, resources, and dependencies from your app module’s
build.gradleto this new library module’sbuild.gradle.
Step 2: Add Custom Build Logic to Embed Dependencies
Add this code to your library module’s build.gradle to embed all transitive dependencies into the AAR’s classes.jar:
android { // Keep your existing Android config (compileSdk, defaultConfig, etc.) // Customize each library variant to bundle dependencies libraryVariants.all { variant -> variant.outputs.each { output -> def processResourcesTask = output.getProcessResourcesProvider().get() processResourcesTask.doLast { // Path to the original classes.jar in the build output def classesJar = new File(output.getClassesDirs().singleFile.parent, "classes.jar") // Get all runtime dependencies def runtimeDependencies = configurations.runtimeClasspath // Temp directory to merge all class files def tempDir = file("$buildDir/temp/fatAar") tempDir.mkdirs() // Extract all dependency JARs into the temp directory runtimeDependencies.each { file -> if (file.name.endsWith(".jar")) { copy { from zipTree(file) into tempDir // Skip signature files to avoid conflicts exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA' } } } // Extract the original classes.jar into the temp directory copy { from zipTree(classesJar) into tempDir } // Create a new fat classes.jar with all merged classes ant.jar(destfile: classesJar) { fileset(dir: tempDir) } // Clean up the temp directory tempDir.deleteDir() } } } }
Step 3: Build and Verify the Fat AAR
Run ./gradlew assembleRelease (or assembleDebug for a debug build) from the terminal. Your fat AAR will be located at your-library-module/build/outputs/aar/.
To confirm dependencies are embedded:
- Rename the AAR file to have a
.zipextension. - Unzip it and open the
classes.jarfile—you should see classes from your dependencies alongside your own code.
Solution 2: Publish to Local Maven (No Embedding Needed)
If you don’t strictly need to embed dependencies into the AAR, a cleaner approach is to publish your library to a local Maven repository. This way, when another project imports your library, Gradle will automatically pull in all transitive dependencies via the generated POM file.
Add this to your library module’s build.gradle:
plugins { id 'com.android.library' id 'maven-publish' // Add the Maven publish plugin } android { // Your existing Android config } dependencies { // Keep all your dependencies here } publishing { publications { maven(MavenPublication) { from components.release // Use the release variant for publication // Set your library's coordinates groupId = 'com.yourcompany' // e.g., your company's domain artifactId = 'your-library-name' // e.g., your library's name version = '1.0.0' // Your library's version } } repositories { maven { // Local repository directory (you can change this path) url = uri("$rootDir/local-maven-repo") } } }
Run ./gradlew publishToMavenLocal to publish to your system’s local Maven repo, or ./gradlew publish to push to the local directory you specified. Other projects can then add your library as a dependency with:
implementation 'com.yourcompany:your-library-name:1.0.0'
Gradle will automatically resolve and download all transitive dependencies listed in the POM file.
内容的提问来源于stack exchange,提问作者user1974368

