1. 程式人生 > 其它 >Android Studio 關聯Module打包成aar和使用本地Maven倉庫

Android Studio 關聯Module打包成aar和使用本地Maven倉庫

技術標籤:Android StudioSDKandroid studiosdkaar

關聯Module打包成aar(建立Maven專案)

  • 理清自己的專案依賴關係
    我的專案
    專案中兩個Module(Interact和JsBridge)需要製作成aar;其中 Interact依賴JsBridge.

  • 按照專案依賴關係由下到上依次製作aar

    1.把JsBridge製作成aar

    配置專案:
    gradle.properties設定全域性變數

#全域性變數
AAR_REPO_PATH=file:///D:/wpf/workspace/project/zhix/AAR
AAR_VERSION=1.0.0
AAR_GROUP_ID=
com.wpf
	JsBridge的build.gradle中配置,最下面新增
//打包釋出配置開始
apply plugin: 'maven'
uploadArchives {
    repositories.mavenDeployer {
        repository(url: AAR_REPO_PATH)
        pom.project {
            groupId AAR_GROUP_ID
            artifactId project.getName()
            version AAR_VERSION
        }
    }
}

android studio Terminal中執行 gradlew :JsBridge:uploadArchives
檢視自己配置的AAR_REPO_PATH路徑下是否引數檔案如:
在這裡插入圖片描述

2.Interact引入本地aar(這裡使用上面製作的本地Maven倉庫)

配置ZhixDemo專案的build.gradle
repositories {
        google()
        jcenter()
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
        maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/' } maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'} jcenter { url AAR_REPO_PATH.toString() } }
引入JsBridge模組的aar, 配置Interact的build.gradle
def isAar = false
//打包釋出配置開始
apply plugin: 'maven'
uploadArchives {
    isAar = true
    repositories.mavenDeployer {
        repository(url: AAR_REPO_PATH)
        pom.project {
            groupId AAR_GROUP_ID
            artifactId project.getName()
            version AAR_VERSION
        }
    }
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 
    implementation 'com.google.code.gson:gson:2.8.5'
    print("isAar:::$isAar")
    if (isAar){
        implementation "${AAR_GROUP_ID}:JsBridge:$AAR_VERSION"
    }else {
        implementation project(path: ':JsBridge')
    }

}

android studio Terminal中執行 gradlew :Interact:uploadArchives
檢視自己配置的AAR_REPO_PATH路徑下是否引數檔案如:

在這裡插入圖片描述

如果有必要,可搭建遠端倉庫,使用***Nexus***搭建。這裡就不詳述

使用本地Maven倉庫

其他專案使用本地庫。

1.配置專案build.gradle

repositories {
        google()
        jcenter()  
        jcenter {
            url 'file:///D:/wpf/workspace/project/zhix/AAR'
        }
    }

2.需要使用Interact模組的aar的lib配置build.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation "com.wpf:Interact:1.0.0"
}

用問題歡迎留言。