background
Development process, if you write a utility class, want to give other items used, it will usually be able to get lib directory to use for other projects:
Lib then introduced into related projects under the call:
problem
If you want to use that project and we are not in a common lib directory it? According to previous old ways, such as copying files, packaged jar of way, although they are not can not be used, but not only easy to maintain and modify, if each change, each place must operate pretentious again, and if there is a resource file even more trouble , it can not be the same with the third party open source libraries: a place to complete the modification, a reference to the local line of code can be solved?
like this:
implementation "io.reactivex.rxjava2:rxjava:2.x.y"
复制代码
Of course, some programs, the Internet, there are many ready-made solutions to streamline and organize my side a bit, give us today a step template to facilitate the rapid publish and share their own code, small step on the pit and detours.
Upload Java code to Jcenter
The main steps (to complete the full recommended in the proxy, or will be very slow or unsuccessful, framed part is the key entry):
- Sign JFrog Bintray account
Note To open the account register here on the right, you should be careful not registered to the left of the green button, and the back will be very troublesome.
- After successful registration, remember your Api key in your Profile, the upload will be to use:
- Create a new warehouse, the warehouse is where we managed code
- Continue to be created in just a warehouse (Respository) a package (Packge), for example, I am now ready to project tools inside the code passed in this package, so I created:
So far, my preparations have been basically put in place, look at a few key messages:
- Warehouse name: TestRepository
- Package name: MyTools
- Username: soulqw (will be replaced by your own)
- Api key: xxxxxxxx (will be replaced by your own)
- Returning to our project to do some template configuration
- Add the following configuration file in the root gradle our project:
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
}
复制代码
- We need to pass back to the directory, that is where our tools directory, add in its gradle file as follows:
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 '[email protected]' //邮箱
}
}
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"
}
}
复制代码
Fill the version number of the above we need to pass the project, which the group, version, and a final project directory merge style rules are as follows:
./gradlew install bintrayUpload
复制代码
Then success is as follows:
We passed up look at library
dependencies {
// implementation project(':tools')
implementation 'com.share:tools:0.0.2.release'
}
复制代码
note:
- Be sure to complete the upload process at global proxy
- After a successful version was passed up, we can not change, if need be changed, re-modify the code to re-upload the modified version of the name, such as 0.0.2.release, (try to use realease or a version number, or easy to review rejected )
- Now do not be passed up later reference, and you need Add to Jcenter, after the adoption of the audit can
Description Try to fill in just fine, usually a few hours to a day or two for approval available. If the audit is rejected, it will be described why refuse you, to modify his request again and reply to a message, you can re-examine:
Once approved, subsequent iterations as long as the modified version of the name does not need to re-reviewed
- If your project dependent on the Lib own a library, such as Android Support Library
dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
}
复制代码
The project for your application also uses this library, but they each depend on the version number of inconsistencies, so that the application program introduced this lib when editing is easy due to the inconsistent version number and error, correct posture is to say lib dependency on the appropriate library in compileOnly to the implementation or to compile provided by the
dependencies {
//这样引用容易引发冲突,改为compileOnly 仅仅lib内可用
// implementation 'com.android.support:appcompat-v7:28.0.0'
compileOnly 'com.android.support:appcompat-v7:28.0.0'
}
复制代码
The following is a table of several gradle change in the old and new configuration:
New configuration | Old Configuration | behavior |
---|---|---|
implementation | compile | Dependency on modules are available at compile time, and only available to consumers in the run-time module. For the construction of large multi-project implementation instead of using api / compile can significantly reduce build time, because it can reduce the amount of the project to build the system needs to be recompiled. Most applications and test modules should use this configuration. |
api | compile | Dependency on modules are available at compile-time and runtime also available to consumers and the module at compile time. This configuration is similar to the behavior compile (now deprecated), under normal circumstances, you should only use it in the library module. Application module should use the implementation, unless you want to open its API to the individual test modules. |
compileOnly | provided | Dependency on module is available only at compile time, and its consumers is not available at compile or run time. This behavior is similar to the configuration provided (now deprecated). |
runtimeOnly | apk | Module and its dependencies are available to consumers only at run time. This configuration behaves like apk (now deprecated). |
Introduction:
Later, with the development of the project, our project slowly moved to kotlin, and later appeared as a tool of class to Kotlin:
++ version,
upLoad、
then. . .
Supports uploading Kotlin Code
- Back to the project level gradle file with:
classpath "org.jetbrains.dokka:dokka-gradle-plugin:0.9.18"
复制代码
Then the complete file should be like this:
// 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
}
复制代码
Back to our tools of gradle file, make up:
//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 are replaced by:
artifacts {
archives generateJavadoc //javadocJar
archives generateSourcesJar //sourcesJar
}
复制代码
Then so can upload java upload down kotlin final template is:
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 '[email protected]' //邮箱
}
}
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"
}
}
复制代码
Then enter the upload command on it:
to sum up
According to the above process is basically complete, there will not be any problems uploading, usually can be accessed through
Finally, pay attention to the configuration file localproperties
Reproduced in: https: //juejin.im/post/5cf8dc44e51d454f6f16eb9e