手把手教你在JCenter发布开源库

本文详细介绍了如何将Android项目上传至GitHub并发布到JCenter的过程,包括注册Bintray账号、创建Maven仓库、配置Gradle插件、设置API key以及解决上传过程中遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  当你自己造好了轮子,想让其他人也能方便的使用到,就好像其他开源库一样,在AndroidStudio中一句话引用:
  

dependencies {
    compile 'chuck.WheelItemView:library:1.0.1'
 }

  这其实不是很难,只需要将你的项目上传Maven,然后发布到JCenter就可以了。

  准备自己的项目
  首先准备好自己的项目,上传至github。哈哈,这个是必须的。

  注册bintray
  Bintray是jcenter的托管商,因此你必须注册一个Bintray账号,注册完账号后记下你的用户名以及API Key。其官网首页:
  

  目前官网的注册入口是公司组织的注册,我们可以直接注册一个个人用户注册地址https://bintray.com/signup/oss

  

  也可以用第三方账号,例如github账号登录。
  
  create Maven仓库
  登录进去之后,如下

  
  
  我们点击Add New Repository增加仓库:

  在Name栏目下填写你的仓库的名字,这个名字可以设置为自己项目的包名,也可以自己定义,但是一定要记住这里的Name,在后边的项目配置中会用到。接着在Type栏目下拉选择Maven。再选择自己使用的开源协议。描述可以随意写一下。最后点create就好了。成功后在你的Owned Repositories栏目下会出现刚刚创建的库。

  项目配置
  首先修改你的项目root目录下的Project:build.gradle文件加入Maven和Jfrog Bintray的依赖插件:
  

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

  这里建议使用Github上这两个插件的最新版本:
  

Maven:https://github.com/dcendents/android-maven-gradle-plugin
Jfrog Bintray:https://github.com/bintray/gradle-bintray-plugin

  然后在配置Library的build.gradle文件,以我之前的开源项目为例:
  

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

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

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

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

//项目主页
def siteUrl = 'https://github.com/hgchenkai/WheelView'
//项目的git地址
def gitUrl = 'https://github.com/hgchenkai/WheelView.git'
//发布到JCenter上的项目名字
def libName = "WheelItemView"

//发布到组织名称名字,必须填写
group = "chuck.WheelItemView"
// 版本号,下次更新是只需要更改版本号即可
version = "1.0.1"
//上面配置后上传至JCenter后的编译路径是这样的: compile 'chuck.WheelItemView:library:1.0.1'

//生成源文件
task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}

//生成Javadoc文档
task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

//文档打包成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 'A Item Of PickerView.'
                url siteUrl
                licenses {
                    license {
                        //开源协议
                        name 'MIT License'
                        url 'http://www.opensource.org/licenses/mit-license.php'
                    }
                }
                developers {
                    developer {
                        //开发者的个人信息
                        id 'hgchenkai'
                        name 'chenkai'
                        email 'hgchenkai437@gmail.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仓库的时候的名字一样
        repo = "maven"
        //发布到JCenter上的项目名字
        name = libName
        //项目描述
        desc = 'A Item Of PickerView.'
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["MIT"]
        publish = true
    }
}

javadoc {
    options {
        //如果你的项目里面有中文注释的话,必须将格式设置为UTF-8,不然会出现乱码
        encoding "UTF-8"
        charSet 'UTF-8'
        author true
        version true
        links "http://docs.oracle.com/javase/7/docs/api"
    }
}

  注意不要忘了最上边的两句话
  

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

  配置Api key
  将你的Bintray中的user和key写到local.properties文件中:

bintray.user=xxxxxx
bintray.apikey=xxxxxxxxxxxxxxxxxxxxxxxx

  这些信息的获取方式:
  这里写图片描述
  
  user就是你的用户名,Api key需要先点击Edit,进入以下界面:
  这里写图片描述

  将Api key粘贴进local.properties中即可,记得在上传github时,对local.properties进行忽略哟。

  上传项目
  打开Android Studio底部工具栏的Terminal窗口,输入命令(也可以在Android Studio右边gradle中选择对应的命令进行执行):
  

gradlew install

  如果是mac或者Linux的话就是

./gradlew install

  我在这里出现了错误
  

Execution failed for task ':wheelview:javadoc'. > Javadoc generation failed. Generated Javadoc options file (useful for troubleshooting):'xx/javadoc/javadoc.options'

  需要在Project:build.gradle文件中加入:
  tasks.getByPath(“:xxxx:javadoc”).enabled=false
  xxxx需要根据上边错误来填写,我这里就是wheelview,根据自己的情况来添加吧。
  解决这个问题之后就可以install成功了,接着就去执行以下命令:
  

gradle bintrayUpload

  类似的mac或者linux:

./gradle bintrayUpload

  如果不出意外可以上传成功。

  发布到JCenter
  这时候打开我们创建的仓库,就会在里面发现已经成功将项目上传到仓库里。
  这里写图片描述
  点击对应的项目,将会进入如下界面:
  这里写图片描述
  
  如果还没有发布到JCenter中,那么在Linked to那一栏会显示Add to JCenter,直接点击,会进入到如下页面:
  这里写图片描述
  Send成功后,就静静的等待审核吧,一般几个小时就可以搞定,批准之后你会收到消息和邮件,这样就可以引用了。
  如果有什么坑,可以参考:
  Android 发布项目到 JCenter 遇到的各种坑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值