前言
我們在Android Studio過程當(dāng)中,經(jīng)常會通過在 build.grade 文件中添加一行代碼來引入第三方的庫。
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:24.1.1'
}
這對于我們來說真的是很方便、快捷!假如有一天我們自己寫一個(gè)庫,也希望讓別人這樣很方便的去使用,這應(yīng)該如何去實(shí)現(xiàn)呢?那么接下來我講的,就是如何去實(shí)現(xiàn)這個(gè)想法。
Step1:申請Bintray帳號
進(jìn)入網(wǎng)頁之后,進(jìn)行注冊。注冊成功之后進(jìn)入到Y(jié)our Profile(個(gè)人資料頁面),取得你的API key
下一步,創(chuàng)建你的Package
填寫你的項(xiàng)目信息
這些都弄好了之后,接下來是我們的重頭戲了。在Android Studio配置你的項(xiàng)目,這個(gè)過程你可能會一次成功,也有可能遭遇很多失敗,沒關(guān)系,讓我們慢慢來。
Step2:在Android Studio配置項(xiàng)目
首先我要大家提醒大家的是,Project 的build.gradle 和Module的 build.gradle大家一定要分清楚,千萬別搞混了,如果在接下來的步驟當(dāng)中,如果你配置錯了,你可以想想是不是這個(gè)原因。
好的我們開始!
修改Project(項(xiàng)目)里的build.gradle,增加以下dependencies
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath 'com.android.tools.build:gradle:2.2.0-alpha6'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
}
}
大家看到上面的classpath 'com.android.tools.build:gradle:2.2.0-alpha6 使用的版本是2.2.0,所以你的項(xiàng)目中也盡量和我的一致,否則可能在構(gòu)建的過程中會發(fā)生錯誤。
如果不一致,請修改你的項(xiàng)目目錄下**/gradle/wrapper/gradle-wrapper.properties**中的
**distributionUrl**=https\://services.gradle.org/distributions/gradle-2.10-all.zip
下一步,
配置Module中(也就是你最后要上傳的那個(gè)Library)中的build.gradle
這里面需要你加的東西,后面都會有注釋,然后按照你bintray網(wǎng)站中填寫的對應(yīng)填寫就可以了。
apply plugin: 'com.android.library'
ext {
bintrayRepo = 'maven'
bintrayName = 'ClearableEditText'//在bintray網(wǎng)站中項(xiàng)目中填寫的名稱
publishedGroupId = 'com.zterry.clearableedittext'//一般為包名
libraryName = 'ClearableEditText'//library名字需要和本地的lib module一致
artifact = 'ClearableEditText'//同上
libraryDescription = 'A clearable EditText which can be easy to clear your input.'//項(xiàng)目描述
libraryVersion = '1.0.2'//項(xiàng)目版本號,只需要在每次更新的時(shí)候填寫
developerId = 'tata1989y'//開發(fā)者Id
developerName = 'Terry Liu'//開發(fā)者名稱
developerEmail = 'tata1989y@gmail.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 24
buildToolsVersion "23.0.3"
defaultConfig {
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:24.1.1'
}
然后在文件的末尾添加一下兩行代碼
apply from: 'https://raw.githubusercontent.com/attwellBrian/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/attwellBrian/JCenter/master/bintrayv1.gradle'
Step3:配置APIkey和username
打開項(xiàng)目根目錄下的local.properties,添加你的用戶名和API Key
bintray.apikey=[your apikey]
bintray.user=[your username]
PS: local.properties這個(gè)文件一定要添加到.gitIgnore里面,千萬可不要上傳到github上去哦!~
Step4:上傳
打開Android Studio中的Terminal終端,輸入以下命令
./gradlew install
./gradlew bintrayUpload
等待上傳完畢之后,去自己的倉庫首頁,找到剛才上傳的項(xiàng)目,點(diǎn)擊
接下來就是等待管理員審核通過,第一次時(shí)間會比較長,如果是更新的會很快!
大功告成!
End:總結(jié):
在一開始,Google很多的資料,但是只有幾篇文章是有效果的,因?yàn)橹虚g配置的過程中發(fā)生了很多的錯誤,再此我把遇到的錯誤以及解決方案給大家分享出來。
Could not create package : HTTP/1.1 400 Bad Request
Package license is mandatory解決:檢查apiKey
Maven group, artifact or version defined in the pom file do not match
the file path解決:檢查 libraryName & artifact 名字需要一致
Q :No service of type Factory available in ProjectScopeServices
A :
```
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-beta1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
}
```