Android(安卓)Studio gradle配置实践
16lz
2021-01-25
Android Studio gradle配置实践
Android Studio中gradle配置主要是app中build.gradle中的配置,以下是我们项目中的配置,作为参考。
app中build.gradle中的配置
apply plugin: 'com.android.application'//定义一个函数,加载配置def loadProperties() { //文件 def proFile = file("../local.properties") Properties p = new Properties() //读取文件 proFile.withInputStream { stream -> p.load(stream) } appReleaseDir = p.appReleaseDir}//调用加载配置文件信息loadProperties()android { //用gradle build命令时,经常由于lint错误终止,而这些错误又经常是第三方库中的,我们可以跳过这些错误,继续编译。 lintOptions { //错误不终止 abortOnError false //不检查release编译 checkReleaseBuilds false } //签名配置信息 signingConfigs { //签名配置信息是隐私信息,最好不要直接写在这个文件中,写在一个配置文件中,然后将配置文件添加在.ignore文件中,通过上面的loadProperties()函数加载配置文件) config { //签名信息写在gradle.properies文件中,eg:KEYALIAS=appname...,签名文件在末尾 keyAlias KEYALIAS keyPassword KEYPASSWORD //通过文件加载路径keystore文件 storeFile file(STOREFILEPATH) storePassword STOREPASSWORD } } //编译sdk版本 compileSdkVersion 23 //compileSdkVersion rootProject.ext.compileSdkVersion 如果项目中有多个Module可以写在project中的build.gradle配置中,进行统一管理 //编译工具版本 buildToolsVersion '23.0.2' //buildToolsVersion rootProject.ext.buildToolsVersion 如果项目中有多个Module可以写在project中的build.gradle配置中,进行统一管理 //aapt 包装资源的工具 aaptOptions.cruncherEnabled = false aaptOptions.useNewCruncher = false //默认配置信息 defaultConfig { //应用id(一般为应用包名) applicationId "......" //最小sdk版本 minSdkVersion 15 // minSdkVersion rootProject.ext.minSdkVersion 如果项目中有多个Module可以写在project中的build.gradle配置中,进行统一管理 //目标sdk版本 targetSdkVersion 19 //targetSdkVersion rootProject.ext.targetSdkVersion //版本号 versionCode 21 //版本名称 versionName '1.53' //项目存档名称 archivesBaseName = "项目名称-$versionName" ndk { moduleName "yibawifisafe" //设置库(so)文件名称 ldLibs "log", "z", "m", "jnigraphics", "android"//引入库,比如要用到的__android_log_print abiFilters "armeabi", "x86", "armeabi-v7a" cFlags "-std=c++11 -fexceptions" // C++11 stl "gnustl_static" } //使用签名配置 signingConfig signingConfigs.config //使用dex multiDexEnabled true } //源文件目录设置 sourceSets { //在main目录中 main { //assets目录设置 assets.srcDirs = ['assets'] //jni目录设置 jni.srcDirs 'src/main/jni' //jni库设置 jniLibs.srcDir 'src/main/jniLibs' } } //dex配置 dexOptions { //对lib库先进行dex拆分 preDexLibraries = false //加快编译速度 incremental true //java最大堆内存4g javaMaxHeapSize "4g" } //BuildTypes 编译类型 buildTypes { //release release { //混淆后的zip优化,默认为true,可不写 zipAlignEnabled true // 移除无用的resource文件 shrinkResources true //是否启用混淆器 minifyEnabled true //混淆文件配置 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' //是否保留调试信息 debuggable false //ndk ndk { // cFlags "-std=c++11 -fexceptions -O3 -D__RELEASE__" // C++11 // platformVersion = "19" moduleName "yibawifisafe" //设置库(so)文件名称 ldLibs "log", "z", "m", "jnigraphics", "android"//引入库,比如要用到的__android_log_print abiFilters "armeabi", "x86", "armeabi-v7a"//, "x86" cFlags "-std=c++11 -fexceptions" // C++11 stl "gnustl_static" } //如果希望可以对文件名做修改,如需要针对不同的需求生成不同的文件 applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('release.apk')) { def timeStamp = new Date().format('yyyyMMddHH'); def fileName = "WeShare-${defaultConfig.versionName}" + "-" + timeStamp + "-lj-" + ".apk"; output.outputFile = file("${outputFile.parent}/${fileName}") } } } //使用签名文件 signingConfig signingConfigs.config //jni调试 jniDebuggable false } debug { //不启用混淆器 minifyEnabled false //zip压缩 zipAlignEnabled true //移除无用的resource文件 shrinkResources false //混淆文件配置 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 保留debug信息 debuggable true ndk { cFlags "-std=c++11 -fexceptions -g -D __DEBUG__" // C++11 } //jni调试 jniDebuggable true //使用签名文件 signingConfig signingConfigs.config } } //编译配置 compileOptions { } //多渠道打包 productFlavors { //GooglePlay googlePlay { } } //所有打包配置(批量处理打包渠道--》manifestPlaceholders:设置打包渠道) productFlavors.all { //平台id flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] }}//依赖库(如果一个工程中有多个Module可以将共有的依赖写在project中的build.gradle中)dependencies { //多个Module共同需要的依赖库,可以写project中build.gradle中 compile rootProject.libRecyclerview compile rootProject.libCardview compile rootProject.libAppcompat compile rootProject.libDesign //本地jar包 compile fileTree(include: ['*.jar'], dir: 'libs') //module依赖,应用android-library项目 compile project(':lib') //依赖jar包 compile files('libs/lite-orm-1.7.0.jar') //配置远程库 compile 'com.jakewharton:butterknife:7.0.1' compile 'com.github.pwittchen:reactivenetwork:0.1.3' compile 'de.hdodenhof:circleimageview:2.0.0' compile 'com.github.hotchemi:android-rate:0.5.6' compile 'com.facebook.android:facebook-android-sdk:[4,5)' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.gordonwong:material-sheet-fab:1.2.1' compile 'com.google.code.gson:gson:2.6.2' compile 'com.google.android.gms:play-services-location:9.0.0' compile 'com.android.support:multidex:1.0.1' compile 'com.google.firebase:firebase-messaging:9.0.0' compile 'pl.tajchert:waitingdots:0.2.0' compile 'com.google.firebase:firebase-ads:9.0.0'}//应用插件apply plugin: 'com.google.gms.google-services'//支持maven中央仓库repositories { mavenCentral()}
以上是app中build.gradle中的配置
gradle.properties配置文件(signingConfigs配置文件信息)
KEYALIAS=...KEYPASSWORD=......STOREFILEPATH=.../Key.jksSTOREPASSWORD=.....
project项目工程中的build.gradle中的配置
//一个变量def supportVersion = "24.2.0"ext { compileSdkVersion = 24 buildToolsVersion = "24.0.0" minSdkVersion = 15 targetSdkVersion = 24 //Glide版本 glideVersion = "3.7.0" //lib libAppcompat = "com.android.support:appcompat-v7:${supportVersion}"//使用变量 libGlide = "com.github.bumptech.glide:glide:${glideVersion}" libRecyclerview = "com.android.support:recyclerview-v7:${supportVersion}" libCardview = "com.android.support:cardview-v7:${supportVersion}" libDesign = "com.android.support:design:${supportVersion}" //test lib 版本 testLibJunit = 'junit:junit:4.12'}buildscript { //Gradle支持JCenter上获取构件 repositories { jcenter() } //依赖gradle的编译版本 dependencies { classpath 'com.android.tools.build:gradle:2.2.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }}//所有项目支持jcenter()仓库allprojects { repositories { jcenter() }}//task clean 声明一个任务,任务名叫clean,任务类型是Delete(也可以是Copy),就是每当修改settings.gradle文件后点击同步,就会删除rootProject.buildDir下的文件task clean(type: Delete) { delete rootProject.buildDir}
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
- BuildGradle自定义打包
- 圆形ImageView
- Android的设备信息