使用Gradle发布Android Library到JCenter

1,准备工作
https://bintray.com/网站申请账号

注意不要选择这个start your free trial进行账号注册,这个是有商业组织的,个人应该选择下面这个链接进行注册(可以是有GitHub以及Google账号进行登录)

注册完成后目前最新页面登录效果

到这里注册完成了,下面要记录注册后的用户名比如我的用户名就是右上角头像旁边显示的alian,点开用户名旁边的下拉菜单,选择EditProfile选项

进入到如下

点开API Key获取你的key
然后再打开用户旁的下拉菜单进入View Profile

打开 Add New Repository 创建个人的仓库
其中Name就是你的仓库名,记住后面需要使用,Type选择Maven

综合上面的准备工作主要记住三个数据
用户名、API Key和仓库名
2,本地工作
下面就是在Android Studio做建库工作
新建一个Android项目,比如我的名字是Pager

其中,ScrollPager是我们建立的Library,这里主要就是一些参数的配置。

Pager目录下面的build.gradle内容如下:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath ‘com.android.tools.build:gradle:2.2.3’

    // 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

}

ScrollPager目录下的build.gradle内容如下:

apply plugin: ‘com.android.library’

//配置插件
apply plugin: ‘com.github.dcendents.android-maven’
apply plugin: ‘com.jfrog.bintray’
version = “1.1.1” //这个是版本号,必须填写

def siteUrl = ‘https://github.com/wanghoulian/Pager’ // 项目的主页
def gitUrl = ‘https://github.com/wanghoulian/Pager.git‘//Git仓库的url
group = “com.nextev.scroll”

ext{
bintrayRepo = ‘nio’//步骤一中记录的仓库名
bintrayName = ‘ScrollPager’//需要以后展示的库名称
publishedGroupId = ‘com.nextev.scroll’
libraryName = ‘ScrollPager’
artifact = ‘ScrollPager’
libraryDescription = ‘ScrollView like pager’
siteUrl = ‘https://github.com/wanghoulian/Pager
gitUrl = ‘https://github.com/wanghoulian/Pager.git
libraryVersion = ‘1.0.1’
developerId = ‘alian’
developerName = ‘alian’
developerEmail = ‘wanghoulian@qq.com’
licenseName = ‘The Apache Software License, Version 2.0’
licenseUrl = ‘http://www.apache.org/licenses/LICENSE-2.0.txt
allLicenses = [“Apache-2.0”]
}

android {
compileSdkVersion 25
buildToolsVersion “25.0.1”

resourcePrefix "alian_"     //这里随便填

defaultConfig {
    minSdkVersion 19
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

lintOptions {
    abortOnError false
}

}

dependencies {
compile fileTree(dir: ‘libs’, include: [‘*.jar’])
androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2’, {
exclude group: ‘com.android.support’, module: ‘support-annotations’
})
compile ‘com.android.support:appcompat-v7:25.0.1’
testCompile ‘junit:junit:4.12’
}

install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging ‘aar’
// Add your description here
name ‘multi-format 1D/2D barcode image processing use zxing.’
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 ‘alian’ //填写的一些基本信息
name ‘alian’
email ‘wanghoulian@qq.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(‘gradle.properties’).newDataInputStream())
bintray {
user = properties.getProperty(“bintray.user”)
key = properties.getProperty(“bintray.apikey”)
configurations = [‘archives’]
pkg {
repo = “nio”//步骤一中记录的仓库名
name = “ScrollPager” //发布到JCenter上的项目名字
userOrg = “alian”
artifact = “ScrollPager”
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = [“Apache-2.0”]
publish = true
}
}
javadoc { //jav doc采用utf-8编码否则会报“GBK的不可映射字符”错误
options{
encoding “UTF-8”
charSet ‘UTF-8’
}

}

如果使用Pager目录下的gradle.properties作为你的user和apikey配置文件就把你的信息按照如下格式写在gradle.properties里面

Project-wide Gradle settings.

IDE (e.g. Android Studio) users:

Gradle settings configured through the IDE will override

any settings specified in this file.

For more details on how to configure your build environment visit

http://www.gradle.org/docs/current/userguide/build_environment.html

Specifies the JVM arguments used for the daemon process.

The setting is particularly useful for tweaking memory settings.

org.gradle.jvmargs=-Xmx1536m

When configured, Gradle will run in incubating parallel mode.

This option should only be used with decoupled projects. More details, visit

http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects

org.gradle.parallel=true

bintray.user=步骤一中记录的用户名

bintray.apikey=步骤一中记录的API Key

自此本地工作已经准备完成,可以写一些代码在你的Library库的java文件里了
3,编译工作
上面工作完成后,下面就要进行编译库文件了,这里题外话就是配置好你的gradle到你的系统PATH路径下面,各个系统不一样,请自行百度,我的是Mac。
配置完成后,可以在命令行或终端进入你的项目目录下,这里因为使用的是Android Studio可以很方便的使用自带的Terminal就可以了,分别执行:
gradle clean
gradle build
gradle bintrayUpload
正常情况下就可以将你的Library发布到JCenter了,可能会有其他的意外情况,基本上通过谷歌或者百度搜索都能完美解决,发布完后如下图:

点开你的库名称,比如我的ScrollPager,进入到下面的页面

第一次需要提出申请,才能加入到jcenter,一般都是很快的,这样以后你或者他人就可以使用gradle的方式引入你的库里!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值