如何用Gradle创建*.aar Android库并在本地仓库引入作为依赖
Got it, let's break this down step by step—creating your own AAR library, adding it to other Android projects, and handling the local repo setup with custom Gradle configs is totally doable once you know the ropes.
First up, building your own AAR starts with an Android Library module in Android Studio:
- Open your Android project, go to
File > New > New Module - Select
Android Library, name it something likemy-custom-library, then hit Finish - Write your code, add resources, and set up any custom Gradle configs (like third-party dependencies or build rules) in this library module
- Generate the AAR: Head to the right-side Gradle panel, find your library module, expand
Tasks > build, then double-clickassembleRelease(orassembleDebugif you need a debug build) - You'll find the finished AAR file in
[your-library-module]/build/outputs/aar/—look for something likemy-custom-library-release.aar
You’ve got two solid options here, pick whichever fits your workflow:
方式一:直接拷贝到libs目录(快速简单)
- Copy your generated AAR into the target project’s
app/libsfolder (create thelibsfolder if it doesn’t exist) - Open the target project’s
app/build.gradlefile:- Add a
flatDirentry to therepositoriesblock to tell Gradle where to look for local AARs:repositories { google() mavenCentral() flatDir { dirs 'libs' } } - Add the dependency in the
dependenciesblock (use the AAR filename without the.aarsuffix):dependencies { implementation(name: 'my-custom-library-release', ext: 'aar') }
- Add a
- Click Sync Now and Gradle will load your AAR dependency.
方式二:通过本地Maven仓库添加(推荐用于版本/依赖管理)
This is the better choice if your library has its own dependencies or you plan to update it regularly—copying the AAR directly won’t carry over dependency metadata, which can cause headaches later. We’ll dive into the full setup for this in the next section.
If your library has custom Gradle configs (like dependency declarations or build logic), publishing it to a local Maven repository ensures all that metadata travels with the AAR. Here’s how to do it:
步骤1:配置Library模块的build.gradle
Open your library’s build.gradle (we’ll use Groovy syntax here; adjust for Kotlin DSL if needed):
- Apply the
maven-publishplugin alongside the Android Library plugin:plugins { id 'com.android.library' id 'maven-publish' } - Set up your Android build settings (adjust SDK versions to match your project):
android { compileSdk 34 defaultConfig { minSdk 21 targetSdk 34 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } - Add the
publishingblock to define your library’s coordinates and target the local Maven repo:publishing { publications { release(MavenPublication) { // Pull the AAR from the release build variant from components.release // Replace these with your own library details groupId = "com.yourcompany" artifactId = "my-custom-library" version = "1.0.0" } } // Publish to your local Maven repo (default path: ~/.m2/repository) repositories { mavenLocal() } }
步骤2:发布到本地Maven仓库
- Go back to the right-side Gradle panel, find your library module, expand
Tasks > publishing, then double-clickpublishToMavenLocal - Wait for the task to finish—your library (including AAR, dependency metadata, and Gradle config details) will now live in your local Maven repo.
步骤3:依赖本地仓库的库在其他项目中
- Open the target project’s
settings.gradleand make suremavenLocal()is included in the repositories list:dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() mavenLocal() // Don't forget this line! } } - Add the dependency to the target project’s
app/build.gradleusing the coordinates you defined earlier:dependencies { implementation 'com.yourcompany:my-custom-library:1.0.0' } - Click Sync Now—Gradle will automatically pull your library and all its associated dependencies from the local repo.
内容的提问来源于stack exchange,提问作者Oguzhan Aydin

