如何将AAR上传到gitHub,自定义远程依赖仓库

这篇博客记录了如何将Android lib 项目打包的AAR ,jar 等文件直接上传到gitHub , 可以像使用其他人开发的库一样,自己也开发lib工具包供全世界的攻城狮使用。


1.首先在gitHub上创建一个新的项目,用来存放aar 和配置文件



2.将该项目clone到本地,指定到一个新的目录里。

比如:/Users/keller/NeverSettle/project/KLUtils



3.新建一个Android 项目,新建一个Module



编辑bundle 的build.gradle 文件

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

 打包发布配置开始 
apply plugin: 'maven'
ext {
    // 从Github上clone下来的项目的本地地址
    GITHUB_REPO_PATH = "/Users/keller/NeverSettle/project/KLUtils"       //这里指定的就是刚刚新建项目后clone下来的在本地的路径
    PUBLISH_GROUP_ID = 'com.keller'
    PUBLISH_ARTIFACT_ID = 'utils_lib'
    PUBLISH_VERSION = '1.0.1'
}
uploadArchives {
    repositories.mavenDeployer {
        def deployPath = file(project.GITHUB_REPO_PATH)
        repository(url: "file://${deployPath.absolutePath}")
        pom.project {
            groupId project.PUBLISH_GROUP_ID
            artifactId project.PUBLISH_ARTIFACT_ID
            version project.PUBLISH_VERSION
        }
    }
}

// 源代码一起打包
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}
artifacts {
    archives androidSourcesJar
}

 打包发布配置结束 




4.打包输出

使用命令行进入到项目的根目录(也就是gradlew文件所在的目录),执行命令:

gradlew uploadArchives

构建成功后会有如下提示:

然后在刚刚配置的clone下来的目录里可以看到生成的aar等文件


得到这些输出产物后,将这些文件统一push到github



5.使用

在项目根目录的build.gradle中添加一下代码:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        google()
        jcenter()
        mavenCentral()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        

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

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://raw.githubusercontent.com/wkangle/KLUtils/master" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

主要就是加了这一句: maven { url "https://raw.githubusercontent.com/wkangle/KLUtils/master" }

其中:wkangle为Github用户名,KLUtils为项目名,其它固定不变

maven { url "https://raw.githubusercontent.com/GitHub用户名/仓库项目名/master" }


然后在Module中引入:




  compile 'com.keller:utils_lib:1.0.1'

这里的规范为:  PUBLISH_GROUP_ID:PUBLISH_ARTIFACT_ID:PUBLISH_VERSION

上述配置文件里面发布aar的时候配置的

    PUBLISH_GROUP_ID = 'com.keller'

    PUBLISH_ARTIFACT_ID = 'utils_lib'

    PUBLISH_VERSION = '1.0.1'

同步一下项目代码,

至此,这个aar就被远程依赖到项目中了。

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值