把自己的依賴放到JCenter 首先需要註冊Bintray ,因爲Bintray 是 JCenter 庫的運營商。
-
首先你要有一個項目,準確的說應該有一個Android moudle並把項目上傳到GitHub
-
註冊 :網址
https://bintray.com/signup/oss
,這裏註冊一定要註冊個人賬號 -
Add New Repository
-
Repository添加成功後會跳轉到Repository首頁,接下來需要Add a Package
-
package添加成功後去package主頁Add a Version
到此,在Bintray上的操作就告一段落
現在回到AndroidStudio進行配置
-
在Project的build.gradle添加依賴(項目最外層build.gradle)
dependencies { classpath 'com.android.tools.build:gradle:3.6.3' //以下兩項依賴需要添加 classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4" classpath "com.github.dcendents:android-maven-gradle-plugin:2.1" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }
-
在local.properties配置Bintray的用戶名和Apikey
bintray.user=你的用戶名 bintray.apikey=你的apikey
-
在moudle的build.gradle添加如下屬性注意是最上面(在apply plugin: 'com.android.library’之上)
ext { bintrayRepo = 'appUpdate' // 之前添加的Repository name bintrayName = 'com.sanzhi.appupdate' // 之前添加的package name publishedGroupId = 'com.sanzhi.appupdate' // 默認應該和package name一樣 this is the ID we want to see in implementation line libraryName = 'appupdate' // moudle的名稱this is the module name of library artifact = 'appupdate' // moudle的名稱this is the artifact we want to see in implementation line libraryDescription = 'self app updata library' // description of library siteUrl = 'https://github.com/parade0393/AppUpdate' // 項目再github的主頁git repo url gitUrl = 'https://github.com/parade0393/AppUpdate.git' // 項目再GitHub的倉庫地址git repo vcs url libraryVersion = '1.0.0' // 之前添加的版本號library version後續更新也是改這個版本號 developerId = 'parad0393' // This is your bintray username developerName = 'parad0393' // Developer's name developerEmail = '[email protected]' // Developer's email licenseName = 'The Apache Software License, Version 2.0' // for example, The Apache Software License, Version 2.0 licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt' // for example, http://www.apache.org/licenses/LICENSE-2.0.txt allLicenses = ["Apache-2.0"] // array of licenses, for example, ["Apache-2.0"] }
-
在moudle下新建publish.gradle文件,並在文件添加如下內容
apply plugin: 'com.jfrog.bintray' apply plugin: 'com.github.dcendents.android-maven' version = libraryVersion group = publishedGroupId // 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 libraryName description libraryDescription url siteUrl // Set your license licenses { license { name licenseName url licenseUrl } } developers { developer { id developerId name developerName email developerEmail } } scm { connection gitUrl developerConnection gitUrl url siteUrl } } } } } task sourcesJar(type: Jar) { from android.sourceSets.main.java.srcDirs archiveClassifier = 'sources' } artifacts { archives sourcesJar } // Bintray 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 = bintrayRepo name = bintrayName desc = libraryDescription websiteUrl = siteUrl vcsUrl = gitUrl licenses = allLicenses publish = true publicDownloadNumbers = true version { desc = libraryDescription } } } //這裏不需要做修改
-
回到moudle的build.gradle在apply plugin: 'com.android.library’後面添加apply from: ‘publish.gradle’
-
至此在AndroidStudio配置完成,接下來就是打包上傳到Bintray,在Studio的terminal中依次輸入以下兩個命令
gradlew install gradlew bintrayUpload
-
正常情況下執行完以上兩個命令,代碼就已經上傳到Bintray裏,現在還剩最後一步,回到Bintray,打開本package頁面 Add to Jcenter
-
等待審覈通過就可以在AndroidStudio裏直接implementation了
-
參考文章