Artifactory搭建本地倉庫(二)-通過Gradle上傳AAR

個(gè)人博客

http://www.milovetingting.cn

前言

在上一篇文章 Artifactory搭建本地倉庫 中,已經(jīng)搭建好了本地倉庫,這一篇,主要介紹在Android Studio中通過Gradle上傳AAR到本地倉庫,以便其它項(xiàng)目引用。

上傳AAR

  • 在項(xiàng)目根目錄下的gradle文件的dependencies節(jié)點(diǎn)增加
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.15.2"

完整的gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        maven{
            url 'http://localhost:8081/artifactory/android_group/'
        }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.1"
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.15.2"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven{
            url 'http://localhost:8081/artifactory/android_group/'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
  • 新建一個(gè)Module,在新建的Module目錄下的gradle文件中增加
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'
  • 新建gradle文件,內(nèi)容如下
//定義artifactory倉庫的地址,按照你自己的修改
def MAVEN_LOCAL_PATH = 'http://localhost:8081/artifactory/'

// 定義構(gòu)件的相關(guān)信息
// 當(dāng)其他項(xiàng)目遠(yuǎn)程依賴該構(gòu)件的時(shí)候,結(jié)構(gòu)類似就是 implementation 'GROUP_ID:ARTIFACT_ID:VERSION_NAME'
def GROUP_ID = 'com.wangyz.plugins'
def ARTIFACT_ID = 'hello'
def VERSION_NAME = '1.0.0'

publishing {
    publications {
        aar_pub(MavenPublication) {//注意這里定義的 aar_pub,在artifactoryPublish 下需要使用
            groupId = GROUP_ID
            artifactId = ARTIFACT_ID
            version = VERSION_NAME

            // aar文件所在的位置
            // module打包后所在路徑為module模塊下的build/outputs/aar,生成的aar名稱為:module名-release.aar
            artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
        }
    }
}

artifactoryPublish {
    contextUrl = MAVEN_LOCAL_PATH
    publications ('aar_pub')        //注意這里使用的是上面定義的 aar_pub

    clientConfig.publisher.repoKey = 'android_local'        //上傳到的倉庫地址
    clientConfig.publisher.username = 'admin'       //artifactory 登錄的用戶名
    clientConfig.publisher.password = 'admin'   //artifactory 登錄的密碼
}

以上gradle內(nèi)容參考自文末的參考鏈接

  • 在新建的Module目錄下的gradle中引用剛才的gradle文件
apply from: 'maven_publish.gradle'

完整的gradle

apply plugin: 'com.android.library'

apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'

apply from: 'maven_publish.gradle'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 30
        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'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    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'

}
  • 打開Android Studio 右側(cè)的Gradle面板,雙擊Tasks-other-assembleRelease,生成AAR文件
  • 雙擊Tasks-publishing-actifactoryPublish,將生成的AAR上傳到Artifactory的倉庫。

引用AAR

在需要引用aar的Module下的gradle文件中增加引用

implementation 'com.wangyz.plugins:hello:1.0.0'

完整gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.wangyz.artifactory"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

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.wangyz.plugins:hello:1.0.0'

}

參考

AndroidStudio加速之--(三)發(fā)布aar到Artifactory

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容