Last active
December 3, 2018 03:26
-
-
Save AoEiuV020/0c953acca25d7a412be5cd7962465035 to your computer and use it in GitHub Desktop.
Android通用gradle配置,包括库模块打包发布,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Top-level build file where you can add configuration options common to all sub-projects/modules. | |
buildscript { | |
ext { | |
// 统一配置所有安卓应用模块, | |
applicationModules = ['app'] | |
// 统一配置所有javay库模块, | |
javaModules = ['jarlibrary'] | |
// 统一配置所有android库模块, | |
androidModules = ['aarlibrary'] | |
// 读取所有库的版本信息, | |
def versionFile = rootProject.file('version.properties') | |
if (versionFile.exists()) { | |
def input = versionFile.newInputStream() | |
def p = new Properties() | |
p.load(input) | |
p.forEach { k, v -> | |
ext.set(k, v) | |
} | |
} | |
// 读取上传仓库相关配置,示例在publish.properties.example, | |
def publishFile = rootProject.file('publish.properties') | |
hasPublish = publishFile.exists() | |
if (hasPublish) { | |
def input = publishFile.newInputStream() | |
pub = new Properties() | |
pub.load(input) | |
} | |
} | |
repositories { | |
google() | |
jcenter() | |
} | |
dependencies { | |
//noinspection GradleDependency | |
classpath 'com.android.tools.build:gradle:' + studio_version | |
//noinspection DifferentKotlinGradleVersion | |
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:' + kotlin_version | |
// 支持安卓库打aar包, | |
classpath 'com.github.dcendents:android-maven-gradle-plugin:' + jitpack_android_version | |
// 使用dokka给kotlin模块生成javadoc文档, | |
classpath 'org.jetbrains.dokka:dokka-gradle-plugin:' + dokka_version | |
// 使用dokka给kotlin-android模块生成javadoc文档, | |
classpath 'org.jetbrains.dokka:dokka-android-gradle-plugin:' + dokka_version | |
// 使用bintray-release上传bintray也就是jcenter, | |
classpath 'com.novoda:bintray-release:' + bintray_version | |
} | |
} | |
allprojects { | |
// 上传仓库的组名优先从publish.properties中拿, | |
group = 'cc.aoeiuv020' | |
version = version_name | |
repositories { | |
google() | |
jcenter() | |
maven { url "https://jitpack.io" } | |
// 我自己的bintray仓库,库同步到jcenter审核前可以从这个仓库获取, | |
maven { url "https://dl.bintray.com/aoeiuv020/maven" } | |
} | |
// 固定编译器识别编码utf-8, | |
tasks.withType(JavaCompile) { | |
options.encoding = 'UTF-8' | |
} | |
tasks.withType(Javadoc) { | |
// 禁用原生的javadoc,只使用dokka, | |
enabled = false | |
options { | |
encoding 'UTF-8' | |
charSet 'UTF-8' | |
links "http://docs.oracle.com/javase/7/docs/api" | |
} | |
} | |
} | |
// app模块, | |
// 多个app模块可能导致AndroidStudio3.1.3右侧gradle面板的任务崩溃,原因和细节不明, | |
// 不影响AndroidStudio的run, 和gradlew命令行, | |
applicationModules.forEach { name -> | |
project(name) { | |
apply plugin: 'com.android.application' | |
apply plugin: 'kotlin-android' | |
apply plugin: 'kotlin-android-extensions' | |
apply plugin: 'kotlin-kapt' | |
android { | |
compileSdkVersion compile_version.toInteger() | |
flavorDimensions "tier" | |
defaultConfig { | |
minSdkVersion min_version.toInteger() | |
targetSdkVersion target_version.toInteger() | |
versionCode version_code.toInteger() | |
versionName version | |
multiDexEnabled true | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
setProperty("archivesBaseName", "$rootProject.name-$versionName") | |
vectorDrawables.useSupportLibrary = true | |
} | |
buildTypes { | |
release { | |
// 删除无用类, | |
minifyEnabled true | |
// 禁用混淆, | |
useProguard false | |
// 删除无用资源, | |
shrinkResources true | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
// 签名信息从signing.properties中获取, | |
// debug和release使用相同签名,以便用debug包覆盖release包从而调试, | |
// 如果没有,就会使用默认debug签名, | |
def signingFile = rootProject.file('signing.properties') | |
if (signingFile.exists()) { | |
def input = signingFile.newInputStream() | |
def p = new Properties() | |
p.load(input) | |
signingConfigs { | |
config { | |
keyAlias p['keyAlias'] | |
keyPassword p['keyPassword'] | |
storeFile file(p['storeFile']) | |
storePassword p['storePassword'] | |
v1SigningEnabled true | |
v2SigningEnabled true | |
} | |
} | |
buildTypes { | |
debug { | |
signingConfig signingConfigs.config | |
} | |
release { | |
signingConfig signingConfigs.config | |
} | |
} | |
} | |
// release打包后复制出安装包和混淆映射mapping文件到release目录下, | |
// 以便发布上传, | |
applicationVariants.all { variant -> | |
if (variant.buildType.name == 'release') { | |
variant.assemble.doLast { | |
copy { | |
from variant.outputs[0].outputFile | |
into "${rootDir}/release" | |
} | |
if (variant.mappingFile != null) { | |
copy { | |
from variant.mappingFile | |
into "${rootDir}/release" | |
rename { String fileName -> | |
"$rootProject.name-mapping-$versionName-${variant.name}.txt" | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
// pc端的单元测试移除无法使用的slf4j-android, | |
// 关键是runtimeOnly依赖不只加入apk中,test也会加上, | |
// 在allprojects设置没用,apply plugin后才存在这些configurations, | |
configurations.findAll { it.name.startsWith('test') }.each { conf -> | |
conf.exclude group: "org.slf4j", module: "slf4j-android" | |
} | |
dependencies { | |
// support-v4, | |
implementation 'com.android.support:support-v4:' + support_version | |
// appcompat-v7, | |
implementation 'com.android.support:appcompat-v7:' + support_version | |
// 单元测试junit, | |
testImplementation 'junit:junit:' + junit_version | |
// 单元测试mock, | |
testImplementation 'org.mockito:mockito-core:' + mockito_version | |
// 安卓测试, | |
androidTestImplementation 'com.android.support.test:runner:' + test_runner_version | |
androidTestImplementation 'com.android.support.test.espresso:espresso-core:' + test_espresso_version | |
// 安卓分包, | |
implementation 'com.android.support:multidex:' + multidex_version | |
// kotlin开发语言支持, | |
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:' + kotlin_version | |
// kotlin专用的安卓工具集,anko-commons, | |
implementation 'org.jetbrains.anko:anko-commons:' + anko_version | |
// 通用日志框架slf4j, 的接口层, | |
implementation 'org.slf4j:slf4j-api:' + slf4j_version | |
// slf4j的安卓实现, | |
runtimeOnly 'org.slf4j:slf4j-android:' + slf4j_version | |
// slf4j的控制台简单实现, | |
testImplementation 'org.slf4j:slf4j-simple:' + slf4j_version | |
// 自己的kotlin通用工具集, | |
implementation 'cc.aoeiuv020:commons:' + a_commons_version | |
} | |
} | |
} | |
// java模块, | |
javaModules.forEach { name -> | |
project(name) { | |
apply plugin: 'java-library' | |
apply plugin: 'kotlin' | |
apply plugin: 'maven' | |
apply plugin: 'org.jetbrains.dokka' | |
apply plugin: 'com.novoda.bintray-release' | |
dependencies { | |
// 单元测试junit, | |
testImplementation 'junit:junit:' + junit_version | |
// kotlin开发语言支持, | |
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:' + kotlin_version | |
// 通用日志框架slf4j, 的接口层, | |
implementation 'org.slf4j:slf4j-api:' + slf4j_version | |
// slf4j的控制台简单实现, | |
testImplementation 'org.slf4j:slf4j-simple:' + slf4j_version | |
// 自己的kotlin通用工具集, | |
implementation 'cc.aoeiuv020:commons:' + a_commons_version | |
} | |
sourceCompatibility = "1.7" | |
targetCompatibility = "1.7" | |
// 生成源码包, | |
task sourcesJar(type: Jar, dependsOn: classes) { | |
group = 'jar' | |
classifier = 'sources' | |
from sourceSets.main.allSource | |
} | |
// 生成dokka日志, | |
task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaTask) { | |
jdkVersion = 7 | |
outputFormat = 'javadoc' | |
outputDirectory = "$buildDir/dokkadoc" | |
} | |
// 生成日志包, | |
task javadocJar(type: Jar, dependsOn: dokkaJavadoc) { | |
group = 'jar' | |
classifier = 'javadoc' | |
from "$buildDir/dokkadoc" | |
} | |
// 指定收集上传源码包和日志包, | |
artifacts { | |
archives sourcesJar | |
archives javadocJar | |
} | |
// 配置上传bintray也就是jcenter, | |
// 如果没有publish.properties或者没有key属性,就不上传,也就是dryRun=true, | |
publish { | |
// 都是publish.properties优先的,这里赋值避免没有publish.properties时gradle同步失败, | |
userOrg = 'aoeiuv020' | |
bintrayUser = userOrg | |
artifactId = name | |
publishVersion = version | |
desc = "Java library $name." | |
website = 'https://github.com/AoEiuV020/AndroidDemo' | |
groupId = group | |
if (hasPublish) { | |
dryRun = false | |
// publish.properties里和这个publish闭包参数同名,参考bintray-release官方wiki, | |
// https://github.com/HailouWang/bintray-release/wiki/%E9%85%8D%E7%BD%AEpublish%E9%97%AD%E5%8C%85 | |
properties.forEach { key, value -> | |
if (pub.getProperty(key)?.trim()) { | |
setProperty(key, pub.getProperty(key)) | |
} | |
} | |
// 字符串转成数组,逗号分隔, | |
// 因为配置文件无法存数组, | |
if (pub.licences?.trim()) { | |
licences = pub.licences.split(',') | |
} | |
} | |
// 如果没有配置bintrayKey就不上传, | |
if (!bintrayKey?.trim()) { | |
dryRun = true | |
} | |
} | |
// 删除bintray-release插件自带的javadocJar和sourceJar任务, | |
// 因为不支持kotlin, 不支持安卓, | |
bintrayUpload.dependsOn('assemble') | |
tasks.whenTaskAdded { task -> | |
if ([ | |
'genereateJavadocsJar', | |
'genereateSourcesJar' | |
].any { | |
task.name.startsWith(it) | |
}) { | |
task.enabled = false | |
} | |
} | |
} | |
} | |
// android模块, | |
androidModules.forEach { name -> | |
project(name) { | |
apply plugin: 'com.android.library' | |
apply plugin: 'kotlin-android' | |
apply plugin: 'kotlin-android-extensions' | |
apply plugin: 'com.github.dcendents.android-maven' | |
apply plugin: 'org.jetbrains.dokka-android' | |
apply plugin: 'com.novoda.bintray-release' | |
android { | |
compileSdkVersion compile_version.toInteger() | |
defaultConfig { | |
minSdkVersion min_version.toInteger() | |
targetSdkVersion target_version.toInteger() | |
versionCode 1 | |
versionName version | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
// pc端的单元测试移除无法使用的slf4j-android, | |
// 关键是runtimeOnly依赖不只加入apk中,test也会加上, | |
// 在allprojects设置没用,apply plugin后才存在这些configurations, | |
configurations.findAll { it.name.startsWith('test') }.each { conf -> | |
conf.exclude group: "org.slf4j", module: "slf4j-android" | |
} | |
dependencies { | |
// 单元测试junit, | |
testImplementation 'junit:junit:' + junit_version | |
// 安卓测试, | |
androidTestImplementation 'com.android.support.test:runner:' + test_runner_version | |
androidTestImplementation 'com.android.support.test.espresso:espresso-core:' + test_espresso_version | |
// kotlin开发语言支持, | |
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:' + kotlin_version | |
// kotlin专用的安卓工具集,anko-commons, | |
implementation 'org.jetbrains.anko:anko-commons:' + anko_version | |
// 通用日志框架slf4j, 的接口层, | |
implementation 'org.slf4j:slf4j-api:' + slf4j_version | |
// slf4j的安卓实现, | |
runtimeOnly 'org.slf4j:slf4j-android:' + slf4j_version | |
// slf4j的控制台简单实现, | |
testImplementation 'org.slf4j:slf4j-simple:' + slf4j_version | |
// 自己的kotlin通用工具集, | |
implementation 'cc.aoeiuv020:commons:' + a_commons_version | |
} | |
// 生成源码包, | |
task sourcesJar(type: Jar) { | |
group = 'jar' | |
from android.sourceSets.main.java.srcDirs | |
classifier = 'sources' | |
} | |
// 生成dokka日志, | |
task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaAndroidTask) { | |
jdkVersion = 7 | |
outputFormat = 'javadoc' | |
outputDirectory = "$buildDir/dokkadoc" | |
} | |
// 生成日志包, | |
task javadocJar(type: Jar, dependsOn: dokkaJavadoc) { | |
group = 'jar' | |
classifier = 'javadoc' | |
from "$buildDir/dokkadoc" | |
} | |
// 指定收集上传源码包和日志包, | |
artifacts { | |
archives sourcesJar | |
archives javadocJar | |
} | |
// 配置上传bintray也就是jcenter, | |
// 如果没有publish.properties或者没有key属性,就不上传,也就是dryRun=true, | |
publish { | |
// 都是publish.properties优先的,这里赋值避免没有publish.properties时gradle同步失败, | |
userOrg = 'aoeiuv020' | |
bintrayUser = userOrg | |
artifactId = name | |
publishVersion = version | |
desc = "Android library $name." | |
website = 'https://github.com/AoEiuV020/AndroidDemo' | |
groupId = group | |
if (hasPublish) { | |
dryRun = false | |
// publish.properties里和这个publish闭包参数同名,参考bintray-release官方wiki, | |
// https://github.com/HailouWang/bintray-release/wiki/%E9%85%8D%E7%BD%AEpublish%E9%97%AD%E5%8C%85 | |
properties.forEach { key, value -> | |
if (pub.getProperty(key)?.trim()) { | |
setProperty(key, pub.getProperty(key)) | |
} | |
} | |
// 字符串转成数组,逗号分隔, | |
// 因为配置文件无法存数组, | |
if (pub.licences?.trim()) { | |
licences = pub.licences.split(',') | |
} | |
} | |
// 如果没有配置bintrayKey就不上传, | |
if (!bintrayKey?.trim()) { | |
dryRun = true | |
} | |
} | |
// 删除bintray-release插件自带的javadocJar和sourceJar任务, | |
// 因为不支持kotlin, 不支持安卓, | |
bintrayUpload.dependsOn('assemble') | |
tasks.whenTaskAdded { task -> | |
if ([ | |
'genereateJavadocsJar', | |
'genereateSourcesJar' | |
].any { | |
task.name.startsWith(it) | |
}) { | |
task.enabled = false | |
} | |
} | |
} | |
} | |
task clean(type: Delete) { | |
delete rootProject.buildDir | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
repo