打包在本地的优势
- 不用搭建Maven库环境(当然想麻烦点搭建一个内网环境也是可以的)
- 无网络时可用(如果还没用镜像这个时间绝对很痛苦)
- 不需要审核(只是个人使用jcenter什么的太浪费时间)
- 随时修改源码(第三方库的bug不能及时修复,那就自己打包一个)
- 减少编译时间(不需要等待Library Module编译了)
快速使用
先来个栗子 libs-android
首先吧config.gradle
复制到Project下(文件去栗子里下载),接着修改Project和Module的build.gradle文件如下。
Project build.gradle 修改
buildscript {
apply from : "config.gradle"
ext.kotlin_version = '1.1.2-4'
repositories {
maven { url rootProject.ext.maven.aliyunMaven}
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
flatDir { dirs 'libs' }
maven { url rootProject.ext.maven.sdkAndroidMaven }
maven { url rootProject.ext.maven.sdkGoogleMaven }
maven { url rootProject.ext.maven.sdkMaven }
maven { url rootProject.ext.maven.localMaven}
maven { url rootProject.ext.maven.localMavenSnapshots}
maven { url rootProject.ext.maven.aliyunMaven}
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Module build.gradle 添加
apply plugin: 'maven'
//https://github.com/wittyneko/libs-android
def siteUrl = 'https://github.com/wittyneko/libs-android'
//https://github.com/wittyneko/libs-android.git
def gitUrl = 'https://github.com/wittyneko/libs-android.git'
// 配置、配置、配置
group = "cn.wittyneko"
archivesBaseName = 'auto-layout'
version = '1.0.0-SNAPSHOT'
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
//title = ''
//description = ''
source = android.sourceSets.main.java.srcDirs
classpath += project.files(configurations.provided.files,configurations.compile.files,android.getBootClasspath().join(File.pathSeparator))
options {
encoding 'utf-8'
charSet 'utf-8'
links 'http://docs.oracle.com/javase/7/docs/api/'
linksOffline "https://developer.android.com/reference", "${android.sdkDirectory}/docs/reference"
}
exclude '**/BuildConfig.java'
exclude '**/R.java'
options.encoding = 'utf-8'
failOnError false
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
uploadArchives {
repositories {
mavenDeployer {
String user = 'admin'
String password = 'admin123'
//先读取gradle.properties,不存在则读取local.properties
Properties properties = rootProject.ext.android.properties
user = rootProject.hasProperty('localUserName') ? rootProject.localUserName : (properties != null ? properties.getProperty('localUserName', user) : user)
password = rootProject.hasProperty('localPassword') ? rootProject.localPassword : (properties != null ? properties.getProperty('localPassword', password) : password)
println("user: $user, passowrd: $password")
repository(url: rootProject.ext.maven.localMaven){
authentication(userName: user, password: password)
}
snapshotRepository(url: rootProject.ext.maven.localMavenSnapshots){
authentication(userName: user, password: password)
}
}
}
}
执行./gradlew uploadArchives
,等待打包完成查看$USER_HOME/.mymaven/snapshotRepository
完成了
接下来详细说明,不想学点Gradle用法的飘过。
环境变量
不配置也是可以的还有gradlew
嘛飘过。
GRADLE_HOME =
GRADLE_USER_HOME =
PATH = GRADLE_HOME/bin
Gradle属性配置文件
Gradle 的属性文件gradle.properties
,在不同位置作用范围不一样
1). 全局配置
全局的Gradle属性文件默认在USER_HOME/.gradle/gradle.properties
没有的话可一新建一个,这个配置在任何一个gradle
文件都能读取到
2). Project配置
工程根目录下的gradle.properties文件,Project可读取,Module读取需要加上rootProject.
3). Module配置
同上
打包文档
1). 。。。
task javadoc(type: Javadoc) {
//title = ''
//description = ''
source = android.sourceSets.main.java.srcDirs
classpath += project.files(configurations.provided.files,configurations.compile.files,android.getBootClasspath().join(File.pathSeparator))
options {
encoding 'utf-8'
charSet 'utf-8'
links 'http://docs.oracle.com/javase/7/docs/api/'
linksOffline "https://developer.android.com/reference", "${android.sdkDirectory}/docs/reference"
}
exclude '**/BuildConfig.java'
exclude '**/R.java'
options.encoding = 'utf-8'
failOnError false
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
}
2). 。。。
android.libraryVariants.all { variant ->
println variant.javaCompile.classpath.files
if(variant.name == 'release') { //我们只需 release 的 javadoc
task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) {
// title = ''
// description = ''
source = variant.javaCompile.source
classpath = files(variant.javaCompile.classpath.files, project.android.getBootClasspath())
options {
encoding "utf-8"
links "http://docs.oracle.com/javase/7/docs/api/"
linksOffline "http://d.android.com/reference", "${android.sdkDirectory}/docs/reference"
}
exclude '**/BuildConfig.java'
exclude '**/R.java'
failOnError false
}
task("javadoc${variant.name.capitalize()}Jar", type: Jar, dependsOn: "generate${variant.name.capitalize()}Javadoc") {
classifier = 'javadoc'
from tasks.getByName("generate${variant.name.capitalize()}Javadoc").destinationDir
}
artifacts {
archives tasks.getByName("javadoc${variant.name.capitalize()}Jar")
}
}
}
打包源码
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts {
archives sourcesJar
}
上传到本地Maven库
apply plugin: 'maven'
group = "cn.wittyneko"
archivesBaseName = 'auto-layout'
version = '1.0.0-SNAPSHOT'
uploadArchives {
repositories {
mavenDeployer {
String user = 'admin'
String password = 'adminpsd'
//先读取gradle.properties,不存在则读取local.properties
Properties properties = rootProject.ext.android.properties
user = rootProject.hasProperty('localUserName') ? rootProject.localUserName : (properties != null ? properties.getProperty('localUserName', user) : user)
password = rootProject.hasProperty('localPassword') ? rootProject.localPassword : (properties != null ? properties.getProperty('localPassword', password) : password)
println("user: $user, passowrd: $password")
repository(url: rootProject.ext.maven.localMaven){
authentication(userName: user, password: password)
}
snapshotRepository(url: rootProject.ext.maven.localMavenSnapshots){
authentication(userName: user, password: password)
}
}
}
}
参考文献
Android Studio使用Gradle上传AAR至Maven
Android 项目打包到 JCenter 的坑
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。转载请保留作者及原文链接