AndroidStudio怎么将开源项目发布到jcenter

知识养成了思想,思想同时又在融化知识。这篇文章主要讲述AndroidStudio怎么将开源项目发布到jcenter相关的知识,希望能为你提供帮助。

如何把自己写的项目让别人依赖呢,像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仓库里。

  1. 在bintray.com上面注册一个账号,可使用github账号登录。
  2. 创建一个新的仓库,如下图所示
    AndroidStudio怎么将开源项目发布到jcenter

    文章图片

    AndroidStudio怎么将开源项目发布到jcenter

    文章图片
      上面的 Issues tracker,例如:https://github.com/DyncKathline/SwipeBackLayout/issues  Version control *, 例如:https://github.com/DyncKathline/SwipeBackLayout
  3. Create Package成功后,就可以看到我们刚创建的Package,如下图所示
    AndroidStudio怎么将开源项目发布到jcenter

    文章图片
把自己的Android Studio类库pull到jcenter上去
【AndroidStudio怎么将开源项目发布到jcenter】在这里我们将要上传的是Android Library module,如下图所示。
AndroidStudio怎么将开源项目发布到jcenter

文章图片

然后我们需要设置bintray的username和API Key来进行bintray的加密认证,这些信息将写在local.properties文件里。写在local.properties的原因在于,bintray的username和APIKey是敏感的私人信息,自己应该保管好,而不是把它上传到github上,正好把local.properties文件写在.gitignore里面过滤掉不会影响上传的github项目。
代码如下:
bintray.user=YOUR_BINTRAY_USERNAME bintray.apikey=YOUR_BINTRAY_API_KEY

这里的username就是你bintray账号的用户名,APIKey可以在bintray页面的Edit Profile找到。
AndroidStudio怎么将开源项目发布到jcenter

文章图片

在要上传的类库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仓库的urllibraryVersion = ‘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‘

这里可以参考以下图
AndroidStudio怎么将开源项目发布到jcenter

文章图片

在这里需要把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窗口执行如下命令:
AndroidStudio怎么将开源项目发布到jcenter

文章图片

gradlew install

如果顺利的话,过几分钟就会出现
BUILD SUCCESSFUL

接下来需要把build成功的文件upload到bintray上
gradlew bintrayUpload

顺利的话,也会出现
BUILD SUCCESSFUL

最后你可以在jcenter上看到如下图所示:
AndroidStudio怎么将开源项目发布到jcenter

文章图片

到了这里我们只要在上图点击“Add 头jCenter”,如下图所示:
AndroidStudio怎么将开源项目发布到jcenter

文章图片

接下来会跳到一个Request to include package GradientUI in jcenter的页面,什么都不用做,直接点击Send按钮就可以了。
大概过个2,3个小时,通过jcenter那边的审核就会在bintray上收到jcenter那边的同意消息提醒。 至此就要恭喜你,你的类库上传到jcenter成功了!
到这里大家就可以用自己的类库了,仅仅只需要添加一行代码:
dependencies { compile ‘com.example.swipebackactivity:swipebacklayout:1.0.0‘ }

转载请注明出处,谢谢!


















    推荐阅读