Gradle问题合集与解决方法录标题)
module类型的区分
Android application module:
apply plugin: 'com.android.application'
Android library module:
apply plugin: 'com.android.library'
Java library module:
apply plugin: 'java'
版本号管理
如果有多个module,在root里面申明全局变量,可以在单独的gradle里(比如新建一个dependency.gradle)申明然后apply from引用进来,或者直接定义在root的build.gradle中
project.ext {
applicationId = "com.xxx"
buildToolsVersion = "23.0.2"
compileSdkVersion = 23
minSdkVersion = 14
targetSdkVersion = 23
versionCode = 1
versionName = "1.0.0"
abortOnLintError = false
checkLintRelease = false
useJack = false
abortOnLintError = false
javaVersion = JavaVersion.VERSION_1_8
...
}
在子module里面则使用rootProject.ext去进行引用:
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId rootProject.ext.applicationId
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
multiDexEnabled true
}
compileOptions {
sourceCompatibility rootProject.ext.javaVersion
sourceCompatibility rootProject.ext.javaVersion
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
lintOptions {
abortOnError rootProject.ext.abortOnLintError
checkReleaseBuilds rootProject.ext.checkLintRelease
quiet true
ignoreWarnings true
// Some libraries have issues with this.
disable 'InvalidPackage'
// Lint gives this warning but SDK 20 would be Android L Beta.
disable 'OldTargetApi'
}
...
}
依赖管理
引用多个第三方库,需要统一定义依赖
def daggerVersion = "2.0.2"
def retrofitVersion = "2.0.0-beta4"
def supportVersion = "23.2.1"
def rxBindingVersion = '0.4.0'
def leakCanaryVersion = "1.3.1"
def blockCanaryVersion = '1.1.4'
project.ext {
...
libSupportAppcompat = "com.android.support:appcompat-v7:${supportVersion}"
libSupportDesign = "com.android.support:design:${supportVersion}"
libSupportRecyclerview = "com.android.support:recyclerview-v7:${supportVersion}"
libSupportV4 = "com.android.support:support-v4:${supportVersion}"
libRxAndroid = "io.reactivex:rxandroid:1.1.0"
libRxJava = "io.reactivex:rxjava:1.1.1"
libEventBus = "org.greenrobot:eventbus:3.0.0"
libJavaxAnnotation = "javax.annotation:jsr250-api:1.0"
libGson = "com.google.code.gson:gson:2.4"
libRetrofit = "com.squareup.retrofit2:retrofit:${retrofitVersion}"
libRetrofitConverterGson = "com.squareup.retrofit2:converter-gson:${retrofitVersion}"
libRetrofitAdapterRxJava = "com.squareup.retrofit2:adapter-rxjava:${retrofitVersion}"
libOkHttpLoggingInterceptor = "com.squareup.okhttp3:logging-interceptor:3.0.0-RC1"
libDagger = "com.google.dagger:dagger:${daggerVersion}"
libDaggerCompiler = "com.google.dagger:dagger-compiler:${daggerVersion}"
libGlide = "com.github.bumptech.glide:glide:3.7.0"
libRxBinding = "com.jakewharton.rxbinding:rxbinding:${rxBindingVersion}"
libRxBindingSupportV4 = "com.jakewharton.rxbinding:rxbinding-support-v4:${rxBindingVersion}"
libRxBindingAppcompatV7 = "com.jakewharton.rxbinding:rxbinding-appcompat-v7:${rxBindingVersion}"
libRxBindingDesign = "com.jakewharton.rxbinding:rxbinding-design:${rxBindingVersion}"
libRxBindingRecyclerview = "com.jakewharton.rxbinding:rxbinding-recyclerview-v7:${rxBindingVersion}"
libRealm = "io.realm:realm-android:0.87.5"
debugDependencies = [
leakCanary: "com.squareup.leakcanary:leakcanary-android:${leakCanaryVersion}",
blockcanary: "com.github.moduth:blockcanary-ui:${blockCanaryVersion}",
]
releaseDependencies = [
leakCanary: "com.squareup.leakcanary:leakcanary-android-no-op:${leakCanaryVersion}",
blockcanary: "com.github.moduth:blockcanary-no-op:${blockCanaryVersion}",
]
}
module中使用:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
...
apt rootProject.ext.libDaggerCompiler
compile rootProject.ext.libDagger
compile rootProject.ext.libRxJava
compile rootProject.ext.libRxAndroid
compile rootProject.ext.libRxBinding
compile rootProject.ext.libGlide
provided rootProject.ext.libJavaxAnnotation
compile rootProject.ext.libSupportAppcompat
compile rootProject.ext.libSupportDesign
compile rootProject.ext.libSupportRecyclerview
compile rootProject.ext.libSupportV4
debugCompile rootProject.ext.debugDependencies.leakCanary
releaseCompile rootProject.ext.releaseDependencies.leakCanary
debugCompile rootProject.ext.debugDependencies.blockCanary
releaseCompile rootProject.ext.releaseDependencies.blockCanary
}
Java8支持
对Android的module
apply plugin: 'me.tatarka.retrolambda'
android {
compileOptions {
sourceCompatibility rootProject.ext.javaVersion
sourceCompatibility rootProject.ext.javaVersion
}
}
对Java的module:
sourceCompatibility = 1.8
targetCompatibility = 1.8
Split APK
个人使用:
splits {
abi {
enable true
reset()
include 'armeabi', 'x86' //, 'x86', 'armeabi-v7a', 'mips'
universalApk false
}
}
Module aar依赖
通过module式aar依赖,在library module目录下,打开build/outputs/aar,把它放到module目录下面,然后在build.gradle里面:
configurations.maybeCreate("default")
artifacts.add("default", file('lib_authorize-debug.aar'))
总结
本文章介绍了我在学习gradle遇到的各种问题和解决方法,希望能给各位启发