用 Gradle 发布项目到 JCenter

一、注册Bintray账号###

企业版个人版
https://bintray.comhttps://bintray.com/signup/oss
收费免费
官网默认注册页Can 'Add to JCenter'

二、创建Maven Repositories###

登录进入首页,点击Add New Repository

《用 Gradle 发布项目到 JCenter》

填写配置

《用 Gradle 发布项目到 JCenter》

生成Maven

《用 Gradle 发布项目到 JCenter》

三、获取 API Key###

《用 Gradle 发布项目到 JCenter》 APIKey

四、添加插件依赖###

在工程的build.gradle添加(项目最外层)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

五、配置Username、APIKey###

打开local.properties并配置

bintray.user=Bintray Username //注册时的用户名
bintray.apikey=Bintray API Key //前面获取的API key

六、配置Gradle###

打开要上传的modulebuild.gradle配置如下。配置完成后Rebuild工程。

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

version = "0.0.1"  //版本号

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.0.0'
}

def siteUrl = 'https://github.com/huang125/RippleView'    // Git项目主页
def gitUrl = 'https://github.com/huang125/RippleView.git' // Git仓库url
group = "com.huang.rippleview" // 一般为包名

install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                name 'Android RippleView' //项目描述
                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 'huang'    // 可以写昵称
                        name 'huang'
                        email 'vip125huang@gmail.com'
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}
task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}

task javadoc(type: Javadoc) {
    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
}
//在local.properties添加bintray.user和bintray.apikey
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 {
        userOrg = "vip125huang"//bintray用户名
        repo = "maven"         // 发布到Maven库
        name = "RippleView"    // 发布到JCenter上的项目名
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}

七、上传到Maven库###

Android StudioTerminal执行如下命令:

gradlew javadocJar
gradlew sourcesJar
gradlew install
gradlew bintrayUpload

全部BUILD SUCCESSFUL,说明项目上传成功。
进入Maven库查看。

《用 Gradle 发布项目到 JCenter》

八、Add to JCenter###

进入Maven库,点击刚上传的项目,进入项目主页。点击右下角Add to jCenter按钮。

《用 Gradle 发布项目到 JCenter》

随意填写项目描述,
send发送。

《用 Gradle 发布项目到 JCenter》

发送成功后,等待2~3小时审核,审核成功会有邮件通知。

《用 Gradle 发布项目到 JCenter》

九、项目更新###

对库进行修改后,可直接执行命令(步骤七)上传更新。

注意:
本地项目更新后,须更改build.gradle下的version、versionCode、versionName,否则无法更新。

十、问题###

1、HTTP/1.1 401 Unauthorized [message:This resource requires authentication]
apikey 或用户名错误。

2、HTTP/1.1 404 Not Found [message:Repo 'android' was not found]
仓库不存在,需建立Maven库,参考步骤二。

十一、巨人的肩膀###

如何使用Android Studio把自己的Android library分享到jCenter和Maven Central
利用Gradle发布项目到JCenter、Maven
Android上传开源项目(Library)到Jcenter

    原文作者:敲代码的本愿
    原文地址: https://www.jianshu.com/p/dd1400013439
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞