这篇博客记录了如何将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就被远程依赖到项目中了。