Artifactory搭建本地仓库(二)-通过Gradle上传AAR

398 阅读2分钟

个人博客

www.milovetingting.cn

前言

在上一篇文章 Artifactory搭建本地仓库 中,已经搭建好了本地仓库,这一篇,主要介绍在Android Studio中通过Gradle上传AAR到本地仓库,以便其它项目引用。

上传AAR

  • 在项目根目录下的gradle文件的dependencies节点增加
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
}
  • 新建一个Module,在新建的Module目录下的gradle文件中增加
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'
  • 新建gradle文件,内容如下
//定义artifactory仓库的地址,按照你自己的修改
def MAVEN_LOCAL_PATH = 'http://localhost:8081/artifactory/'

// 定义构件的相关信息
// 当其他项目远程依赖该构件的时候,结构类似就是 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内容参考自文末的参考链接

  • 在新建的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 右侧的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加速之--(三)发布aar到Artifactory