Android Studio发布JAR包(Android Library)到 jcenter (bintray)

引言:经常上github的你,会看到如下方式,直接gradle一行代码就能构建依赖包了,从此不用辛辛苦苦地下载 再导入jar包,如此简洁的方式,你是否也想自己的Libraries也能这样被其他网友使用呢?下面几个步骤即可

compile 'com.squareup.picasso:picasso:2.5.2'

这些库其实是放在文件仓库里的,我们是从仓库下载到本地再使用,常用的仓库有jcenter 和 Maven Central,本文章介绍jcenter




一、jcenter是一个由 bintray.com维护的一个仓库,若想发布Librarie到jcenter,我们先得上传到 bintray.com,我们先在这网站注册个帐号,推荐直接用github登录。 然后点击右上角用户头像-Your Profile,

  • Add New Repository
    先创建一个仓库,tpye选Maven,名字任意,不过你要记住名字,后面步骤上传Libraries要对应仓库名字的。

  • 点击用户头像旁边的Edit,复制你的usernameAPI Key信息,后面步骤要用




二、bintray告一段落,现在来新建一个Android PorjectDemo, 之后再新建一个Module ,选择Android Library(这个Library名就是以后你上传的库名),新建完毕,现在来进行build.gradle等的配置

  • Project根目录下的build.gradle需要添加
dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'    
        //版本若更新,自行网上搜索新版本号
    }


  • Module:app下的build.gradle需要添加
dependencies {
    compile project(':yourlibrary')
}


  • Module:yourlibrary目录下的build.gradle需要添加
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
version = "0.0.1"   //这个是你上传到bintray仓库的版本号,以后更新就改这个

底部添加:

def siteUrl = 'https://github.com/yourname/myfirstlibrary'    //Homepage URL of the library  重要,最好在github上也同步了工程
def gitUrl = 'https://github.com/yourname/myfirstlibrary.git'    //Git repository url重要
def issueUrl = 'https://github.com/yourname/myfirstlibrary/issues'    //issue url of the library重要
group = "com.github.yourname"    //这个gruop很重要,参考 compile 'groupId:artifactId:version'     groupId就是这个,artifactId就是包名,version就是顶部那个
install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                // Add your description here
                name 'A custom imageview that can add different style tag on corner'
                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 'yourID' //your user ID 不重要
                        name 'yourName' //your name 不重要
                        email 'your@gmail.com' //your email 不重要
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}
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))
}
task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}
artifacts {
    archives javadocJar
    archives sourcesJar
}
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 = "android"    //发布到Bintray的哪个仓库,这个就是第一步时你在bintray网站建立的仓库名,若这仓库不存在,则会报错
        name = "myfirstlibrary"    //发布到Bintray上的名字,这个是显示在bintray仓库中的项目名字,与包名可同可不同,若仓库没有这个项目文件夹,则会自动创建
        desc = "a library to test."    //项目描述
        websiteUrl = siteUrl   
        vcsUrl = gitUrl
        issueTrackerUrl = issueUrl
        licenses = ["Apache-2.0"]
        labels = ['android']    //标签
        publish = true
        publicDownloadNumbers = true
    }
    javadoc { //jav doc采用utf-8编码否则会报“GBK的不可映射字符”错误
        options {
            encoding "UTF-8"
            charSet 'UTF-8'
        }
    }
}

最后还要配置根目录下的local.properties,添加如下两行:

bintray.user=yourname  //就是你bintray网站的用户名
bintray.apikey=yourkey //就是第一步让你记住的API Key


三、好了配置完成了,现在我们来把library上传到bintray网站

  • 首先build一下项目,若成功编译,就到下一步
  • 打开AS底部的Terminal,输入gradlew bintrayUpload 执行

  • 若等待数分钟就提示BUILD
    SUCCESSFUL,那么恭喜你,说明已经上传成功,此时登录bintray.com
    查看你第一步建立的仓库下,就能看到你上传的library了

  • 若等了好久 一直卡在这提示 Building 98% > :mysecondlibrary:bintrayUpload
    说明网络有问题,那么可以取消重新上传(方法ctrl+c,按y确认)

  • 若是提示BUILD FAILED 也很正常,看错误信息 Could not create package
    ‘yourname/android/mysecondlibrary’: HTTP/1.1 404 Not Found
    [message:Repo ‘android’ was not found]
    仓库不存在,上bintray网站看你新建了仓库没有,或是你build.gradl下方的repo信息没写对

  • 报错信息如下:Could not upload to
    https://api.bintray.com/content/yourname/maven/myfirstlibrary/0.0.2/com/github/yourname/mylibrary/0.0.2/mylibrary-0.0.2-javadoc.jar‘: HTTP/1.1 409 Conflict [message:Unable to u pload files: An artifact with the path’com/github/yourname/mylibrary/0.0.2/mylibrary-0.0.2-javadoc.jar’ already exists

代表文件已经存在了,把版本号改一下 再上传

其他错误信息自行百度


四、library成功上传到bintray后,进入项目所在页面,点击右下角Linked To 旁边的Add To JCenter即可申请发布到jcenter,待审核通过,即可用compile ‘com.squareup.picasso:picasso:2.5.2’ 一句话引用你的库了

若没看到“Add To JCenter”按钮,可能是还没登录帐号呢


完整的Demo下载,只需要输入你在bintray网站注册的name&key(在local.properties文件中),新建仓库名repo(在library目录下的build.gradle底部) 3个信息即可

https://github.com/forgot2015/myfirstlibrary





参考文章:
http://blog.saymagic.cn/2015/02/16/release-library-to-jcenter.html
http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0623/3097.html
http://blog.csdn.net/small_lee/article/details/52328613

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值