天天看點

android studio 釋出 library 到jcenter步驟

在開發安卓的時候,我們會封裝自己的library ,但是每次建立項目都需要搬遷library,代碼搬遷是比較麻煩的事。如果條件允許的話,可以将library釋出到jcenter 通過gradle添加需要library

如 :

dependencies{
	compile 'com.google.code.gson:gson:2.8.0'
}
           

第一步 注冊

JCenter是由Bintray公司在維護,是以首先你需要注冊一個帳号,傳送門

登陸首頁,往下拉,找到“For Open Source Plan  Sign Up Here” 點選注冊個人免費版,如果是企業使用者,可以點選"star your free trial",不過需要收取伺服器費用。

注意,個人免費版項目都是必須公開的,這個和github一樣。注冊需要郵箱,貌似國内郵箱不支援,是以,大家可以自行注冊gmail

(p.s.釋出過程中遇到網絡問題,請自備梯子.......)

android studio 釋出 library 到jcenter步驟

第二步 建立Repository,擷取API Key

注冊之後 ,在首頁中找到add new Repository 建立項目存放倉庫 

name 輸入你的倉庫名  (記下名字,待會需要用到)

type為倉庫類型

Licenses  為遵循的許可協定

android studio 釋出 library 到jcenter步驟

建立完之後,選擇Edit Profile,進入個人資料頁面,選擇API Key , 拿到自己的key待會 設定需要用到

android studio 釋出 library 到jcenter步驟
android studio 釋出 library 到jcenter步驟

第三步 在Android studio 配置上傳檔案

1. 打開Project 下的 build.gradle  引入相關插件

dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        //引入插件
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
}
           

2.打開Library下的build.gradle 在android 中加入 lintOptions  abortOnError false  避免在gradle編譯的時候 lint 檢查報錯

然後在最底部加入apply from: "bintrayUpload.gradle" 引入編譯檔案

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
}
           
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.google.code.gson:gson:2.8.0'
}
apply from: "bintrayUpload.gradle"
           

3.在library目錄下建立file  bintrayUpload.gradle  拷貝下面代碼 對應中文位置修改即可

apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.0.3" //library 版本,以後更新隻需要更改版本号就行

def siteUrl = 'https://github.com/ljzdyh/CarouselFigure' //項目官網,沒有就填github上位址

def gitUrl = 'https://github.com/ljzdyh/CarouselFigure.git'//項目的git庫位址  必須填寫,不然會報錯


// Maven Group ID for the artifact,一般填你唯一的包名
//比如 compile 'com.android.support:support-v4:24.0.0' 中的
//com.android.support 部分
group = "你的包名"

install {
    repositories.mavenInstaller {
        // This generates POM.xml with proper parameters
        pom {
            project {
                packaging 'aar'
                // Add your description here
                name '通用下拉重新整理上拉加載元件,可自定義上下下載下傳效果'     //項目描述
                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 'test'        //填寫的一些基本資訊
                        name 'test'
                        email '[email protected]'
                    }
                }
                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))
    options.encoding = "UTF-8" //添加編碼方式,才不會報編碼錯誤
}
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 {
        repo = "maven"  //此處填寫 你建立倉庫名
        name = "popularrefreshlayout"    //釋出到JCenter上的項目名字
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish = true
    }
}
           

4.最後,打開Project的local.properties 添加剛剛注冊的使用者名 和 API key

sdk.dir=D\:\\android-sdk-windows
bintray.user=注冊使用者名
bintray.apikey=key值
           

第四步 将項目送出倉庫

打開Android studio Terminal 輸入

gradlew install
           

編譯library,編譯通過之後會顯示BUILD SUCCESS

編譯成功之後,就可以輸入gradlew bintrayUpload,進行library上傳

gradlew bintrayUpload
           

上傳成功後,重新整理你建立的倉庫頁面,就可以看到新上傳的library

android studio 釋出 library 到jcenter步驟

點選進入library ,在右下角Linked to  點選Add to JCenter,然後點選send 發送給管理者稽核,等幾小時之後,稽核通過就會給你發送通知

隻要稽核通過你就可以在gradle中直接引用你的library了

android studio 釋出 library 到jcenter步驟

P.S. 下次修改bug或者版本更新後,隻需要在bintrayUpload.gradle 檔案修改version版本号後, 再次 執行gradlew bintrayUpload 上傳即可,很友善的操作。

繼續閱讀