用Gradle构建Android Studio项目,默认会有一个基础配置,那么在这个基础上,我们通过将变量聚合在一起,既明了又简洁。
文章目录
* 一、配置源码编译路径 <https://blog.csdn.net/sslinp/article/details/84865724#_4>
* 配置资源目录 <https://blog.csdn.net/sslinp/article/details/84865724#_6>
* 二、配置编译版本和依赖版本为全局变量
<https://blog.csdn.net/sslinp/article/details/84865724#_26>
* 三、打包相关的一些配置 <https://blog.csdn.net/sslinp/article/details/84865724#_69>
* 四、Gradle编译兼容性配置
<https://blog.csdn.net/sslinp/article/details/84865724#Gradle_85>
* 1. lint检查 <https://blog.csdn.net/sslinp/article/details/84865724#1_lint_86>
* 2. 依赖仓库 <https://blog.csdn.net/sslinp/article/details/84865724#2__99>
<>一、配置源码编译路径
我们都知道,AS会有默认的源码编译路径,比如java的在main/java下,.so库的引用在main/jniLibs下,如下修改它的引用地址:
<>配置资源目录
学会配置资源编译路径,在jni的库和src编译路径,马甲包的编译等非常实用
android { sourceSets.main { //默认是jniLibs,修改为libs目录 jniLibs.srcDir
'src/main/libs' //disable automatic ndk-build call //当手动编写NDK编译指令时设置
jni.srcDirs = [] manifest.srcFile 'src/main/AndroidManifest.xml' java.srcDirs =
['src/main/java'] resources.srcDirs = ['src/main/resources'] aidl.srcDirs =
['src/main/aidl'] renderscript.srcDirs = ['src/main/renderscript'] res.srcDirs
= ['src/main/res'] assets.srcDirs = ['src/main/assets'] } }
<>二、配置编译版本和依赖版本为全局变量
通常我们构建一个上线项目必定包含多个Modules和第三方库的依赖,有时候依赖或者编译版本冲突时,手动改,很麻烦,一个自动化的项目,必定需要统一配置,以下通过一个gradle全局的配置来实现这个操作。
*
我们在主工程目录App: build.gradle中配置全局的版本参数
ext { supportLibVersion = '27.0.1' // variable that can be referenced to keep
support libs consistent versionBuildTool = '27.0.3' versionCompiler = 27
versionTarget = 27 versionNameString = '1.0.0' javaSourceCompatibility =
JavaVersion.VERSION_1_8 javaTargetCompatibility = JavaVersion.VERSION_1_8 }
*
在Modules的build.gradle中使用
android { compileSdkVersion versionCompiler buildToolsVersion
versionBuildTool //指定java版本和配置属性 compileOptions { sourceCompatibility
javaSourceCompatibility targetCompatibility javaTargetCompatibility }
defaultConfig { applicationId "com.xxx.xxx" minSdkVersion 18 targetSdkVersion
versionTarget versionCode 1 versionName versionNameString
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }
//----------忽略部分 } dependencies { api fileTree(dir: 'libs', include: ['*.jar'])
implementation "com.android.support:support-v4:${supportLibVersion}"
implementation "com.android.support:support-annotations:${supportLibVersion}" }
<>三、打包相关的一些配置
*
其中shrinkResources在我测试看来效果并不明显,如果release工程发布,可以用【Refactor】选项【Remove Unused
Resources】,在编译前移除
buildTypes { release {{//打release包 shrinkResources true // 检查并移除无用res文件,缩小apk
zipAlignEnabled true // Zipalign优化,压缩效率高一些 minifyEnabled true //是否混淆 //指定混淆文件
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro' debuggable false //是否debug模式 jniDebuggable true
//指定jni是否debug模式打包 } debug {//打debug包 } }
<>四、Gradle编译兼容性配置
<>1. lint检查
通过添加lintOptons声明,配置编译时一些不重要报错打断编译问题(个人不建议添加,因为欠债要还,不规范的地方总会有爆发的一天)
lintOptions { /** Whether lint should set the exit code of the process if
errors are found */ abortOnError false //报错打断编译选项 /** * Returns whether lint
should check for fatal errors during release builds. Default is true. * If
issues with severity "fatal" are found, the release build is aborted. */
checkReleaseBuilds false //release编译时检查 }
<>2. 依赖仓库
通过gradle我们方便的使用依赖包,但如同一起eclipse使用maven仓库时一样,我们在配置gradle的时也要声明Lib仓库。
在project下的build.gradle中声明:
buildscript { repositories { google() jcenter() //mavenCentral()
使用maven仓库的话,各有各的库,可能都有,看自己配置 } dependencies { classpath
'com.android.tools.build:gradle:3.2.0-alpha13' // NOTE: Do not place your
application dependencies here; they belong // in the individual module
build.gradle files } } allprojects { repositories { google() jcenter() } } task
clean(type: Delete) { delete rootProject.buildDir }
热门工具 换一换