以前我自己整理的工具类会打一个jar包,然后到处复制。自从IDE换到AS后,使用gradle导入其他工程,只要简单的一行代码,甚至还有界面化搜索功能,实在是好用的很,所以最近整理了一些工具类就想做成这种方式。于是查了些资料得知,JCenter现在是Android Studio中repositories的默认节点,所以要把项目上传到JCenter这个远端仓库,https://bintray.com/网站管理着JCenter仓库。
由于上传项目时,遇到了一些小问题,我根据网上的资料,整理一下这个过程。
配置Gradle
Module的build.gradle:
apply plugin: 'com.android.library' //把application改成library
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
//applicationId "包名" //需要注释掉applicationId,因为在rebuild的时候报错,具体原因可能是因为第一行改成library的原因,不是很清楚。
minSdkVersion 9
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.3.0'
compile 'com.android.support:design:23.3.0'
}
//以上部分基本是由IDE生成
//添加插件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
//定义版本
version = "0.0.1"
//定义相当网站
def siteUrl = 'https://github.com/gitusername/projectname' // project homepage
def gitUrl = 'https://github.com/gitusername/projectname.git' // project git
//定义GROUP
group = "包名"
//定义pom并打包aar
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
name ''//一些描述
url siteUrl
licenses {
license {
name 'The Apache software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {//开发者信息
id ''
name ''
email ''
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
//打包javadocjar和sourcejar
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
}
javadoc {
options{
encoding 'UTF-8'
charSet 'UTF-8'
author true
}
}
artifacts {
archives javadocJar
archives sourcesJar
}
//上传到maven仓库,从local.properties读取user和apikey
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" //在网站上创建的仓库名,在https://bintray.com/上注册完帐号时,已经有了一个组织(organization),但是没有仓库(Repositories),需要自己先创建一个仓库
name = "projectname" // project name in maven
userOrg = '' //组织名
//这里是困我最长时间的地方,网站上创建组织(organization)时组织名称不能与用户名(user)相同
//而在上传的时候,默认路径是user/repo/name 正确的位置应该是organization/repo/name
//由于默认使用user,所以上传时一直报404错误
//网上的几个资料都没有指出配置userOrg,我自然也不知道用什么字段可以指定正确路径
//由于我猜测字段是organization,然后报了错,并提示了一个网址,于是通过这个网址找到了bintray的GITHUB项目(https://github.com/bintray/gradle-bintray-plugin),找到了相关配置
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
Module的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:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
//添加下边这两句
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
local.properties:
## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/Users/dean/Dev/bak/adt-bundle-mac-x86_64-20140702/sdk
bintray.user= //网站用户名 如果报401错误,可能是user或者apikey写错了
bintray.apikey= //登录网站后,找到编辑简介(Edit Profile)-> API Key
Rebuild一下,使用命令行(AS自带)上传:
gradlew bintrayUpload
上传成功后,在网站(https://bintray.com)自己创建的仓库里找到上传的项目,在项目详情中找到"Add to JCenter" ,等待审核通过。
来源:oschina
链接:https://my.oschina.net/u/1422723/blog/779326