上传 Android library 到 Jcenter

来源:互联网 发布:中粮我买网淘宝店 编辑:程序博客网 时间:2024/06/09 17:07

前言

自从用上了 Android-Studio ,我们只要简单的一句:

compile 'com.squareup.okhttp3:okhttp:3.6.0'

就可以引入类库了。既然那么方便,我们也可以利用 JCenter 让我们写封装好的方法通过一句代码来引用。

Bintray 账号

创建

这里我们利用 Bintray 来管理我们的类库。所以需要注册一个 Bintray 的账户。我们可以利用 Github 或 Google 的账号来进行注册所以十分方便。

不过需要注意的是,进入首页注册时不要点击 START YOUR FREE TRIAL 的按钮,那是组织账户,而我们只需要个人账户就好。

获取 API Key

在上传时我们需要填写我们的 API key 。

点击 Edit

点击左下角的 API Key

点击查看或复制

配置 Gradle

1. 首先我们需要在整个项目的 build.gradle 中加入

classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.2'

添加后

 buildscript {  repositories {    jcenter()  }  dependencies {    classpath 'com.android.tools.build:gradle:2.2.3'    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'    classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.2'    // NOTE: Do not place your application dependencies here; they belong    // in the individual module build.gradle files  }}

2. library 中的 build.gradle

apply plugin: 'com.android.library'apply plugin: 'com.jfrog.bintray'apply plugin: 'com.github.dcendents.android-maven'//版本号,更新时记得修改version = "0.0.1"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'  }  }}dependencies {  compile fileTree(dir: 'libs', include: ['*.jar'])  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {    exclude group: 'com.android.support', module: 'support-annotations'  })  compile 'com.android.support:appcompat-v7:25.2.0'  compile 'com.squareup.okhttp3:okhttp:3.6.0'  compile 'com.google.code.gson:gson:2.8.0'  compile 'com.github.dcendents:android-maven-plugin:1.2'  testCompile 'junit:junit:4.12'}// 项目的主页def gitUrl = 'https://github.com/jianjunhuang/OkHttpDemo'// Git仓库的urldef siteUrl = 'https://github.com/jianjunhuang/OkHttpDemo.git'group = "com.library.jianjunhuang.okhttputils" //一般填你唯一的包名,在引用时的代码就是根据这个来的install {  repositories.mavenInstaller {    // This generates POM.xml with proper parameters  pom {      project {        packaging 'aar'        name 'OkHttp Utils' //项目的描述        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 'jianjunhuang' //填写的一些基本信息            name 'jianjunhuang'            email 'demon903732218@gmail.com'  }        }        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))}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"    //发布到JCenter上的项目名字    name = "OkHttpUtils"    websiteUrl = siteUrl    vcsUrl = gitUrl    licenses = ["Apache-2.0"]    publish = true  }}

3. 配置 UserName 和 APIKey

在项目的 local.properties 中的配置用户名和 APIKey

bintray.user=jianjunhuangbintray.apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxx

记得在 .gitignore 中配置避免上传至网上,造成信息泄露

/local.properties

执行命令

在 Terminal 中输入命令

gradlew installgradlew bintrayUpload

若都显示 BUILD SUCCESS 就成功了。

这时候我们就可以在 Bintray 的仓库中找到我们的 library 。点击 Add to JCenter,上传 JCenter 中只要经过验证就可以用了,不过要经过一段时间的审核。之后就可以引用了。

常见问题

  • Could not upload to ‘https://.pom’: HTTP/1.1 400 Bad Request [message:Unable to upload files: Maven group, artifact or version defined in the pom file do not match the file path ‘***.pom’]
    名字错误

  • bintrary 中没有建立 maven 仓库

  • 注释中使用了中文
    添加:

tasks.withType(Javadoc) {  options.addStringOption('Xdoclint:none', '-quiet')  options.addStringOption('encoding', 'UTF-8')  options.addStringOption('charSet', 'UTF-8')}
0 0
原创粉丝点击