Android项目发布自己的库到Jcenter

     Android项目编写了一个通用串口通信库,好东西那当然要分享,以下是发布的主要细节,大家有兴趣的可以试试发布自己的库

Android Studio早期版本使用的是mavenCentral(),后来切换到jcenter()了。既然Android Studio 使用了jcenter作为中央仓库,我们就需要把我们的开发库上传到中央库发布,步骤如下:

第一步:

   申请Bintray账号

   因为JCenter是Bintray公司在维护,所以我们要先注册一个Bintray账号
   https://bintray.com/signup/oss
   :注册账号不能用QQ邮箱和163邮箱(不清楚原因)

 

 第二步:

第三步:

第四步:

将以下代码添加到build.gradle文件中

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

}

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

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

//项目主页
def siteUrl = 'https://github.com/xxxx'
//项目的版本控制地址
def gitUrl = 'https://github.com/xxxx/xxx'

//发布到组织名称名字,必须填写
group = "com.lotsrc.zserialport"
//发布到JCenter上的项目名字,必须填写
def libName = "zserialport"
// 版本号,下次更新是只需要更改版本号即可
version = "1.0.3"
/**  上面配置后上传至JCenter后的编译路径是这样的: compile 'group:libName:version'  **/

//生成源文件
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.author true
    options.version true
    options.links "https://github.com/xxx/xxx"
    failOnError false
}

//文档打包成jar
task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}
//拷贝javadoc文件
task copyDoc(type: Copy) {
    from "${buildDir}/docs/"
    into "docs"
}

//上传到jCenter所需要的源码文件
artifacts {
    archives javadocJar
    archives sourcesJar
}

// 配置maven库,生成POM.xml文件
install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                name 'zserialport'
                url siteUrl
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id 'zhong'
                        name 'zhongjian'
                        email '[email protected]'
                    }
                }
                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 = "zhong"
        name = libName    //发布到JCenter上的项目名字,必须填写
        desc = 'zserialport'    //项目描述
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }

 

第五步:

需要在local.properties文件中配置Bintray用户名和apiKey:

  1. bintray.user=xxx
  2. bintray.apikey=xxxx

 

第六步:

  1. ./gradlew install
  2. ./gradlew bintrayUpload

 

ok,现在到Bintray官网上查看,发现仓库里多了一个package,点击进去:

第七步:
     点击“add to JCenter”,审核过即可,会发邮箱,收到邮件后通过后,只要在需要的项目中引入即可:

源码地址:

https://github.com/zhongjian3344/zserialport

 

发布到jcenter借鉴了下面几篇文章,非常感谢无私分享:
http://blog.bugtags.com/2016/01/27/embrace-android-studio-maven-deploy/
https://blog.csdn.net/zhcswlp0625/article/details/54895584

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章