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

如何用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.

1. 创建专属的AAR库文件

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 like my-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-click assembleRelease (or assembleDebug if you need a debug build)
  • You'll find the finished AAR file in [your-library-module]/build/outputs/aar/—look for something like my-custom-library-release.aar
2. 将AAR作为Gradle依赖添加到其他Android项目

You’ve got two solid options here, pick whichever fits your workflow:

方式一:直接拷贝到libs目录(快速简单)

  • Copy your generated AAR into the target project’s app/libs folder (create the libs folder if it doesn’t exist)
  • Open the target project’s app/build.gradle file:
    • Add a flatDir entry to the repositories block to tell Gradle where to look for local AARs:
      repositories {
          google()
          mavenCentral()
          flatDir {
              dirs 'libs'
          }
      }
      
    • Add the dependency in the dependencies block (use the AAR filename without the .aar suffix):
      dependencies {
          implementation(name: 'my-custom-library-release', ext: 'aar')
      }
      
  • 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.

3. 将带有自有Gradle文件的AAR添加到本地Maven仓库

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-publish plugin 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 publishing block 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-click publishToMavenLocal
  • 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.gradle and make sure mavenLocal() 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.gradle using 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:31:28