android同步aar到jcenter,详解Android Studio 上传aar(Library)到JCenter

1、 注册

a095c678f8fe

regeist.png

在 Bintray上注册一个私人账户。有两种注册方式,选择第二种,For an open Source Account。

2 获取api key

a095c678f8fe

edit_pro.png

点击 api key,输入登录密码,获取api key:

a095c678f8fe

edit_api.png

a095c678f8fe

edit_api_get.png

3创建maven仓库

a095c678f8fe

create_maven.png

a095c678f8fe

create_maven_result.png

maven 仓库配置好后,可以开始配置build.gradle

4 根目录build.gradle

首先配置根目录的build.gradle,在dependencies中添加jcenter插件

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'

classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'

a095c678f8fe

root_build.png

5 配置module 的build.gradle

配置完根目录的build后,然后配置需要上传jcenter 的module的build

根节点配置plugin

//上传jcenter

apply plugin: 'com.github.dcendents.android-maven'

apply plugin: 'com.jfrog.bintray'

根节点配置version

version = 0.0.1

定义相关网址:

def siteUrl = 'https://github.com/johnnycmj/appfactory' // project homepage

def gitUrl = 'https://github.com/johnnycmj/appfactory.git' // project git

def issueUrl = 'https://github.com/johnnycmj/appfactory/issues'

定义group

一般在gradle中引用库是这样的。compile 'io.reactivex:rxjava:1.2.9',其中这个aar用:分3个部分,第一个部分是group,第二个部分是 name,第三就是版本。

group = com.smart.app

上传到jcenter至少需要四个文件,除了打包的aar之外,还需要pom和javadoc,source,否则是通不过jcenter的审核。这些我们都可以用脚本生成。

打包javadocjar和sourcejar

//打包javadocjar和sourcejar

task sourcesJar(type: Jar) {

from android.sourceSets.main.java.srcDirs

classifier = 'sources'

}

task javadoc(type: Javadoc) {

//增加编码,这行很重要,如果是windows系统,同时如果在代码中有中文的话,不加这行,上传会报错

options.encoding="utf-8"

source = android.sourceSets.main.java.srcDirs

classpath += project.files(android.getBootClasspath().join(File.pathSeparator))

}

task javadocJar(type: Jar, dependsOn: javadoc) {

classifier = 'javadoc'

from javadoc.destinationDir

}

artifacts {

archives javadocJar

archives sourcesJar

}

定义pom并打包aar

//定义pom并打包aar

install {

repositories.mavenInstaller {

// This generates POM.xml with proper parameters

pom {

project {

packaging 'aar'

name 'Appfactory For Android'

url siteUrl

// Set your license

licenses {

license {

name 'The Apache Software License, Version 2.0'

url 'http://www.apache.org/licenses/LICENSE-2.0.txt'

}

}

developers {

developer { //填写的一些基本信息

id 'johnnycmj'

name 'johnny.chan'

email 'chmj1208@163.com'

}

}

scm {

connection gitUrl

developerConnection gitUrl

url siteUrl

}

}

}

}

}

上传到Jcenter仓库

上传到jcenter的网站Bintray,需要用户验证:

一般这种属于私有参数,我们配置在local.properties,在git上传时用git.ignore忽略上传。

bintray.user=用户名

bintray.apikey=之前获取的apikey

//配置bintray参数

Properties properties = new Properties()

properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {

user = properties.getProperty("bintray.user")

key = properties.getProperty("bintray.apikey")

configurations = ['archives']

pkg {

repo = "maven" //跟上面创建的Maven仓库名字保持一致

name = POM_ARTIFACT_ID //发布到JCenter上的项目名字

websiteUrl = siteUrl

vcsUrl = gitUrl

licenses = ["Apache-2.0"]

publish = true

}

}

完全配置

//上传jcenter

apply plugin: 'com.github.dcendents.android-maven'

apply plugin: 'com.jfrog.bintray'

android {

compileSdkVersion 25

buildToolsVersion "25.0.3"

defaultConfig {

minSdkVersion 15

targetSdkVersion 23

versionCode 1

versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

android {

lintOptions {

abortOnError false

}

}

buildscript {

repositories {

jcenter()

}

}

dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:25+'

compile'com.android.support:support-v4:25+'

compile'com.android.support:support-annotations:25+'

compile'com.android.support:design:25+'

compile 'com.fasterxml.jackson.core:jackson-core:2.1.4'

compile 'com.fasterxml.jackson.core:jackson-databind:2.1.4'

}

version = "0.0.1"

def siteUrl = 'https://github.com/johnnycmj/appfactory' // project homepage

def gitUrl = 'https://github.com/johnnycmj/appfactory.git' // project git

def issueUrl = 'https://github.com/johnnycmj/appfactory/issues'

group = "com.smart.app"

//打包javadocjar和sourcejar

task sourcesJar(type: Jar) {

from android.sourceSets.main.java.srcDirs

classifier = 'sources'

}

task javadoc(type: Javadoc) {

//增加编码,这行很重要,如果是windows系统,同时如果在代码中有中文的话,不加这行,上传会报错

options.encoding="utf-8"

source = android.sourceSets.main.java.srcDirs

classpath += project.files(android.getBootClasspath().join(File.pathSeparator))

}

task javadocJar(type: Jar, dependsOn: javadoc) {

classifier = 'javadoc'

from javadoc.destinationDir

}

tasks.getByPath(":appfactory:javadoc").enabled = false

artifacts {

archives javadocJar

archives sourcesJar

}

//定义pom并打包aar

install {

repositories.mavenInstaller {

// This generates POM.xml with proper parameters

pom {

project {

packaging 'aar'

name 'Appfactory For Android'

url siteUrl

// Set your license

licenses {

license {

name 'The Apache Software License, Version 2.0'

url 'http://www.apache.org/licenses/LICENSE-2.0.txt'

}

}

developers {

developer { //填写的一些基本信息

id 'johnnycmj'

name 'johnny.chan'

email 'chmj1208@163.com'

}

}

scm {

connection gitUrl

developerConnection gitUrl

url siteUrl

}

}

}

}

}

//上传到Jcenter仓库

//配置bintray参数

Properties properties = new Properties()

properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {

user = properties.getProperty("bintray.user")

key = properties.getProperty("bintray.apikey")

configurations = ['archives']

pkg {

repo = "maven" //跟上面创建的Maven仓库名字保持一致

name = appfactory //发布到JCenter上的项目名字

websiteUrl = siteUrl

vcsUrl = gitUrl

licenses = ["Apache-2.0"]

publish = true

}

}

配置都配完,准备上传bintrary,在androidstudio 中的 terminal 命令窗中输入:

gradle install

gradle bintrayUpload

如果顺利的话就会上传成功,不过这边会有很多坑。

坑一

生成javadoc会有一大堆错误,有些是因为我们写注释的时候没有按javadoc的标准来写,所有生成的时候会报错。

a095c678f8fe

fault_doc.png

Lint 检查默认是开启的,Lint 会检查项目中的语法错误,如果没有通过则无法继续。只需要在 Module 的 build.gradle 添加如下代码:

android {

lintOptions {

abortOnError false

}

}

然后在去掉javadoc的task

tasks.getByPath(":appfactory:javadoc").enabled = false

其中":appfactory:javadoc" appfactory是你要上传的module名称。这两步之后即可解决上面问题。

坑二

出现 HTTP/1.1 401 Unauthorized

a095c678f8fe

fault_auth.png

这个是用户名和apikey错误,导致无权限,检查user和 apikey即可。

坑三

出现 HTTP/1.1 404 Not Found

a095c678f8fe

fault_404.png

检查repo的名称是否跟bintrary上创建的一样

坑四

无法引用,上传jcenter成功无法引用,由于,上传到jcenter会有一段审核时间,在审核通过后可以用一般的引用,还未审核的时候会报错误:

a095c678f8fe

error.png

解决:

在根build中加上自己的jcenter地址:

allprojects {

repositories {

jcenter()

maven{

url "https://dl.bintray.com/johnnychmj/maven"

}

}

}

然后编译即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值