前言
新手安卓,也是刚开始接触AS,被gradle,虐的不行,满嘴 F***,真心没有感觉gradle丝毫方便,后来发现gradle的方便之处。被大佬要求去做多渠道。不得不硬着头皮去上了。搜了好多。Google了,看了好多,都是从好多截取下来才找到有用信息,太费时间了。所以自己打算写写,以便有其他新手遇到同样问题不再费事。第一篇博文请轻喷。。~~哈哈
正文
apply plugin: 'com.android.application'
apply plugin: 'newlens'
android {
compileSdkVersion 19
buildToolsVersion '23.0.0'
sourceSets { main{
jniLibs.srcDirs = ['libs'] }
}
defaultConfig {
// 一些默认属性
applicationId "com.0000.000" //包名
minSdkVersion 10
targetSdkVersion 19
versionCode 4
versionName "1.0"
//strings 中的 @string/app_name 不要 负责这里
// resValue app_name会无效,而是优先采用 strings中的<pre name="code" class="plain"> resValue "string", "app_name", "0000" //清单配置文件中 app_name
buildConfigField "boolean", "AUTO_UPDATES", "true"
buildConfigField "String" ,"APPURL", "\"http://455557/jf/api/\""
}
// 优先级 buildTypes > productFlavors > defaultConfig
buildTypes {
// 这里的优先级最高 注意同productFlavors重复设置 问题 我这里 建议只设置IP属性
// 你们可以根据业务进行调整
release {
//是否混淆
minifyEnabled true
//去除无用的 资源
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//正式版 国外
buildConfigField "String" ,"APPURL", "\"http://45550.107/jf/api/\""
}
debug{
//测试版 国内
// buildConfigField "String" ,"APPURL", "\"http://115555172/jf/api/\""
// leaf
// buildConfigField "String" ,"APPURL", "\"http://1555255/jf/api/\""
// 国外
buildConfigField "String" ,"APPURL", "\"http://55557/jf/api/\""
// minifyEnabled true
// proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
一定要
注意优先级 问题,避免设的属性无效
// signingConfigs {
// release {
// // relase签名
// keyAlias '123'
// keyPassword '123456'
// storeFile file('is.jks')
// storePassword '123456'
// }
// }
我这里并没有用
signingConfigs
//another app name
// android.applicationVariants.all { variant ->
// // get app_name field from defaultConfig
// def appName = variant.mergedFlavor.resValues.get('app_name').getValue()
// // concat new App name with each flavor's name
// appName = "${appName}"
${****}
// variant.productFlavors.each { flavor ->
// appName += " ${flavor.name}"
// }
// // optionally add buildType name
// appName += " ${variant.buildType.name}"
// // your requirement: if buildType == debug, add DEV to App name
// if(variant.buildType.name == "debug") {
// appName += " DEV"
// }
// // set new resVale
// variant.resValue 'string', 'app_name', appName
// }
productFlavors {
//这里的参数 如果和 buildTypes冲突,默认优先使用buildTypes
test {
resValue "string", "app_name", "DEBUG"
//包名修改 仅供安装两个app,jpush等会有包名错误,不建议设置
// applicationId "com.000.000.test"//包名修改
//更新 设置 有些 渠道不能自更新
buildConfigField "boolean", "AUTO_UPDATES", "true"
}
testnoup {
resValue "string", "app_name", "DEBUG"
//包名修改 仅供安装两个app,jpush等会有包名错误,applicationid不建议设置
// applicationId "com.000.000.test"//包名修改
buildConfigField "boolean", "AUTO_UPDATES", "false"
}
googleplay {
buildConfigField "boolean", "AUTO_UPDATES", "false"
}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [TD_CHANNEL_ID_VALUE: name]
}
}
productFlavors
这个是多渠道打包使用的一些参数设置
<pre name="code" class="plain"> [TD_CHANNEL_ID_VALUE: name]
来看一下 mainfests 中 我怎么写的
变量方法三
我们再来看一下 代码中的
buildConfigField "boolean", "AUTO_UPDATES", "false"
buildConfigField "boolean", "AUTO_UPDATES", "true"
这是 gradle中定义变量的另外一种方法
因为有些渠道不能自更新。所以做使用。
看我java代码中操作、、
就这样用就行了 我的 checkOutVersion 是一个更新的方法。我就不再说了。
在其他java代码中 如果引用 ,就先在 build.gradle中写好 然后
BuildConfig. (调用你命名的变量)
原来有个不错链接还有其中原理都很清楚,重要的是 !!我现在找不太到了,,先凑活看一下 这个,
http://stormzhang.com/android/2015/01/25/gradle-build-field/
三个变量方法
链接给你,看着我的结合这个,就清楚了。
细心看我写的。这个链接没把一些坑写出了。坑了我。
http://drakeet.me/gradle-change-android-resvalue-buildconfig-and-manifest
好了 就这样了。完整的 build.gradle 给你们粘贴一下
apply plugin: 'com.android.application'
apply plugin: 'newlens'
android {
compileSdkVersion 19
buildToolsVersion '23.0.0'
sourceSets { main{
jniLibs.srcDirs = ['libs'] }
}
defaultConfig {
// 一些默认属性
applicationId "com.0000.000" //包名 我改了一下哈哈~~
minSdkVersion 10
targetSdkVersion 19
versionCode 4
versionName "1.0"
//strings 中的 @string/app_name 不要 负责这里
// resValue app_name会无效,而是优先采用 strings中的
resValue "string", "app_name", "0000" //清单配置文件中 app_name
buildConfigField "boolean", "AUTO_UPDATES", "true"
buildConfigField "String" ,"APPURL", "\"http://455557/jf/api/\""
}
// 优先级 buildTypes > productFlavors > defaultConfig
buildTypes {
// 这里的优先级最高 注意同productFlavors重复设置 问题 建议只设置IP属性
release {
//是否混淆
minifyEnabled true
//去除无用的 资源
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//正式版 国外
buildConfigField "String" ,"APPURL", "\"http://45550.107/jf/api/\""
}
debug{
//测试版 国内
// buildConfigField "String" ,"APPURL", "\"http://115555172/jf/api/\""
// leaf
// buildConfigField "String" ,"APPURL", "\"http://1555255/jf/api/\""
// 国外
buildConfigField "String" ,"APPURL", "\"http://55557/jf/api/\""
// minifyEnabled true
// proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// signingConfigs {
// release {
// // relase签名
// keyAlias '123'
// keyPassword '123456'
// storeFile file('is.jks')
// storePassword '123456'
// }
// }
//another app name
// android.applicationVariants.all { variant ->
// // get app_name field from defaultConfig
// def appName = variant.mergedFlavor.resValues.get('app_name').getValue()
// // concat new App name with each flavor's name
// appName = "${appName}"
// variant.productFlavors.each { flavor ->
// appName += " ${flavor.name}"
// }
// // optionally add buildType name
// appName += " ${variant.buildType.name}"
// // your requirement: if buildType == debug, add DEV to App name
// if(variant.buildType.name == "debug") {
// appName += " DEV"
// }
// // set new resVale
// variant.resValue 'string', 'app_name', appName
// }
productFlavors {
//这里的参数 如果和 buildTypes冲突,默认优先使用buildTypes
inftest {
resValue "string", "app_name", "DEBUG"
//包名修改 仅供安装两个app,如果设置jpush等会有包名错误,不建议设置
// applicationId "com.000.000.test"//包名修改
//更新 设置 有些 渠道不能自更新
buildConfigField "boolean", "AUTO_UPDATES", "true"
}
inftestnoup {
resValue "string", "app_name", "DEBUG"
//包名修改 仅供安装两个app,jpush等会有包名错误,applicationid不建议设置
// applicationId "com.000.000.test"//包名修改
buildConfigField "boolean", "AUTO_UPDATES", "false"
}
shenzheninf {
}
olala {
}
googleplay {
buildConfigField "boolean", "AUTO_UPDATES", "false"
}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [TD_CHANNEL_ID_VALUE: name]
}
}
// android.applicationVariants.all { variant -> variant.outputs.each { output -> output.outputFile = new File(output.outputFile.parent, defaultConfig.applicationId + "-" + buildType.name + "-v" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk" ); } }
//}
repositories {
mavenCentral()
}
//apply plugin: 'com.bugtags.library.plugin'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//数据统计
compile files('libs/TalkingdataAnalytics.jar')
testCompile 'junit:junit:4.12'
compile 'com.loopj.android:android-async-http:1.4.6'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'de.hdodenhof:circleimageview:2.0.0'
// <!--listview动画 -->
compile 'com.nhaarman.listviewanimations:library:2.6.0'
//听云。性能检测
compile 'com.networkbench.newlens.agent.android:nbs.newlens.agent:2.3.1'
//bugtags
// compile 'com.bugtags.library:bugtags-lib:1.1.0'
//内存泄露分析
// debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'
// releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'
}
附加
res-auto"
之前的
设置不同的
applicationId 之后要这样写
ps:开始我也是设置不同包名想着debug一个,正式一个。手机上装两个app,后来发现用处不大,就没再用了。如果有人
遇到多包名的 自定义view问题可以参考一下。
一本书 Packt.Gradle.for.Android
这本书 入门 可以