首页 > 系统 > Android > 正文

一个完整Android项目所需要用到的gradle配置技巧

2019-11-09 15:35:59
字体:
来源:转载
供稿:网友

Gradle配置技巧

本人总结了四条gradle配置技巧,这些技巧配置在Android项目中可以更加的方便工程的调试和打包等等 1.使用gradle的自定义PRoperty实现Android项目的配置和依赖统一管理 2.使用gradle的productFlavors实现Android项目多渠道打包 3.使用gradle实现Android项目debug版与release版共存 4.使用gradle实现批量修改生成的apk文件名

完整的Android项目gradle配置:

完整gradle配置所用到的技巧可以参考上面的四条gradle技巧总结

项目根目录的build.gradle配置

// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.3' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' }}plugins { id "com.jfrog.bintray" version "1.6"}allprojects { repositories { jcenter() maven { url "https://jitpack.io" } }}task clean(type: Delete) { delete rootProject.buildDir}ext { //Android configs configs = [ applicationId : "com.example.myapplication", compileSdkVersion: 25, buildToolsVersion: '25.0.2', minSdkVersion : 17, targetSdkVersion : 25, versionCode : 17, versionName : '3.1.5' ] // App dependencies supportLibraryVersion = '25.1.1' junitVersion = '4.12' multidexVersion = '1.0.1' gsonVersion = '2.4' butterknifeVersion = '8.4.0' ......}

app目录下的build.gradle配置

apply plugin: 'com.android.application'apply plugin: 'android-apt'def buildTime() { def date = new Date() def formattedDate = date.format('yyyy-MM-dd', TimeZone.getTimeZone("UTC")) return formattedDate}android { signingConfigs { release { storeFile file('../yourapp.keystore') keyAlias 'your passWord' keyPassword 'your alias' storePassword 'your password' } } compileSdkVersion configs.compileSdkVersion buildToolsVersion configs.buildToolsVersion defaultConfig { applicationId configs.applicationId minSdkVersion configs.minSdkVersion targetSdkVersion configs.targetSdkVersion versionCode configs.versionCode versionName configs.versionName // Enabling multidex support. multiDexEnabled true } buildTypes { release { //开启混淆 minifyEnabled true //打包时自动读取签名配置文件 signingConfig signingConfigs.release //开启zip对齐 zipAlignEnabled true //移除无用的resource文件 shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' //设置release版本的app_name和application_id manifestPlaceholders = [ APP_NAME : "@string/app_name", APPLICATION_ID: "@string/application_id" ] //设置release版本只包含armeabi和armeabi-v7a的so包 ndk { abiFilters "armeabi", "armeabi-v7a" } //批量修改生成的apk文件名 applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { // 输出apk名称为AppName_v1.0_2017-02-09_wandoujia.apk def apkFile = "AppName_v${defaultConfig.versionName}_${buildTime()}" + "_${variant.productFlavors[0].name}.apk" output.outputFile = new File(outputFile.parent, apkFile) } } } } debug { //设置debug版本的报名为<应用id>.debug applicationIdSuffix ".debug" //设置debug版本的app_name和application_id manifestPlaceholders = [ APP_NAME : "@string/app_name_debug", APPLICATION_ID: "@string/application_id_debug" ] //设置debug版本包含x86的so文件 ndk { abiFilters "armeabi", "armeabi-v7a", "x86" } } } packagingOptions { exclude 'META-INF/LICENSE' exclude 'META-INF/NOTICE' } dexOptions { javaMaxHeapSize "2g" } //友盟多渠道打包 productFlavors { wandoujia {} baidu {} _360 {} _91 {} anzhuomarket {} huawei {} xiaomi {} lenovo {} tencent {} coolapk {} meizu {} vivo {} ali {} yulin {} } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] }}dependencies { testCompile "junit:junit:$rootProject.junitVersion" compile "com.android.support:multidex:$rootProject.multidexVersion" compile "com.google.code.gson:gson:$rootProject.gsonVersion" //MaterialDesign compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion" compile "com.android.support:design:$rootProject.supportLibraryVersion" //CardView compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion" //RecyclerView compile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion" //butterKnife compile "com.jakewharton:butterknife:$rootProject.butterknifeVersion" apt "com.jakewharton:butterknife-compiler:$rootProject.butterknifeVersion" ......}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表