将Android库发布到jcenter是为了更方便的引用
一般有以下几个步骤:
- 新建工程和要发布的module
- 修改build.gradle
- 新建仓库
- library的构建和上传
- 将library发布到jceter等待审核通过
- 收到审核结果邮件,引用项目
1. 新建工程和module
新建工程,并将你要发布的独立成一个module,便于发布
example:
这里面新建了lib-db module,我们的目标就是将它达成aar格式并发布到jcenter中
2. 修改build.gradle
project 的build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.3' //关键的二行代码必须加上 classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
module的build.gradle
apply plugin: 'com.android.library' //1、引用插件 apply plugin: 'com.github.dcendents.android-maven' apply plugin: 'com.jfrog.bintray' android { compileSdkVersion 28 defaultConfig { minSdkVersion 14 targetSdkVersion 28 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) } //2、自定义一些属性,这些属性在upload.gradle都会使用到 ext { //发布到bintray上仓库名,执行上传前必须手动创建同名到仓库,否则上传会失败 bintrayRepo = 'framework' bintrayName = 'db' //群组id publishedGroupId = 'com.fzm.db' //发布成库的名称 libraryName = 'db' //最好和libraryName保持一致 artifact = 'db' //库描述 libraryDescription = 'a db library for fzm' siteUrl = 'github project index page' gitUrl = 'github project url' libraryVersion = '0.0.3' developerId = 'developer id' developerName = 'your name' developerEmail = 'xxxx@163.com' licenseName = 'The Apache Software License, Version 2.0' licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt' allLicenses = ["Apache-2.0"] } //此处是为了将发布的代码逻辑封装到upload.gradle脚步中去了,这样build.gradle更简洁,代码维护性更高 apply from : 'upload.gradle'
module name建议和bintrayName、libraryName保持一致
upload.gradle
此脚步文件格式比较固定,可以直接copy即可
group = publishedGroupId version = libraryVersion install { repositories.mavenInstaller { pom.project { packaging 'aar' groupId publishedGroupId artifactId artifact name libraryName description libraryDescription url siteUrl 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) { classifier = 'sources' from android.sourceSets.main.java.srcDirs } 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 = bintrayRepo name = bintrayName desc = libraryDescription websiteUrl = siteUrl vcsUrl = gitUrl licenses = allLicenses dryRun = false publish = true override = false publicDownloadNumbers = true version { desc = libraryDescription } } } javadoc{ options{ encoding "UTF-8" charSet "UTF-8" } }
3. 新建仓库
访问https://bintray.com/ -> login -> view Profile -> add new Repository
注意:
这里的仓库名称必须和bintrayRepo字段保持一致,此处示例为framework;
因为我们通过脚步实现上传发布,所以需要用到api key;
edit Profile -> api key -> 输入密码
因为api key 属于敏感信息,所以我们存放到本地(local.properties),不要上传到git上
bintray.user=userName
bintray.apikey=your api key
4. library的构建、上传
//clean and install
./gradlew clean install
upload
./gradlew bintrayUpload
成功后我们登录官网可以在framework仓库中看到发布的library了
注意此时该library并没有发布到jcenter中,还差最后一步
5. 发布到jcenter
进入自己的library(示例中为db) -> Add to JCenter
提交申请后只需要耐心等待审核了,审核通过会显示如下,这样你就可以直接引用了
三种引用方式