Android Studio中把项目的lib库提交到Jcenter仓库中,须要使用到Bintray,Bintray是jCenter的提供商,他支持上传lib到多个平台,jCenter只是众多平台中的一个,形象的说jCenter是位于某地的仓库,Bintray是送货的卡车,你写的库就是货了。android
1.在Bintray上注册帐号,并建立package。 注册bintray,注意:注册时尽可能使用国外的邮箱,避免接收不到验证码。例如我使用雅虎邮箱。git
2.完成注册以后,登陆网站,而后点击maven。 github
3.点击Add New Package,为咱们的library建立一个新的package。 web
4.假设你已经注册帐你并按照上面步骤操做,或者使用我提供的帐号,登录成功后会出现以下界面,点击maven进入该仓库,并点击Add New Package建立新的包。 bash
6.操做AS项目,配置相关信息,命令行操做lib包上传。 Android Studio安装上传Bintray插件和填写相关信息:(下面选用我测试经过而且操做路径最短的方式) 在项目的根build文件中补充以下标红内容 这是根build源文件:maven
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.novoda:bintray-release:+' // 新增
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
tasks.withType(Javadoc) { // 新增
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}复制代码
7.而后在lib的build文件中补充以下内容: 这是lib的源build文件:工具
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release' // 新增
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 2
versionName "1.0.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions { // 新增
abortOnError false
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
publish { // 新增
userOrg = 'huangweicai' // 注册bintray时的username
groupId = 'com.xxx_demo_lib' // 项目包名
artifactId = 'xxx_demo_lib' // 项目名
publishVersion = '1.0.2' // 发布版本号
desc = 'Summarize the tools or methods commonly used in routine development' // 项目描述,可选项
website = 'https://github.com/huangweicai/xxx_demo_lib' // 项目站点,可选项
}复制代码
8.在Android Studio的命令行窗口依次输入以下命令:测试
gradlew generatePomFileForReleasePublication
gradlew publishReleasePublicationToMavenLocal
gradlew bintrayUpload -PbintrayUser=xxx -PbintrayKey=xxx -PdryRun=false
复制代码
其中,PbintrayUser是Bintray的用户名,PbintrayKey是Bintray的API Key。(API Key在注册成功后,能够在修改信息的界面找到,最好在第一次注册成功后就记录好) 等待执行,看到BUILD SUCCESSFUL说明上传Bintray成功。gradle
9.进入Bintray,能够找到咱们上传的包,在页面的左下角看到maven地址说明上传内容正确,第一次在页面的右下角会看到add to jcenter,须要咱们手动点击一下这个add to jcenter按钮,而后等待lib包审核经过后,咱们就能够引用jcenter上的包了。 以上就是Android Studio打包上传到Jcenter的完整流程。
AS引入implementation 'com.xxx_demo_lib:xxx_demo_lib:1.0.2',代码中调用演示工具类TestUtil.test(context);查看吐司是否提示,提示成功说明已经成功发布并引入jcenter包。