Android 开发

[Android]上传library到Jcenter简记

2018-06-28  本文已影响27人  天神Deity

在项目的build.gradle中添加以下配置:

dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        //上传到jcenter中需要用到的依赖库
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
    }

在library项目的build.gradle中添加以下配置:

//上传到jcenter配置

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

// 声明当前的版本号
version = "1.0.0"
//定义当前library的站点
def siteUrl = 'https://github.com/MeDeity/LibAndroidCommon'    // project homepage
def gitUrl = 'https://github.com/MeDeity/LibAndroidCommon.git' // project git

//定义组织,这样引用该library就变成:compile 'com.deity.common:common:1.0.0‘
group = "com.deity.common"

//上传到jcenter至少需要四个文件,除了打包的aar之外,还需要pom和javadoc,source,否则是通不过jcenter的审核。这些我们都可以用脚本生成。

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

//生成文档
task javadoc(type: Javadoc) {
    //编码GBK的不可映射字符->请正确配置javadoc编码
    options.encoding "UTF-8"
    options.charSet 'UTF-8'
    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
}

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

// 根节点添加
install {
    repositories.mavenInstaller {// This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                name 'Common For Android'
                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 'lastDeity'
                        name 'wenhua.feng'
                        email 'langrenbule@gmail.com'
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}


//配置bintray参数
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
    user = properties.getProperty("bintrayUser")
    key = properties.getProperty("bintrayApiKey")
    configurations = ['archives']
    pkg {
        repo = "Android-lib"           //跟jcenter上创建的Maven仓库名字保持一致
        name = "LibAndroidCommon"      //发布到JCenter上的项目名字
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}

在local.properties中配置 jcenter的用户信息及apikey

bintrayUser = lastdeity
bintrayApiKey = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

执行命令上传library

gradlew install
gradlew bintrayUpload

到jcenter中查看是否上传成功


上传结果验证

提交审核,并等待审核通过


提交审核并等待审核通过
上一篇下一篇

猜你喜欢

热点阅读