For the properties of gradle, you can check the documentation by yourself. I mainly summarize some commonly used ones from the documentation.
Documentation: http://google.github.io/android-gradle-dsl/
The current Android Studio version 3.0 and above has used the gradle plugin above 3.0, so look directly at the latest documentation. Note: The attributes of this article, whether application or library, are under android{}. There are very few properties that are only available in the library.
aaptOptions
property | Description |
---|---|
additionalParameters | Specify the parameters of aapt, which can be viewed through aapt –help |
cruncherEnabled | 3.1 Deprecated, replaced by BuildType.isCrunchPngs() |
cruncherProcesses | Specifies the number of processes to compress images |
failOnMissingConfigEntry | I don't know, it should be looking for the config entry of the apk. If an error is reported, it will return. I know the great god, please comment and let me know |
ignoreAssets/ignoreAssets | These two calls the same method, there is no difference, ignore the assets file, you can use wildcards |
noCompress | Specify uncompressed files, specifying "" will try to turn off all file compression |
2. adbExecutable
A method that returns File, specifically the path of adb.exe, feels useless.
3. adbOptions
property | Description |
---|---|
installOptions | The parameters when installing apk can be viewed through adb –help |
timeOutInMs | timeout for adb operations |
四、applicationVariants
All build variants of the project, at a glance
applicationVariants.all{
println it.name
}
五、buildToolsVersion
The version of the SDK build tool, 3.1 can not be specified, the default is 27.0.3, I don't know if the latest version of my computer is 27.0.3.
六、buildTypes
Regarding build variants, the documentation on the official website is very detailed, I won't explain it, just get on the car https://developer.android.com/studio/build/build-variants.html#build-types , just say other properties.
property | Description |
---|---|
applicationIdSuffix | suffix added by applicationId |
consumerProguardFiles | Add obfuscated files |
crunchPngs | Whether to compress pictures |
debuggable | Whether to allow debugging |
jniDebuggable | Whether to allow debugging of jni code |
manifestPlaceholders | manifest file placeholder |
matchingFallbacks | This is an attribute only available after 3.0.0, specifying the corresponding buildType. For example, if the app module has a staged buildType, and the library does not have a staged buildType, an error will be reported. Unable to resolve dependency for ':app@stage/compileClasspath': Could not resolve project :library. This can be specified by this property. |
minifyEnabled | Enable obfuscation optimization |
multiDexEnabled/multiDexKeepFile/multiDexKeepProguard | Multidex attribute specification, refer to the official website: https://developer.android.com/studio/build/multidex |
postprocessing | The documentation doesn't say, it may change in the future, no matter what |
proguardFiles | Specify the obfuscation file, by default these two proguard-android.txt proguard-android-optimize.txt |
postprocessing | The documentation doesn't say, it may change in the future, no matter what |
pseudoLocalesEnabled | The UI can be adjusted for the localized language text, such as left-aligned or right-aligned text |
renderscriptDebuggable/renderscriptOptimLevel | 渲染脚本相关,反正没用过 |
shrinkResources | 压缩资源,必须开启minifyEnabled才有用 |
signingConfig | 签名配置 |
testCoverageEnabled | 测试覆盖率 |
useProguard | 使用instant run的时候可以用此变量关闭混淆 |
versionNameSuffix | 版本名后缀 |
zipAlignEnabled | 优化对齐,可以减少apk内存使用 |
method | usage |
---|---|
void buildConfigField(String type, String name, String value) | 为生成的BuildConfig类添加属性,一般来说一个module都会生成一个BuildConfig类,可以参考https://blog.csdn.net/a568478312/article/details/74989722 |
BuildType consumerProguardFiles(Object… proguardFiles) | 往application project传递proguard文件,该属性只在library project有效 |
ExternalNativeBuildOptions externalNativeBuild(Action action) | native参数配置,后面说 |
DefaultBuildType initWith(BuildType that) | 复制传递的buildType所有属性 |
BuildType proguardFiles(Object… files) | 添加新的proguard文件 |
void resValue(String type, String name, String value) | 添加新的生成资源 |
BuildType setProguardFiles(Iterable proguardFileIterable) | 设置混淆文件 |
1、externalNativeBuild
注意这里是buildType下面的属性,通过文档看这个和android{}下的有区别。
property | Description |
---|---|
cmake | cmake配置参数 |
ndkBuild | ndk-build配置参数 |
这两个的属性都是下面几个
- abiFilters 指定cpu架构
- arguments 指定对应的参数
- cFlags c编译的flags
- cppFlags c++编译的flags
- targets 指定需要编译的可执行文件
七、 compileOptions
property | usage |
---|---|
encoding | java的编码 |
incremental | java编译是否使用gradle的增量模型 |
sourceCompatibility | java原文件的语言版本 |
targetCompatibility | java字节码的语言版本 |
八、compileSdkVersion
指定编译的api 版本
九、dataBinding
一个xml绑定数据的库,参考https://developer.android.com/topic/libraries/data-binding/
(十) defaultConfig
这里有部分和buildType想同,所以只列出特有的部分。
property | Description |
---|---|
applicationId | 应用的id |
dimension | 风味维度,这个参考上面的构建变体结合使用 |
javaCompileOptions | 这里只能配置annotationProcessorOptions 注解处理器的参数,然而文档里直接什么都没有写,不明所以 |
ndk | 只能指定abifilter |
testApplicationId/testApplicationId/testApplicationId/testApplicationId/testInstrumentationRunnerArguments | 几个测试的,没有怎么用过,不了解 |
vectorDrawables | 构建vectorDrawalbe的支持,有两个属性。generatedDensities:指定生成PNG的屏幕密度;useSupportLibrary:使用运行时的库对vectorDrawable的支持。 |
versionCode | 版本号 |
versionName | 版本名 |
versionNameSuffix | 版本名后缀 |
method | usage |
---|---|
versionCode | 版本号 |
versionName | 版本名 |
versionNameSuffix | 版本名后缀 |
void maxSdkVersion(int maxSdkVersion) | 最大支持的api版本 |
void minSdkVersion(int minSdkVersion) | 最小支持的api版本 |
void missingDimensionStrategy(String dimension, String requestedValue) | gradle3.0.0以上版本才有,指定维度缺失策略,也就是说app有的Dimension而library里没有找到对应的时候,就靠这个来指定策略,可以在productFlavors设置来单独覆盖 |
void resConfig(String config) | 指定可选的资源来进行保留,用来排除某些不需要的语言资源等 |
void targetSdkVersion(int targetSdkVersion) | 指定target的api版本,比如如果设置小于23,那么动态申请权限将不会应用 |
十一、defaultPublishConfig
指定发布到远程仓库的版本名,不指定默认发布release,如果设置了productFlavors,则需要明确指出。本地模块不需要指定此属性。
十二、dexOptions
property | Description |
---|---|
additionalParameters | dx命令的参数 |
javaMaxHeapSize | 指定虚拟机的-Xmx参数,例如 2048m |
jumboMode | 该模式会将字符串数量由2^16增大到2^32 |
keepRuntimeAnnotatedClasses | 保留运行时的注解 |
maxProcessCount | dex的最大进程数,默认4 |
preDexLibraries | 预编译库 |
threadCount | dx的线程数,默认4 |
十三、externalNativeBuild
注意这个快是在android{}下面的,属性如下:
1.cmake
- buildStagingDirectory 指定native编译输出目录
- path 指定cmakeLists.txt的相对路径
- version 指定使用CMake的版本呢
2.ndkBuild
- buildStagingDirectory 指定native编译输出目录
- path 指定Android.mk的相对路径
十四、flavorDimensionList
通过使用flavorDimensions指定维度列表,参考buildType的变体构建,结合使用。
十五、lintOptions
property | Description |
---|---|
abortOnError | 发现错误是否停止 |
absolutePaths | 错误输出是否显示完整路径 |
check | 指定需要检查的id,可以通过lint –list查看所有支持的id |
checkAllWarnings | 是否检查所有警告 |
checkReleaseBuilds | 是否检查releast的构建 |
disable | 禁止对应id的检查 |
enable | 使能对应id的检查 |
explainIssues | 是否需要错误解释,注意html和xml报告是无条件有解释的,忽略这个设置的。 |
htmlOutput | html报告的输出路径 |
htmlReport | 是否需要html报告 |
ignoreWarnings | 是否忽略警告 |
lintConfig | 配置默认lint配置文件,可以参考https://developer.android.com/studio/write/lint#pref |
noLines | 是否包含资源行号 |
quiet | 是否使用安静模式,例如不输出路径或者报告文件等 |
severityOverrides | 覆盖默写id的等级,比如原本资源id为error的覆盖成fatal |
showAll | 是否需要包含所有输出 |
textOutput | text报告输出路径 |
textReport | 是否需要text报告 |
warningsAsErrors | 是否将警告置为错误来处理 |
xmlOutput | xml报告路径 |
xmlReport | 是否需要xml报告 |
Methods | Description |
---|---|
check | 添加需要检查的id |
disable | 添加禁用的id |
enable | 添加使能id |
error | 将指定id的等级改为error |
fatal | 将指定id的等级改为fatal |
ignore | 将指定id的等级改为ignore |
warning | 将指定id的等级改为warning |
十六、ndkDirectory
配置gradle使用的ndk目录
十七、packagingOptions
这个主要是处理打包时候的一些文件冲突。
property | Description |
---|---|
excludes | 排除文件 |
merges | 合并发现的冲突文件 |
pickFirsts | 发现冲突首选第一次找到的文件 |
十八、productFlavors
同样参考构建变体。
十九、resourcePrefix
指定资源前缀,主要是用来防止library的资源和其他module产生冲突的时候使用。
二十、sdkDirectory
配置Gradle使用的SDK目录。
二十一、signingConfigs
配置签名,这个就不展开说了,搜一下一堆。
二十二、sourceSets
配置各种文件的目录,平时用到都是默认目录,可以使用gradlew sourceSet查看默认目录。这个看需求来定是否需要配置,也不展开说了,直接百度一堆。
二十三、splits
配置apk拆分选项,可以通过区分api,density,language等来减少apk大小。具体使用可以参考
https://developer.android.com/studio/build/configure-apk-splits
https://developer.android.com/topic/instant-apps/guides/config-splits
二十四、variantFilter
配置Android插件需要保留或者排除的变体。其实就是过滤变体罢了。
android {
...
variantFilter { variant ->
def buildTypeName = variant.buildType*.name
def flavorName = variant.flavors*.name
if (flavorName.contains("dev") && buildTypeName.contains("release")) {
// Tells Gradle to ignore each variant that satisfies the conditions above.
setIgnore(true)
}
}
}
二十五、useLibrary
在classpath指定library,基本上没什么卵用。
android {
// Adds a platform library that ships with the Android SDK.
useLibrary 'org.apache.http.legacy'
}