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

如何制作包含build.gradle(app)所有依赖的本地.aar文件?开启transitive无效

How to Create a Local .aar with All Dependencies (Fixing Transitive Flag Issues)

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

  1. In Android Studio, go to File > New > New Module > Android Library and create a new module.
  2. Move all your code, resources, and dependencies from your app module’s build.gradle to this new library module’s build.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:

  1. Rename the AAR file to have a .zip extension.
  2. Unzip it and open the classes.jar file—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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:11:28