打包在本地的優(yōu)勢(shì)
- 不用搭建Maven庫(kù)環(huán)境(當(dāng)然想麻煩點(diǎn)搭建一個(gè)內(nèi)網(wǎng)環(huán)境也是可以的)
- 無(wú)網(wǎng)絡(luò)時(shí)可用(如果還沒(méi)用鏡像這個(gè)時(shí)間絕對(duì)很痛苦)
- 不需要審核(只是個(gè)人使用jcenter什么的太浪費(fèi)時(shí)間)
- 隨時(shí)修改源碼(第三方庫(kù)的bug不能及時(shí)修復(fù),那就自己打包一個(gè))
- 減少編譯時(shí)間(不需要等待Library Module編譯了)
快速使用
先來(lái)個(gè)栗子 libs-android
首先吧config.gradle
復(fù)制到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)
}
}
}
}
執(zhí)行./gradlew uploadArchives
,等待打包完成查看$USER_HOME/.mymaven/snapshotRepository
完成了
接下來(lái)詳細(xì)說(shuō)明侧戴,不想學(xué)點(diǎn)Gradle用法的飄過(guò)宁昭。
環(huán)境變量
不配置也是可以的還有gradlew
嘛飄過(guò)。
GRADLE_HOME =
GRADLE_USER_HOME =
PATH = GRADLE_HOME/bin
Gradle屬性配置文件
Gradle 的屬性文件gradle.properties
酗宋,在不同位置作用范圍不一樣
1). 全局配置
全局的Gradle屬性文件默認(rèn)在USER_HOME/.gradle/gradle.properties
沒(méi)有的話可一新建一個(gè)积仗,這個(gè)配置在任何一個(gè)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庫(kù)
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)
}
}
}
}
參考文獻(xiàn)
Android Studio使用Gradle上傳AAR至Maven
Android 項(xiàng)目打包到 JCenter 的坑
本作品采用知識(shí)共享署名-非商業(yè)性使用-相同方式共享 4.0 國(guó)際許可協(xié)議進(jìn)行許可。轉(zhuǎn)載請(qǐng)保留作者及原文鏈接