1.把自己需要上傳的項目打包(.jar or .aar)
2.創建Bintray帳號并創建對應的Jcenter倉庫
3.通過Gradle命令上傳
4.注意事項
首先來說第一點:
jar、aar兩者區別
*.jar: 只包含了class文件與清單文件 ,不包含資源文件,如圖片等所有res中的文件。
*.aar: 包含所有資源 ,class以及res資源文件全部包含
如果你只是一個簡單的類庫那么使用生成的.jar文件即可;如果你的是一個UI庫,包含一些自己寫的控件布局文件以及字體等資源文件那么就只能使用.aar文件。
使用方式:
*.jar:拷貝到:libs目錄,eclipse直接導入即可,AndroidStudio項目中添加:
dependencies {?
compile fileTree(include: [‘*.jar’], dir: ‘libs’)?
}?
重新編譯一次項目既可完成加載。
那么要如何生成jar文件或者aar文件呢,這里以aar為例子
首先:
使用android studio生成一個module
在新建的module里面把自己的類文件、布局文件、資源文件等放進去,rebuild一下,就可以了。
生成的aar文件路徑是
右鍵aar文件,show in exploer,即可找到aar文件取出來給別的項目使用
使用方法:
(1)將aar包復制到lib目錄下
(2)配置build.gradle文件:
repositories {?
flatDir {?
dirs ‘libs’?
}
compile(name:’camerascan-1.0’, ext:’aar’)?
第一步就完成了;
第二步:創建Bintray帳號
https://bintray.com?
創建完帳號后,接著需要創建Organization
創建成功后,然后在Organization里面創建一個倉庫(Create a repository)
這里type選擇maven,其他隨意
創建完倉庫(respository)后,需要新建一個包(package),紅色箭頭標記那里
這個包就是你需要上傳的aar文件存儲的地方
這里可以填寫一些基本信息,名字,包的描述,licenses這里選擇Apache-2.0,版本隨意,這里我寫的是1.0.0
基本工作就做好了,接下來就是第三步了
打開你的as,在項目中local.properties 中添加 user和apikey
這里的user是
然后點擊Edit Profile ,最后一項API Key,點擊show就能看到了
接著 在項目最外層的build.gradle 中添加如下兩行代碼
接下來在你要上傳的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
// 你項目的版本號
version= "1.0.0"
android {
? ? compileSdkVersion 26
? ? buildToolsVersion "26.0.2"
? ? resourcePrefix "customView__"
? ? defaultConfig {
? ? ? ? minSdkVersion 15
? ? ? ? targetSdkVersion 26
? ? ? ? versionCode1
? ? ? ? versionName"1.0"
? ? ? ? testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"
? ? }
? ? buildTypes {
? ? ? ? release {
? ? ? ? ? ? minifyEnabledfalse
? ? ? ? ? ? proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
? ? ? ? }
}
? ? tasks.withType(Javadoc) {
? ? ? ? options.encoding= "UTF-8"? // 避免GBK編碼格式錯誤
? ? }
}
dependencies {
? ? implementation fileTree(dir:'libs',include:['*.jar'])
? ? implementation 'com.android.support:appcompat-v7:26.1.0'
? ? testImplementation 'junit:junit:4.12'
? ? androidTestImplementation 'com.android.support.test:runner:1.0.2'
? ? androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
def siteUrl= 'https://gitee.com/baiaj/SearchView'? ? ? // 項目的主頁
def gitUrl= 'https://gitee.com/baiaj/SearchView.git'? // Git倉庫的url
group= "com.clt5201314.baiajRepo.SearchView"
install {
? ? repositories.mavenInstaller {
? ? ? ? // This generates POM.xml with proper parameters
? ? ? ? pom{
? ? ? ? ? ? project{
? ? ? ? ? ? ? ? packaging'aar'
? ? ? ? ? ? ? ? // Add your description here
? ? ? ? ? ? ? ? name'Android SearchView widget'
? ? ? ? ? ? ? ? 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'baiaj'
? ? ? ? ? ? ? ? ? ? ? ? name'byerman'
? ? ? ? ? ? ? ? ? ? ? ? email'baiajclt@gmail.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= "clt5201314"http://JCenter上創建的的Organization
? ? ? ? repo= "baiajRepo"http://你要上傳的庫的名字
? ? ? ? name= "SearchView"? ? //發布到JCenter上的項目名字
? ? ? ? websiteUrl= siteUrl
vcsUrl= gitUrl
licenses = ["Apache-2.0"]
? ? ? ? publish= true
? ? }
}
接下來在Terminal命令行那里輸入?
gradlew install?
gradlew bintrayUpload
就可以了上傳了
上傳成功后到對應的Package里面即可查到對應的消息
點擊Add to JCenter,填寫一些提交信息,等待審批通過
一般第二天審批通過就可以在AS里面調用這個庫了
調用方式 implementation 'com.android.support:appcompat-v7:26.1.0'
注意事項
1.在上傳過程中發現提示:編碼GBK的不可映射字符->請正確配置javadoc編碼
按上面代碼即可解決
tasks.withType(Javadoc) {
? ? ? ? options.encoding= "UTF-8"? // 避免GBK編碼格式錯誤
? ? }