引言 AndroidStudio普及以来,一个完整的Android应用Gradle的配置必不可少,针对Gradle的定制,结合自己日常使用以及网络上其他人分享的内容整理了这篇
我的配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 apply plugin: 'com.android.application' def releaseTime () { return new Date().format("yyyy-MM-dd" , TimeZone.getTimeZone("UTC" )) } android { compileSdkVersion ANDROID_BUILD_SDK_VERSION as int buildToolsVersion ANDROID_BUILD_TOOLS_VERSION defaultConfig { applicationId "net.robinx.hengrui" minSdkVersion ANDROID_BUILD_MIN_SDK_VERSION as int targetSdkVersion ANDROID_BUILD_TARGET_SDK_VERSION as int versionCode VERSION_CODE as int versionName VERSION_NAME vectorDrawables.useSupportLibrary = true } signingConfigs { myConfig { storeFile file ('xxxxx.jks' ) storePassword 'xxxxxx' keyAlias 'xxxxx' keyPassword 'xxxxx' } } buildTypes { release { signingConfig android.signingConfigs.myConfig minifyEnabled false proguardFiles getDefaultProguardFile ('proguard-android.txt' ) , 'proguard-rules.pro' applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk' )) { def fileName = "HengRui_v${defaultConfig.versionCode}_${releaseTime()}_${variant.productFlavors[0].name}.apk" output.outputFile = new File(outputFile.parent, fileName) } } } manifestPlaceholders = [baidu_map_key: "xxxxxxxxxxxxxxxxxxx" ] } debug { signingConfig android.signingConfigs.myConfig minifyEnabled false proguardFiles getDefaultProguardFile ('proguard-android.txt' ) , 'proguard-rules.pro' manifestPlaceholders = [baidu_map_key: "xxxxxxxxxxxxxxxxxxx" ] } } productFlavors { Default {} } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] } lintOptions { checkReleaseBuilds false abortOnError false } repositories { flatDir { dirs 'libs' , '../lib.aar/libs' } } } dependencies { compile fileTree (include: ['*.jar' ], dir: 'libs' ) compile project (':lib.aar' ) compile 'com.android.support:appcompat-v7:25.0.1' }
这是平时自己用的配置,主要自定义了打包的文件名称,debug包使用release签名,以及多渠道打包
其他配置 网络上的一篇配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 android { compileSdkVersion 25 buildToolsVersion "25.0.3" defaultConfig { applicationId "com.imliujun.gradle" minSdkVersion 16 targetSdkVersion 25 versionCode gitVersionCode () versionName rootProject.ext.versionName manifestPlaceholders = [UMENG_APP_KEY : "填你的友盟 APP KEY" , UMENG_CHANNEL_VALUE: "默认的渠道名" ] } signingConfigs { keyStore { storeFile file ("test.jks" ) storePassword "111111" keyAlias "test" keyPassword "111111" } } buildTypes { release { buildConfigField "boolean" , "LOG_DEBUG" , "false" signingConfig signingConfigs.keyStore } debug { buildConfigField "boolean" , "LOG_DEBUG" , "true" versionNameSuffix "-debug" signingConfig signingConfigs.keyStore manifestPlaceholders.UMENG_CHANNEL_VALUE = "test" } } productFlavors { offline { buildConfigField "String" , "DOMAIN_NAME" , "\"https://offline.domain.com/\"" versionName getTestVersionName () } online { buildConfigField "String" , "DOMAIN_NAME" , "\"https://online.domain.com/\"" } admin { buildConfigField "String" , "DOMAIN_NAME" , "\"https://admin.domain.com/\"" versionName rootProject.ext.versionName + "-管理员" manifestPlaceholders.UMENG_CHANNEL_VALUE = "admin" } } }
根目录gradle文件配置
1 2 3 4 5 6 7 8 9 10 11 12 ext { versionName = "2.0.2" testNum = "0001" } def getTestVersionName () { return String.format("%s.%s" , rootProject.ext.versionName, rootProject.ext.testNum) } static int gitVersionCode () { def count = "git rev-list HEAD --count" .execute().text.trim() return count.isInteger() ? count.toInteger() : 0 }
与自己平时使用的有些细微差别,可以参看这篇文章http://www.imliujun.com/gradle1.html
小结 结合两者相信可以定制出适合自己项目的gradle配置
转载请指明出处RobinBlog:http://robinx.net/2017/07/20/Gradle的定制/