轉自:http://www.cnblogs.com/cpacm/p/5548241.htmlhtml
在 Android Studio 中,咱們一般能夠利用 gradle 來導入別人寫的第三方庫,一般能夠簡單得使用一句話就能搞定整個導包過程,
好比:java
compile 'net.cpacm.moneytext:moneyview:1.0.0'
在這個過程當中,Android Studio 會從 Maven 倉庫服務器中下載所對應的包。如今比較通用的兩個服務器分別爲 Jcenter 和 Maven Central。本文主要講的就是Jcenter了,Android Studio 默認使用的服務器倉庫。android
Jcenter 是 bintray.com 所使用的 Maven 倉庫。與 Maven Central 相比,jcenter 的速度更快,包含的庫更多,UI界面更友好,更容易使用,同時 bintray 還支持將 jcenter 上傳到 Maven Central 的功能。git
之前使用過 Maven 的可能會比較清楚,導入的語句如何指向倉庫中惟一存在的庫。github
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
表明使用的包的版本。web
bintray 支持 github 和 google+ 第三方直接登陸。apache
登陸後進入 Maven 倉庫創建要使用的包,同時填入相應的對應信息。api
首先確保你要上傳的第三方包是一個 library,做爲一個項目的 module 存在。服務器
在 project gradle 中加入須要的依賴包app
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 文件裏面加入 api-key
bintray.user=YOUR_BINTRAY_USERNAME bintray.apikey=YOUR_BINTRAY_API_KEY
在 bintray 網站的我的信息編輯頁能夠找到。
對了,記得不要把 local.properties
傳到 github網站上致使我的信息的泄露。
修改 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 網站看看結果。
點擊上圖的 Add to jcenter 按鈕,向管理員申請添加到 jcenter 倉庫,通常等個幾小時就能經過。
最後在 https://jcenter.bintray.com 找到本身的庫就表示成功了。