標籤:
如何把自己寫的項目讓別人依賴呢,像compile ‘com.google.code.gson:gson:2.6.2‘一樣?
上面的依賴library需要3各部分,即:GROUP_ID:ARTIFACT_ID:VERSION,其中GROUP_ID是com.google.code.gson(庫的包名),ARTIFACT_ID是gson(類庫名稱),VERSION是2.6.2(版本號碼)。
怎麼上傳你的類庫到jcenter倉庫?
基本上大致的步驟可以慨括為,在AndroidStudio上準備好要上傳的庫項目,配置相關gradle代碼,然後上傳到bintray網站上,最後同步到jcenter倉庫裡。
- 在bintray.com上面註冊一個帳號,可使用github帳號登入。
- 建立一個新的倉庫,如所示
上面的 Issues tracker,例如:https://github.com/DyncKathline/SwipeBackLayout/issues Version control *, 例如:https://github.com/DyncKathline/SwipeBackLayout
- Create Package成功後,就可以看到我們剛建立的Package,如所示
把自己的Android Studio類庫pull到jcenter上去
在這裡我們將要上傳的是Android Library module,如所示。
然後我們需要設定bintray的username和API Key來進行bintray的加密認證,這些資訊將寫在local.properties檔案裡。寫在local.properties的原因在於,bintray的username和APIKey是敏感的私人資訊,自己應該保管好,而不是把它上傳到github上,正好把local.properties檔案寫在.gitignore裡面過濾掉不會影響上傳的github項目。
代碼如下:
bintray.user=YOUR_BINTRAY_USERNAMEbintray.apikey=YOUR_BINTRAY_API_KEY
這裡的username就是你bintray帳號的使用者名稱,APIKey可以在bintray頁面的Edit Profile找到。
在要上傳的類庫module的build.gradle檔案裡添加如下代碼:
apply plugin: ‘com.android.library‘android { compileSdkVersion 23 buildToolsVersion ‘23.0.3‘ defaultConfig { minSdkVersion 11 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘ } }}dependencies { compile fileTree(dir: ‘libs‘, include: [‘*.jar‘]) compile ‘com.android.support:appcompat-v7:23.4.0‘}//解決 gradlew 構建錯誤: 編碼 GBK的不可映射字元tasks.withType(Javadoc) { options.encoding = "UTF-8"}ext { bintrayRepo = ‘SwipeBackLayout‘ //這個應該是傳到maven的倉庫的 bintrayName = ‘SwipeBackLayoutLibrary‘ //發布的項目名字 publishedGroupId = ‘com.example.swipebackactivity‘ libraryName = ‘SwipeBackLayoutLibrary‘ artifact = ‘swipebacklayout‘ libraryDescription = ‘With immersion sideslip the effect of closed Activity‘ //項目的描述 siteUrl = ‘https://github.com/DyncKathline/SwipeBackLayout‘ // 項目的首頁 gitUrl = ‘https://github.com/DyncKathline/SwipeBackLayout.git‘ // Git倉庫的url libraryVersion = ‘1.0.0‘ // 這個version是library的版本,更新後也需要更改這個值 developerId = ‘dync‘ developerName = ‘DyncKathline‘ developerEmail = ‘[email protected]‘ licenseName = ‘The Apache Software License, Version 2.0‘ licenseUrl = ‘http://www.apache.org/licenses/LICENSE-2.0.txt‘ allLicenses = ["Apache-2.0"]}apply from: ‘https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle‘apply from: ‘https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle‘
這裡可以參考以
在這裡需要把bintrayName寫成之前Create Pacakge所填寫的package name,publishedGroupId就是我們之前提到的GROUP_ID,artifact就是之前提到的ARTIFACT_ID,libraryVersion就是VERSION。
然後在project的build.gradle裡添加以下代碼:
// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript { repositories { jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:2.1.3‘ // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files classpath ‘com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6‘ classpath ‘com.github.dcendents:android-maven-gradle-plugin:1.5‘ }}allprojects { repositories { jcenter() }}task clean(type: Delete) { delete rootProject.buildDir}
最後在AndroidStudio提供的Terminal視窗執行如下命令:
gradlew install
如果順利的話,過幾分鐘就會出現
BUILD SUCCESSFUL
接下來需要把build成功的檔案upload到bintray上
gradlew bintrayUpload
順利的話,也會出現
BUILD SUCCESSFUL
最後你可以在jcenter上看到如所示:
到了這裡我們只要在點擊“Add 頭jCenter”,如所示:
接下來會跳到一個Request to include package GradientUI in jcenter的頁面,什麼都不用做,直接點擊Send按鈕就可以了。
大概過個2,3個小時,通過jcenter那邊的審核就會在bintray上收到jcenter那邊的同意訊息提醒。 至此就要恭喜你,你的類庫上傳到jcenter成功了!
到這裡大家就可以用自己的類庫了,僅僅只需要添加一行代碼:
dependencies { compile ‘com.example.swipebackactivity:swipebacklayout:1.0.0‘}
轉載請註明出處,謝謝!
AndroidStudio怎麼將開源項目發布到jcenter