Android Studio 上传 Library 至 Jcenter 生成依赖的两种方式

989 阅读5分钟

之前感觉 Studio 中直接使用 compile ‘xxxxxxx’ 感觉挺方(装)便(逼)的。然后网上翻阅了老多的资料,不停的去尝试。虽然当时也成功了,后来才发现原来账号都注册错了(注册成组织账号了 /尴尬)。这里整理一下上传的完整步骤。

注册

Bintray官网首页默认注册是组织 , 个人的正确注册地址是:bintray.com/signup/oss

这里注意一点 不能使用国内的邮箱注册。 也可以使用 Google账号,Github账号 关联登录。

获取 Key

图片出处

这里写图片描述

这里写图片描述

创建个人Maven 仓库

这里写图片描述

注意:这里创建的maven仓库名字如果是 maven 那么可以使用以下两种配置方式的任意一种 ,如果自定义仓库名字非 maven 需要通过第二种配置进行上传,否则存在 404。

这里写图片描述

上传之前的配置

配置方式一仓库名必须为 maven):

项目根目录 build.gradle 中完整配置代码:


buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
        classpath 'com.novoda:bintray-release:0.3.4'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
    // 解决 Execution failed for task':[YourLibraryName]:mavenAndroidJavadocs'.
    tasks.withType(Javadoc) {
        options.addStringOption('Xdoclint:none', '-quiet')
        options.addStringOption('encoding', 'UTF-8')
    }
}

在app 和 library 的 build.gradle 文件中加入以下代码解决 Execution failed for task ‘:core:lint’ :

android{
    ...
    ...
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
}

在 library 的 build.gradle 加入一些配置 :

apply plugin: 'com.novoda.bintray-release'  // 新增
...
android{
    ...
    ...
    lintOptions... 省略
}
publish {
    userOrg = 'xxx'//bintray.com注册的用户名
    groupId = 'com.lfq'//jcenter上的路径
    artifactId = 'customrepo'//上传到 Jcenter 的项目名称
    publishVersion = '1.0.1'//版本号
    desc = ''//选填
    website = ''//选填;可以填写你 Github 上的当前项目地址。
}

此配置的最终生成结果格式为:

compile 'com.lfq:module名称:1.0.1'

生成的依赖的组成格式 ‘groupId : module名称(非配置属性):版本号’

配置方式二自定义仓库名):

在Project 的 build.gradle 中添加 Maven 和 Jfrog Bintray 的依赖

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        //添加下面两行
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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

版本号这里目前是最新的,后续如果有更新,可以去查看 MavenJfrog Bintray 的最新版本。

在 module 的 builde.gradle 中进行配置

apply plugin: 'com.android.library'
//添加这两行
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'


android {
    compileSdkVersion 24
    buildToolsVersion '25.0.0'

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 22
        version 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    //添加配置
    lintOptions {
//        checkReleaseBuilds false
        // Or, if you prefer, you `can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:24.2.1'
    provided 'com.github.bumptech.glide:glide:3.7.0'
}

//项目主页
def siteUrl = 'https://github.com/lvfaqiang/Multi-Image-Selector'
//项目的git地址
def gitUrl = 'git@github.com:lvfaqiang/Multi-Image-Selector.git'

def libName = "MultiImageSelector"; // 上传到 Bintray 的 package 名称。
group = "me.lfq";
version = "1.0.1"
// 这两个参数配置是为了最终生成 compile 'me.lfq:依赖库的名称:1.0.0'  group  version 是关键字,自动识别的。只需配置好就行。

install {
    repositories.mavenInstaller {
        // 生成pom.xml和参数
        pom {
            project {
                packaging 'aar'
                // 项目描述,复制我的话,这里需要修改。
                name 'MultiImageSelector'// 可选,项目名称。
                description ''// 可选,项目描述。
                url siteUrl // 项目主页,这里是引用上面定义好。

                // 软件开源协议,现在一般都是Apache License2.0吧,复制我的,这里不需要修改。
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }

                //填写开发者基本信息,复制我的,这里需要修改。
                developers {
                    developer {
                        id 'lvfaqiang' // 开发者的id。
                        name 'lvfaqiang' // 开发者名字。
                        email 'lvfaqiang@gmail.com' // 开发者邮箱。
                    }
                }

                // SCM,复制我的,这里不需要修改。
                scm {
                    connection gitUrl // Git仓库地址。
                    developerConnection gitUrl // Git仓库地址。
                    url siteUrl // 项目主页。
                }
            }
        }
    }
}

//上传到JCenter
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {
    user = properties.getProperty("bintray.username")    //读取 local.properties 文件里面的 bintray.user 登录用户名。
    key = properties.getProperty("bintray.apikey")   //读取 local.properties 文件里面的 bintray.apikey
    configurations = ['archives']
    pkg {
        //这里的repo值必须要和你创建Maven仓库的时候的名字一样
        repo = "custom"
        //发布到JCenter上的项目名字
        name = libName
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true //是否是公开项目。
    }
}

// 生成jar包的task,不需要修改。
task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier = 'sources'
}
// 生成jarDoc的task,不需要修改。
task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    // destinationDir = file("../javadoc/")
    failOnError false // 忽略注释语法错误,如果用jdk1.8你的注释写的不规范就编译不过。
}
// 生成javaDoc的jar,不需要修改。
task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}
artifacts {
    archives javadocJar
    archives sourcesJar
}

以上需要修改的地方也就配置的几个项目主页地址,项目描述,以及开发者个人信息。仓库名这里要对应你在 Bintray上创建的自定义仓库名。

在 app 的 build.gradle 中需要添加的配置

android{
    ...
    ...
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
}

在 local.properties 中添加个人的参数值:

bintray.username= bintray注册的用户名
bintray.apikey= 文章开头获取的 apikey

配置基本上也就这些了。

上传

基本就是以上所描述的这些配置,接下来我们打开 Studio 的控制台 Terminal .
windows 环境下输入:

gradlew clean build bintrayUpload -PbintrayUser=BINTRAY_USERNAME -PbintrayKey=BINTRAY_KEY -PdryRun=false

Mac OS 环境下输入:
如果出现拒绝该命令./gradlew: Permission denied,可以先运行 chmod +x gradlew再运行该命令;

./gradlew clean build bintrayUpload -PbintrayUser=BINTRAY_USERNAME -PbintrayKey=BINTRAY_KEY -PdryRun=false

上面命令中 BINTRAY_USERNAME 是你在 bintray 上注册的用户名。BINTRAY_KEY 是上面注册的时候所获取的 key. 替换了用户名和 API key 回车执行,等到控制台最终输出 BUILD SUCCESSFUL 就表明项目上传成功。

发布到 Jcenter

这个时候回到 bintray 我们的 maven 仓库中,进入我们刚上传成功的 packge 。
这里写图片描述
点击左下角红框区域,进入下一页 直接点击 send 等待审核,耐心等待审核通过之后,就可以直接通过 compile 引入项目使用。

在审核通过之前,我们可以通过配置上图右上角红框区域链接来使用,在根目录中添加:

allprojects {
    repositories {
        jcenter()
        maven {url '右上角的链接'}
    }
}

项目中加入依赖:

compile 'com.lfq:module名称:1.0.1'    // 模拟类型

注意:如果这里依赖不成功,可以再后边加上@arr

compile 'com.lfq:module名称:1.0.1@arr'

以上两种配置方式,我都亲测过,如果还有什么问题,还望各位指出。我也好做出调整。谢谢!

参考博客:
blog.csdn.net/wzgiceman/a…
blog.csdn.net/xingshen58/…
blog.csdn.net/yanzhenjie1…