Android将自己写的开源代码上传到jcenter

在安卓开发中我们经常可以看到在gradle中引入compile来引入第三方库,那么这到底是怎么回事?这个是Android项目所在jcenter的一个远程仓库。我们只需要将你的libray上传到jcenter即可,那么如何将一个项目上传到jcenter呢?

1.去jcenter官网注册个账号:https://bintray.com

注册界面如下图:

《Android将自己写的开源代码上传到jcenter》

2.在项目根目录的build.gradle(Project)添加配置

在如下图中的位置加入如下配置

classpath’com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3′

classpath”org.jfrog.buildinfo:build-info-extractor-gradle:4.0.0″

classpath’com.github.dcendents:android-maven-gradle-plugin:2.1′

《Android将自己写的开源代码上传到jcenter》

3.在你要上传的module的build.gradle中添加如下配置

在如上图中的位置加入如下配置:配置中都有详细的注释是什么作用。

/** 以下开始是将Android Library上传到jcenter的相关配置**/

applyplugin:’com.github.dcendents.android-maven’

applyplugin:’com.jfrog.bintray’

//项目主页

def siteUrl =’https://github.com/czl0325/ZLDotProgressbar-android’

//项目的版本控制地址

def gitUrl =’https://github.com/czl0325/ZLDotProgressbar-android.git’

//发布到组织名称名字,必须填写

group =”com.github.czl0325″

//发布到JCenter上的项目名字,必须填写

def libName =”ZLDotProgressbar”

// 版本号,下次更新是只需要更改版本号即可

version =”0.0.1″

/**  上面配置后上传至jcenter后的编译路径是这样的:compile ‘cn.bluemobi.dylan:sqlitelibrary:1.0’  **/

//生成源文件

task sourcesJar(type: Jar) {

from android.sourceSets.main.java.srcDirs

classifier =’sources’

}

//生成文档

task javadoc(type: Javadoc) {

source = android.sourceSets.main.java.srcDirs

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

options.encoding”UTF-8″

    options.charSet’UTF-8′

    options.authortrue

    options.versiontrue

    options.links”https://github.com/linglongxin24/FastDev/tree/master/mylibrary/docs/javadoc”

    failOnErrorfalse

}

//文档打包成jar

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

classifier =’javadoc’

    from javadoc.destinationDir

}

//拷贝javadoc文件

task copyDoc(type: Copy) {

from”${buildDir}/docs/”

    into”docs”

}

//上传到jcenter所需要的源码文件

artifacts {

archivesjavadocJar

    archivessourcesJar

}

// 配置maven库,生成POM.xml文件

install {

repositories.mavenInstaller {

// This generates POM.xml with proper parameters

        pom {

project {

packaging’aar’

                name’a dot progressbar framework’

                url siteUrl

licenses {

license {

name’a dot progressbar framework’

                        url’https://github.com/czl0325/ZLDotProgressbar-android’

                    }

}

developers {

developer {

id’czl’

                        name’czl0325′

                        email’295183917@qq.com’

                    }

}

scm {

connection gitUrl

developerConnection gitUrl

url siteUrl

}

}

}

}

}

//上传到jcenter

Properties properties =new Properties()

properties.load(project.rootProject.file(‘local.properties’).newDataInputStream())

bintray {

user = properties.getProperty(“bintray.user”)//读取 local.properties 文件里面的bintray.user

    key = properties.getProperty(“bintray.apikey”)//读取 local.properties 文件里面的bintray.apikey

    configurations = [‘archives’]

pkg {

repo =”maven”

        name =”ZLDotProgressbar”    //发布到JCenter上的项目名字,必须填写

        desc =’a dot progressbar framework’    //项目描述

        websiteUrl = siteUrl

vcsUrl = gitUrl

licenses = [“Apache-2.0”]

publish =true

    }

}

4.在你项目根目录的local.properties中添加用户名和API Key

在如下图中的位置先找到自己的APIkey:登录到Jcenter官网->Edit Profile->API Key

《Android将自己写的开源代码上传到jcenter》

在如下图中的位置加入如下配置

gradle 

bintray.user=linglongxin24 

bintray.apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 

5.执行命令

(1)生成maven库所需要的POM文件:gradlew install

gradlew install

如果查看到BUILD SUCCESSFUL说明POM文件已经生成成功,并且在你的Android studio的Project视图中可以看到javadoc和javasource的jar包文件

请注意,mac电脑下是使用

gradle install

没有了w,在mac电脑下要从终端启动,进入项目目录,先查看adb devices 和 gradle -v版本,如果没有安装请用brew update gradle安装。

(2)上传你的Library库到jcenter的maven库:graedlew bintrayUpload

gradlew bintrayUpload

1

如果查看到BUILD SUCCESSFUL说明你的Library已经成功上传到了jcenter的maven库

6.查看上传的maven库,并提交审核

登录jcenter首页->点击下方maven即可进入到你上传的maven库列表界面

在maven库列表界面找你你刚才上传的maven库点进去即可看到相关的项目详情

在你上传的maven库详情界面最下面可以看到:Add to JCenter 按钮

点击Add to JCenter 按钮填写项目描述即可提交。这个网站在国外维护,由于时差的原因一般他们上班时间在我们的晚上,等到第二天即可通过审核

第二天查看通过审核的界面

7.遇到的各种坑以及解决办法

(1)坑一:错误: 编码GBK的不可映射字符->请正确配置javadoc编码

java//生成文档      

 task javadoc(type:Javadoc){

options.encoding”UTF-8″

options.charSet’UTF-8′     

 }

(2)坑二:错误: 不允许使用自关闭元素->请删除javadoc注释里面所有的含有html标签, 

(3)坑三: 错误: 程序包android.support.v7.widget不存在;错误: 找不到符号 ->在javadoc中加入忽略错误配置

(4)坑四:Could not create version ‘0.1’: HTTP/1.1 401 Unauthorized [message:This resource requires authentication],->没有配置正确的API Key

(5)坑五:没有有效的POM文件->一定要按步骤执行并没有配置正确的API Key:

(6)坑六:没有Add to JCenter按钮:

注意:在这个地址注册:https://bintray.com/signup/oss;不是https://bintray.com/signup;这两个地址不一样的!

如果后面还有什么问题,可以一起交流。

最后如果还有其他问题推荐查看这篇文章,写的很详细 

Android快速发布项目到jcenter :http://blog.csdn.net/zhcswlp0625/article/details/54895584

8.GitHub

    原文作者:厦门第一帅哥
    原文地址: https://www.jianshu.com/p/06898151e593
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞