开发过程当中,若是你写了一个工具类,想给其余项目使用的话,一般都是将它抽到lib目录供其余项目使用: java
而后在调用项目下引入相关的Lib: react
那若是想使用的项目和咱们公用的lib不在一个目录呢?按照之前老的方式,譬如拷贝文件,打包jar的方式虽然又不是不能用,可是不只便于维护和修改,若是每次改动,每一个地方都要操做造做一遍,并且若是有资源文件就更麻烦了,那么能不能跟第三方开源库同样:一个地方完成修改,引用到的地方一行代码就能解决?android
implementation "io.reactivex.rxjava2:rxjava:2.x.y"
复制代码
方案固然是有的,网上也有不少现成方案,我这边精简和整理了一下,今天就给你们一个步骤模板,方便你们快速发布和共享本身的代码,少踩坑和走弯路。git
这里注意要注册右边的开源帐号,千万注意不要注册成左边的绿色按钮了,后面会很麻烦。 github
到目前为止,个人准备工做已经基本到位,回顾一下几个关键信息:web
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
}
复制代码
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
def siteUrl = 'https://www.google.com/' //可选 若是有的话
def gitUrl = 'https://www.google.com/' //可选 若是有的话
group = "com.share" //路径
version = "0.0.2.release" //版本名称, 不要用beta,不然容易审核不经过
//以上两个配合项目目录名最终上传上去引用就是 compile 'com.share:tools:0.0.2.release'
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
}
task copyDoc(type: Copy) {
from "${buildDir}/docs/"
into "docs"
}
artifacts {
archives javadocJar
archives sourcesJar
}
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
name 'is permission tool for android'
url siteUrl
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'soulqw' //用户名
name 'qinwei' //姓名
email 'cd5160866@126.com' //邮箱
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
//关键信息咱们从localProperties文件中读取
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
repo = "TestRepository" // 仓库名
name = "MyTools"// 包名
desc = 'just tools'
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
javadoc {
options {
encoding "UTF-8"
charSet 'UTF-8'
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
}
}
复制代码
上面填入了咱们须要传项目的版本号,其中group、version、和项目目录合并起来的终样式规则以下: apache
./gradlew install bintrayUpload
复制代码
而后成功以下: api
再来看看咱们传上去的库 oracle
dependencies {
// implementation project(':tools')
implementation 'com.share:tools:0.0.2.release'
}
复制代码
注意:app
描述尽可能填写就好,通常几个小时到一两天就审核经过可用了。 若是审核被拒绝,会跟你描述为什么被拒绝,按照他的要求再次修改并回复邮件,便可从新审核:
一旦审核经过,后续只要修改版本名称的迭代都不须要再次审核了
dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
}
复制代码
而你应用的项目也使用了这个库,可是它们各自所依赖的版本号不一致,这样应用项目在引入这个lib时候编辑就容易因版本号不一致而报错,正确的姿式就是讲lib对 相应库的依赖由 implementation 改成 compileOnly 或者由 compile 改成 provided
dependencies {
//这样引用容易引起冲突,改成compileOnly 仅仅lib内可用
// implementation 'com.android.support:appcompat-v7:28.0.0'
compileOnly 'com.android.support:appcompat-v7:28.0.0'
}
复制代码
如下是几个gradle中新老配置的变化对照表:
新配置 | 老配置 | 行为 |
---|---|---|
implementation | compile | 依赖项在编译时对模块可用,而且仅在运行时对模块的消费者可用。 对于大型多项目构建,使用 implementation 而不是 api/compile 能够显著缩短构建时间,由于它能够减小构建系统须要从新编译的项目量。 大多数应用和测试模块都应使用此配置。 |
api | compile | 依赖项在编译时对模块可用,而且在编译时和运行时还对模块的消费者可用。 此配置的行为相似于 compile(如今已弃用),通常状况下,您应当仅在库模块中使用它。 应用模块应使用 implementation,除非您想要将其 API 公开给单独的测试模块。 |
compileOnly | provided | 依赖项仅在编译时对模块可用,而且在编译或运行时对其消费者不可用。 此配置的行为相似于 provided(如今已弃用)。 |
runtimeOnly | apk | 依赖项仅在运行时对模块及其消费者可用。 此配置的行为相似于 apk(如今已弃用)。 |
后来随着项目的发展,咱们的项目慢慢迁移到了kotlin,后来又出现了以Kotlin作为工具的类:
版本号++、
upLoad、
而后。。。
classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.9.18"
复制代码
那么完整的文件就应该是这样:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.10'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
//upload
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
//kotlin
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
//for kotlin upload
classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.9.18"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
复制代码
再回到咱们tools的gradle文件,补上:
//for kotlin upload
apply plugin: 'org.jetbrains.dokka'
//同时添加三个方法 for kotlin
task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaTask) {
outputFormat = 'javadoc'
outputDirectory = javadoc.destinationDir
}
task generateJavadoc(type: Jar, dependsOn: dokkaJavadoc) {
group = 'jar'
classifier = 'javadoc'
from javadoc.destinationDir
}
task generateSourcesJar(type: Jar) {
group = 'jar'
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
复制代码
artifacts 中替换为:
artifacts {
archives generateJavadoc //javadocJar
archives generateSourcesJar //sourcesJar
}
复制代码
那么这么下来上传java 又能上传kotlin的最终模版就是:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
//for upload
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
//for kotlin upload
apply plugin: 'org.jetbrains.dokka'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 14
targetSdkVersion 28
versionCode 2
versionName "0.0.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
//这样引用容易引起冲突,改成compileOnly 仅仅lib内可用
// implementation 'com.android.support:appcompat-v7:28.0.0'
compileOnly 'com.android.support:appcompat-v7:28.0.0'
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
def siteUrl = 'https://www.google.com/' //可选 若是有的话
def gitUrl = 'https://www.google.com/' //可选 若是有的话
group = "com.share" //路径
version = "0.0.3.release" //版本名称, 不要用beta,不然容易审核不经过
//以上两个配合项目目录名最终上传上去引用就是 compile 'com.share:tools:0.0.2.release'
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
}
//同时添加三个方法 for kotlin
task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaTask) {
outputFormat = 'javadoc'
outputDirectory = javadoc.destinationDir
}
task generateJavadoc(type: Jar, dependsOn: dokkaJavadoc) {
group = 'jar'
classifier = 'javadoc'
from javadoc.destinationDir
}
task generateSourcesJar(type: Jar) {
group = 'jar'
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task copyDoc(type: Copy) {
from "${buildDir}/docs/"
into "docs"
}
//for java only
//artifacts {
// archives javadocJar
// archives sourcesJar
//}
//for kotlin
artifacts {
archives generateJavadoc //javadocJar
archives generateSourcesJar //sourcesJar
}
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
name 'is permission tool for android'
url siteUrl
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'soulqw' //用户名
name 'qinwei' //姓名
email 'cd5160866@126.com' //邮箱
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
//关键信息咱们从localProperties文件中读取
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
repo = "TestRepository" // 仓库名
name = "MyTools"// 包名
desc = 'just tools'
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
javadoc {
options {
encoding "UTF-8"
charSet 'UTF-8'
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
}
}
复制代码
再输入上传命令,就能够了:
基本上按照完整的以上流程,就不会有什么上传的问题,通常都能经过
最后留意下localproperties文件的配置便可