上传 Android library 到 Jcenter
2017-05-11 09:59
363 查看
前言
自从用上了 Android-Studio ,我们只要简单的一句:compile 'com.squareup.okhttp3:okhttp:3.6.0'
就可以引入类库了。既然那么方便,我们也可以利用 JCenter 让我们写封装好的方法通过一句代码来引用。
Bintray 账号
创建
这里我们利用 Bintray 来管理我们的类库。所以需要注册一个 Bintray 的账户。我们可以利用 Github 或 Google 的账号来进行注册所以十分方便。不过需要注意的是,进入首页注册时不要点击 START YOUR FREE TRIAL 的按钮,那是组织账户,而我们只需要个人账户就好。
![](http://of57kdq4z.bkt.clouddn.com/bintrary-8acb3cf5-7ff9-4b7e-86f7-0e331061f9fd.jpg)
获取 API Key
在上传时我们需要填写我们的 API key 。![](http://of57kdq4z.bkt.clouddn.com/bintrary-f2f75449-fda4-46aa-ab68-886d726b84a8.png)
![](http://of57kdq4z.bkt.clouddn.com/bintrary-414ef85f-dbcb-4e5d-a319-5ef58325f2dd.png)
![](http://of57kdq4z.bkt.clouddn.com/bintrary-e2e57d99-68ba-4b73-a74e-b3052d9e9f05.png)
配置 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仓库的url def 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 中的配置用户名和 APIKeybintray.user=jianjunhuang bintray.apikey=xxxxxxxxxxxxxxxxxxxxxxxxxxx
记得在 .gitignore 中配置避免上传至网上,造成信息泄露
/local.properties
执行命令
在 Terminal 中输入命令gradlew install gradlew bintrayUpload //linux or mac ./gradlew install ./gradlew bintrayUpload
若都显示 BUILD SUCCESS 就成功了。
这时候我们就可以在 Bintray 的仓库中找到我们的 library 。点击 Add to JCenter,上传 JCenter 中只要经过验证就可以用了,不过要经过一段时间的审核。之后就可以引用了。
![](http://of57kdq4z.bkt.clouddn.com/bintrary-5c99ca31-ccd6-4c76-be97-db82a4be5369.png)
![](http://of57kdq4z.bkt.clouddn.com/bintrary-2ac3cbe6-5412-4327-aa28-2ab6c192f682.png)
常见问题
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') }
相关文章推荐
- Mac Android Studio将项目(library)上传到jcenter
- Android将Library上传到jcenter超简单完整图文步骤以及遇到的各种坑
- Android上传开源项目(Library)到Jcenter
- Android 上传library到jcenter
- Android 上传 Library 到 JCenter
- Android将Library上传到jcenter超简单完整图文步骤以及遇到的各种坑
- Android将Library上传到jcenter超简单完整图文步骤以及遇到的各种坑
- Android studio 上传Android library到jcenter
- Android library快速构建上传jcenter
- 怎么把自己的Android library上传到jCenter和Maven Central
- Android library上传到jcenter
- Android library快速构建上传jcenter
- 【Android】5分钟发布Android Library项目到JCenter
- Android Studio上传Library库到JCenter,并同步到Maven Central
- Android studio 发布Android Library项目到JCenter
- 发布Android Library项目到JCenter的便捷方法
- Android 发布自己的library到Jcenter
- 如何使用Android Studio把自己的Android library分享到jCenter和Maven Central
- Gradle 如何使用Android Studio把自己的Android library分享到jCenter和Maven Central
- Android Studio 将Library 上传到 Jcenter