将你的代码上传 Bintray 仓库(转)

转自:http://www.cnblogs.com/cpacm/p/5548241.html

在 Android Studio 中,我们通常可以利用 gradle 来导入别人写的第三方库,通常可以简单得使用一句话就能搞定整个导包过程,
比如:

compile 'net.cpacm.moneytext:moneyview:1.0.0'

在这个过程中,Android Studio 会从 Maven 仓库服务器中下载所对应的包。现在比较通用的两个服务器分别为 Jcenter 和 Maven Central。本文主要讲的就是Jcenter了,Android Studio 默认使用的服务器仓库。

Jcenter

Jcenter 是 bintray.com 所使用的 Maven 仓库。与 Maven Central 相比,jcenter 的速度更快,包含的库更多,UI界面更友好,更容易使用,同时 bintray 还支持将 jcenter 上传到 Maven Central 的功能。

 

语法规则

以前使用过 Maven 的可能会比较清楚,导入的语句如何指向仓库中唯一存在的库。

compile 'net.cpacm.simpleslider:library:1.0.0'
compile 'GROUP_ID:ARTIFACT_ID:VERSION'

其中
GROUP_ID 是指包所在的组,比如我包放在 net.cpacm.simpleslider 中, 那么我的 GROUP_ID 就是 net.cpacm.simpleslider
ARTIFACT_ID 是指工程名字,一般在android中都为library
VERSION 代表使用的包的版本。

Bintray 使用

bintray 支持 githubgoogle+ 第三方直接登录。

登录后进入 Maven 仓库建立要使用的包,同时填入相应的对应信息。

Gradle 配置

首先确保你要上传的第三方包是一个 library,作为一个项目的 module 存在。

project gradle

在 project gradle 中加入需要的依赖包

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

    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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

local.properties

在项目的 local.properties 文件里面加入 api-key

bintray.user=YOUR_BINTRAY_USERNAME
bintray.apikey=YOUR_BINTRAY_API_KEY

 在 bintray 网站的个人信息编辑页可以找到。

对了,记得不要把 local.properties 传到 github网站上导致个人信息的泄露。

library gradle

修改 library 的 gradle 信息

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

version = '1.0.0'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

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

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

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
}

group = 'net.cpacm.simpleslider'
install {
    repositories.mavenInstaller {
        pom.project {
            packaging 'aar'
            groupId 'net.cpacm.simpleslider' //自己定义的组名
            artifactId 'library' 

            name 'simpleslider'
            description 'A simple slider allows you to easily use.'
            url 'https://github.com/cpacm/SimpleSlider'
            inceptionYear '2016'

            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    distribution 'repo'
                }
            }
            scm {
                connection 'https://github.com/cpacm/SimpleSlider.git'
                url 'https://github.com/cpacm/SimpleSlider'

            }
            developers {
                developer {
                    name 'cpacm'
                    email 'shenliming@gmail.com'
                }
            }
        }

    }
}

// Bintray

//获取local.propertes的信息
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream()) 

bintray {
    user = properties.getProperty("bintray.user")
    key = properties.getProperty("bintray.apikey")
    publish = true
    configurations = ['archives']
    pkg {
        //填入 bintray 上对应的 package 信息
        repo = 'maven'
        name = 'SimpleSlider'
        vcsUrl = 'https://github.com/cpacm/SimpleSlider.git'
        websiteUrl = 'https://github.com/cpacm/SimpleSlider'
        licenses = ['Apache-2.0']
        issueTrackerUrl = 'https://github.com/cpacm/SimpleSlider/issues'
        publicDownloadNumbers = true
        version {
            name = '1.0.0'
            desc = 'simple slider release'
            vcsTag = '1.0.0'
            attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin']
        }
    }
}

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

task findConventions << {
    println project.getConvention()
}

安装和上传

在Android Studio Terminal窗口中运行命令

gradlew install
gradlew bintrayUpload

 至此已经打包上传结束,可以回到 bintray 网站看看结果。

添加到Jcenter

点击上图的 Add to jcenter 按钮,向管理员申请添加到 jcenter 仓库,一般等个几小时就能通过。
最后在 https://jcenter.bintray.com 找到自己的库就表示成功了。

原文地址:https://www.cnblogs.com/taintain1984/p/6101060.html