使用Gradle发布项目到JCenter仓库

如果想在AndroidStudio中直接使用别人的项目或者是库,一般的做法是在dependencies中进行compile,而compile的东西就是存放在JCenter或者Maven仓库中的。

在这里我上传到JCenter,因为现在默认是支持JCenter,同时JCenter也兼容Maven

注册Bintray账号

简单注册就好,Bintray传送门, 也可以选择github或者其他方式登陆

生成项目的JavaDoc和source JARs并上传

存放在JCenter仓库中的东西就是JavaDoc和sourceJARs.

1. 在根build.gradle中:
添加后两行classpath就可以了,第一行不用变

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

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

2. 在需要上传的Module下的build.gradle中:
第一行,如果是library就改为com.android.library。
后面两行必须的。

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

version = "1.0.0"

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    resourcePrefix "" //随便填

    defaultConfig {
        applicationId "com.customwidget.lzqwidget"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.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'
    compile 'com.android.support:design:23.1.1'
}

// 下面的可以全部复制,然后更改为自己的就好了
def siteUrl = 'https://github.com/LiZHongquan2013/CustomWidget' // 项目主页
def gitUrl = 'https://github.com/LiZHongquan2013/CustomWidget.git' // 项目git的clone地址
group = "com.customwidget.lzqwidget" // Maven Group ID for the artifact 一般为包名
install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                // Add your description here
                name 'Android CustomWidget' //项目描述
                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 'LiZHongquan2013'
                        name 'LiZHongquan2013'
                        email 'JonsTank2013@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) {
    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
}
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 {
        repo = "maven"
        name = "CustomWidget"
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}

3. 在local.properties中:

API Key可以点击自己的头像,选择your profile->点击edit选项->选择API Key

bintray.user=填写你的Bintray用户名
bintray.apikey=填写你的Bintray API Key

这个文件一般是需要是.gitignore中写入的,避免信息泄露
默认.gitignore中是包含了它,如果没有,就需要手动加入

4. 生成
Rebuild一下,然后 ./gradlew install

5. 上传到Bintray
./gradlew bintrayUpload

上传完成即可在Bintray网站上找到你的Repo,我们需要完成最后一步工作,申请你的Repo添加到JCenter。可以进入这个页面,点击include my package, 输入你的项目名字点击匹配到的项目,然后写一写Comments再send即可,然后就等管理员批准了,我是大概等了40分钟,然后网站上会给你一条通过信息,然后就OK了,大功告成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值