背景
開源一直是Android 的優(yōu)勢,代碼共享,不僅可以造福其他程序員,也可以發(fā)現(xiàn)自身不足,從而提升自己。Github上有太多優(yōu)秀的開源庫,這使得我們在項(xiàng)目開發(fā)中非常方便,不用重復(fù)造輪子。只需要在build.gradle 中 添加添加一行代碼compile ' xxxx '(如:compile 'com.android.support:appcompat-v7:24.2.1') 就可以使用了,非常方便。最近在學(xué)習(xí)如何將自己寫的代碼開源,也就是將項(xiàng)目發(fā)布到JCenter, 這個過程中也遇到了一些問題,通過Google 和參考了一些博客,折騰了大半天才搞定,本篇本章對學(xué)習(xí)過程中的知識點(diǎn)和遇到的問題做一個記錄和總結(jié)。
正文
本篇文章以自己的一個小項(xiàng)目CustomPopwindow為例,講一下通過Android Studio 發(fā)布開源項(xiàng)目到JCenter的步驟:
1,要將我們的項(xiàng)目發(fā)布到JCenter,首先我們需要到Bintray官網(wǎng)注冊一個帳號,一般都有Github,所以選擇直接用Github 登陸,如圖:
2,登陸之后,來到 Bintray 的主頁:
上圖兩個紅色尖頭標(biāo)記的很重要,user 和 userOrg,后面會用到這2個,在第一步點(diǎn)擊登陸后會讓你填公司名字(隨便填一個就好)Company,然后會根據(jù)Company 生成一個默認(rèn)的組織 Organization, 根據(jù)上圖的菜單可以看出,我們還可以創(chuàng)建新的Organization。
3,點(diǎn)擊 edit profile ,獲取 apikey ,這個key 后面會用。點(diǎn)擊show 就可顯示出來。
4,通過右上角的菜單,進(jìn)入我們創(chuàng)建的Organization,然后在這個Organization里面創(chuàng)建一個倉庫repo( 也就是我們發(fā)布的項(xiàng)目在JCenter 上存放的倉庫)
5,創(chuàng)建好倉庫后,在倉庫里面創(chuàng)建一個Package, 也就是我們發(fā)布的項(xiàng)目在JCenter 上的名字,如圖
6,創(chuàng)建好了之后,在我們的倉庫里就會有這個項(xiàng)目了,如下圖:
** 通過以上6步,我們的準(zhǔn)備工作就做完了,接下來就是在 Android Studio 中,添加代碼,執(zhí)行命令上傳 **
7,在項(xiàng)目中l(wèi)ocal.properties 中添加 user和apikey
bintray.user=xxxxx
bintray.apikey=xxxx
也就是上面步驟2和步驟3標(biāo)出來的 user 和apikey.
8,在項(xiàng)目最外層的build.gradle 中添加如下兩行代碼
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
//添加以下兩行
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
9,接下來在你要上傳的Libray的build.gradle 中添加代碼:
apply plugin: 'com.android.library'
//添加兩個插件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
// This is the library version used when deploying the artifact
// 你項(xiàng)目的版本號
version = "1.0.0"
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
resourcePrefix "customPop__" //這個隨便填
defaultConfig {
minSdkVersion 16
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(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:24.2.1'
testCompile 'junit:junit:4.12'
}
def siteUrl = 'https://github.com/pinguo-zhouwei/CustomPopwindow' // 項(xiàng)目的主頁
def gitUrl = 'https://github.com/pinguo-zhouwei/CustomPopwindow.git' // Git倉庫的url
group = "com.example.zhouwei.library" // 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 PopupWindow widget' //項(xiàng)目描述
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 'zhouwei' //填寫的一些基本信息
name 'JayZhou'
email 'zhouweigmail@163.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 {
userOrg = "weiyinshidai"http://JCenter上創(chuàng)建的的Organization
repo = "maven"http://你要上傳的庫的名字
name = "CustomPopwindow" //發(fā)布到JCenter上的項(xiàng)目名字
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
以上代碼** 添加注釋的地方換成你自己的**,千萬別把userOrg 這個弄掉了,我當(dāng)時沒有加這個,一直上傳不成功。
10,接下來在Android Studio 的 Terminal 中 執(zhí)行以下命令命令上傳了
1, ./gradlew install
2, ./gradlew bintrayUpload
11,等到上傳完全之后,進(jìn)入Bintray找到你上傳的項(xiàng)目,點(diǎn)擊右下角的 Add to JCenter發(fā)送Request,然后就是等了,通過了之后會給你發(fā)消息到Bintray帳號上的。
12,通過之后,就可以直接在項(xiàng)目中通過compile 來引用了:
dependencies {
compile 'com.example.zhouwei.library:library:1.0.0'
}
通過以上我們就能將我們的項(xiàng)目發(fā)布都JCenter了。
遇到的問題總結(jié):
這個過程中遇到了兩個問題,折騰了好久:
1,404 Not Found, 這個錯誤是你在grade 中寫的repo和 userOrg 和你在Bintray上的不一致,導(dǎo)致找不到,下面這三個:
2,401 need authorized ,這個錯誤是因?yàn)槟愕膗ser 和 apikey 不正確,我當(dāng)時是把user 這個字段的值寫成 Organization 名了,一直報401,如果報這個錯,請仔細(xì)檢查 user 和 api key.
以上就是通過Android Studio 發(fā)布開源項(xiàng)目到JCenter的一些知識點(diǎn)及遇到的坑