defaultConfig {
applicationId "com.example.test"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
multiDexEnabled true
flavorDimensions "default"
}
signingConfigs {
release {
storeFile file("../***.jks")
storePassword "asdfgh"
keyAlias "fileInput"
keyPassword "asdfgh"
v2SigningEnabled false
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
applicationVariants.all {
variant ->
variant.outputs.all {
outputFileName = productFlavors[0].manifestPlaceholders.get("apk_name")+".apk"
}
}
lintOptions {
abortOnError false
}
//配置多版本的apk
productFlavors{
Offline{
applicationId "com.xxxx.xxxx.beta"
manifestPlaceholders = [app_name:"@string/app_name" ,icon: "@drawable/ic_launcher",banner:"@mipmap/launcher_beta",apk_name:"fileBeta"]
resValue("integer" , "logo_select","@drawable/logo") //没有调用成功
resValue("integer" , "countType","3")
resValue("string" , "othername","beta")
resValue("bool" , "debugSwitch","true")
versionCode 2
versionName "1.0.2"
signingConfig signingConfigs.release
buildConfigField("int","logo","R.drawable.logo")
buildConfigField("boolean","ENABLE_DEBUG","true")
}
Online{
applicationId "com.xxxx.xxxx.online"
manifestPlaceholders = [app_name:"fileOnline" ,icon: "@drawable/ic_launcher_canada",banner:"@mipmap/launcher_online",apk_name:"fileOnline"]
resValue("integer" , "logo_select","@drawable/logo_line") //没有调用成功
resValue("integer" , "countType","2")
resValue("string" , "othername","online")
resValue("bool" , "debugSwitch","false")
versionCode 1
versionName "1.0.3"
signingConfig signingConfigs.Onlinerelease//单独配置签名
buildConfigField("int","logo","R.drawable.ic_logo")
buildConfigField("boolean","ENABLE_DEBUG","false")
}
注意:
1. 这里分了beta和Online两个版本,applicationId分别配置的是两个版本的包名,如果没有配置的话,则默认使用defaultConfig下的包名。
2.manifestPlaceholders里配置应用名称或图标,需要在AndroidManifest.xml的application标签下配置android:label="${app_name}" android:icon="${icon}"
3.resValue可以配置一些我们需要的不同的值,第一个参数是变量的类型,第二个参数key,第三个参数是变量的value。具体的使用value=context.getResources().getString(R.string.key)
4. manifestPlaceholders里配置应用名称可以用@string/app_name, 实现多语言翻译,但applicationVariants.all中配置apk打包后的名字时就不能用这个app_name了,得另外定义一个新的key值“apk_name”
5.BuildConfig的使用,在java文件中:
ivLogo.setImageResource(BuildConfig.logo);
可以根据不同的apk显示不同的图标。
sync一下,在侧边栏的gradle中就会出现assembleOnline和assembleOffline两个task,运行这两个task就能生成相应的包。
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.***.***">
<application
tools:replace="android:allowBackup,android:label"
android:allowBackup="true"
android:banner="${banner}"
android:icon="${icon}"
android:label="${app_name}"
android:supportsRtl="true"
android:theme="@style/AppTheme">
</application>
</manifest>
遇到的错误:
resValue属性的使用:
1. 当写成resValue("integer" , "logo_select","R.drawable.logo") ,编译报错:error:invalid integer;
当写成与BuildConfig一样,resValue("int" , "logo_select","R.drawable.logo"),编译也报:error:invalid integer;
正确的写法:resValue("integer" , "logo_select","@drawable/logo"), 但是使用时并没有调用成功。
2. 目前调用成功的resValue类型有:
resValue("integer" , "countType","3")
resValue("string" , "othername","beta")
resValue("bool" , "debugSwitch","true")
调用:getResources().getInteger(R.integer.countType).
getResources().getString(R.string.othername);
getResources().getBoolean(R.boolean.debugSwitch);
3. defaultConfig中如果没有配置flavorDimensions "default"的话,会报“ERROR: All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html”