如何创建自己的开源依赖库(如何上传自己的依赖库library到jCenter)

前端之家收集整理的这篇文章主要介绍了如何创建自己的开源依赖库(如何上传自己的依赖库library到jCenter)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

平时自己遇到了问题,或者一些经典的代码无头绪,写不出来的时候,我们很多人都会进入到 github 上去搜索与自己的问题相似的项目。然后搜索到之后,窃喜3秒钟!!简单阅读之后,看一看使用方式,如:

通过简单的一行代码就能引用我们需要的功能,好神奇!当然这是我刚开始接触到STUdio时候的感觉。然后,当我们使用久了,必然会有这样的想法,我也需要写自己的功能代码依赖库,然后像这一样“简单粗暴”引用,不用在重复写代码了,多好!而且还能让更多的小伙伴共享~,不失为一件“普天同庆”的大事儿。

接下来我就一步一个脚印的分析:

  • 在Studio上建Project

需要先创建一个Project,然后在这个Project中创建一个Module。上图中,mymodule,就是新建的Module,而app则是建立Project时候自动创建的Module。

  • 在码云上新建一个代码仓库


在个人主页的左下角,创建公开的项目,如上图。


  • 托管到码云


在自己刚刚创建的项目中,如上图,复制HTTPS或者SSH的链接。到git上进行提交到码云托管本地IDE上的代码



从上面,我们可以看到git命令:从本地ANDROID-STUdio上的项目,托管到码云平台;从下图的提交注释可以看出,已经提交(push)成功了!


  • 在JFrog Bintray上操作:创建仓库、获取API-KEY

当然,在JFrog Bintray上操作需要我们进行注册一个账户;然后Add New Repository,并获取API KEY备用;

添加一个远程仓库 Repository,新建成功之后,可进入下一步操作。


获取API KEY,在头像下选择 Edit Profile 进入新页面可以考到API KEY然后复制保存备用;




到此,所有的准备功能已经准备完毕!接下来就要进入到ANDROID STUdio 中进行代码的一些配置;


首先在Project下的 build.gradle 文件添加代码
  1. classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
  2. classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'


然后在library下的 build.gradle 文件添加代码

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


然后,具体详细代码的配置,关于library的build.gradle;

  1. apply plugin: 'com.android.library'
  2.  
  3. //第一步:上传到jCenter的配置
  4. apply plugin: 'com.github.dcendents.android-maven'
  5. apply plugin: 'com.jfrog.bintray'
  6.  
  7. android {
  8. compileSdkVersion 26
  9. buildToolsVersion "26.0.0"
  10.  
  11. defaultConfig {
  12. minSdkVersion 19
  13. targetSdkVersion 26
  14. versionCode 1
  15. versionName "1.0"
  16.  
  17. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  18.  
  19. }
  20. buildTypes {
  21. release {
  22. minifyEnabled false
  23. proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
  24. }
  25. }
  26. }
  27.  
  28. dependencies {
  29. compile fileTree(dir: 'libs',include: ['*.jar'])
  30. androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{
  31. exclude group: 'com.android.support',module: 'support-annotations'
  32. })
  33. compile 'com.android.support:appcompat-v7:26.+'
  34. testCompile 'junit:junit:4.12'
  35.  
  36.  
  37. //上传编译的文件到bintray,使用如下的命令:
  38. //gradlew bintrayUpload
  39. /*library版本*/
  40. version = "v1.0.0"
  41. /*github上的项目主页以及git仓库地址*/
  42. def siteUrl = 'https://gitee.com/yuan1530702811/BaSEOnLibrary'
  43. def gitUrl = 'https://gitee.com/yuan1530702811/BaSEOnLibrary.git'
  44. /*maven group ID 这个gruop很重要,参考 compile 'groupId:artifactId:version' groupId就是这个,artifactId就是包名,version就是顶部那个*/
  45. group = "com.ztman.cn"
  46. install {
  47. repositories.mavenInstaller {
  48. // This generates POM.xml with proper parameters
  49. pom {
  50. project {
  51. packaging 'aar'
  52. /*项目描述*/
  53. name 'just a test'
  54. url siteUrl
  55. /*设置证书s*/
  56. licenses {
  57. license {
  58. name 'The Apache Software License,Version 2.0'
  59. url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
  60. }
  61. }
  62. developers {
  63. developer {
  64. id 'yjh'
  65. name 'yjh'
  66. email 'yuannunhua@gmail.com'
  67. }
  68. }
  69. scm {
  70. connection gitUrl
  71. developerConnection gitUrl
  72. url siteUrl
  73. }
  74. }
  75. }
  76. }
  77. }
  78. task sourcesJar(type: Jar) {
  79. from android.sourceSets.main.java.srcDirs
  80. classifier = 'sources'
  81. }
  82. task javadoc(type: Javadoc) {
  83. source = android.sourceSets.main.java.srcDirs
  84. classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
  85. options.encoding = "UTF-8" //添加UTF-8编码方式
  86. }
  87. task javadocJar(type: Jar,dependsOn: javadoc) {
  88. classifier = 'javadoc'
  89. from javadoc.destinationDir
  90. }
  91. artifacts {
  92. archives javadocJar
  93. archives sourcesJar
  94. }
  95. Properties properties = new Properties()
  96. properties.load(project.rootProject.file('local.properties').newDataInputStream())
  97. bintray {
  98. user = properties.getProperty("bintray.user")
  99. key = properties.getProperty("bintray.apikey")
  100. configurations = ['archives']
  101. pkg {
  102. repo = "baSEOnMvpLibrary" //对应bintray账号上的仓库名称;发布到Bintray的那个仓库,即你在bintray网站建立的仓库名,若该仓库不存在,会报错。
  103. name = "baSEOnMvpLibrary" //发布到JCenter仓库下的项目名字
  104. websiteUrl = siteUrl
  105. vcsUrl = gitUrl
  106. licenses = ["Apache-2.0"]
  107. publish = true
  108. }
  109. }
  110. }


在local.properties中代码配置:
  1. ## This file is automatically generated by Android Studio.
  2. # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
  3. #
  4. # This file must *NOT* be checked into Version Control Systems,# as it contains information specific to your local configuration.
  5. #
  6. # Location of the SDK. This is only used by Gradle.
  7. # For customization when using a Version Control System,please read the
  8. # header note.
  9. #Fri Dec 01 16:08:45 CST 2017
  10. ndk.dir=C\:\\Users\\YJH\\AppData\\Local\\Android\\Sdk\\ndk-bundle
  11. sdk.dir=C\:\\Users\\YJH\\AppData\\Local\\Android\\Sdk
  12. #bintray上的用户名
  13. bintray.user=yjh
  14. #上面在bintray中,已经copy下来的apikey
  15. bintray.apikey=bb1e83b4e1da047429738dfacc27420fb9e40bbc
相比之前多了两行代码
  1. #bintray上的用户名
  2. bintray.user=yjh
  3. #上面在bintray中,已经copy下来的apikey
  4. bintray.apikey=bb1e83b4e1da047429738dfacc27420fb9e40bbc

有一些内容我需要在这里进行指明道破:分别在文件 build.gradle和local.properties 中进行截图说明。如图






在自己的ANDROID STUdio 的终端 Terminal 中执行命令:

检测>>




编译上传>>




当我们在上面的终端中敲入命令,最终的运行结果都是 "BUILD SUCCESSFUL" 时候,说明我们到此的所有操作都是正确的了。这个时候,我们可以看到此时我们之前创建的Repository的名字后面多了一个 单词-package


截止到此时,我们已经成功的把library 上传到了 bintray 的 Respository中。但是目前还不能进行 compile 的依赖,因为我们还需要把package 提交到 jCenter。

点击进入,仍需要这样的操作:




待审核通过,然后就能 compile 了!!


注意:如果在IDE终端执行命令 gradlew bintrayUpload 出现

  1. FAILURE: Build Failed with an exception.
  2.  
  3. * What went wrong:
  4. Execution Failed for task ':mymodule:bintrayUpload'.
  5. > Could not upload to 'https://api.bintray.com/content/yjh/baSEOnMvpLibrary/baSEOnMvpLibrary/v1.0.0/com/ztman/cn/mymodule/v1.0.0/mymodule-v1.0.0-javadoc.jar'
  6. : HTTP/1.1 409 Conflict [message:Unable to upload files: An art
  7. ifact with the path 'com/ztman/cn/mymodule/v1.0.0/mymodule-v1.0.0-javadoc.jar' already exists]

说明

  1. mymodule-v1.0.0-javadoc.jar

文件已存在,意思就是已经上传过一次了,要再次上传版本内容,此时需要我们更新新的版本并上传

猜你在找的设计模式相关文章