网上搜了各种通过gradle批量打包apk时更改相关icon和label的资料,卧槽,一眼看上去就觉得好烦,根本就不适合我这种懒人初学者,后来无意间知道了清单占位符,这下就简单了!二话不说先上代码:
- AndroidManifest.xml部分
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.nicole.mytest">
<application
android:name="MyApplication"
android:allowBackup="true"
android:icon="${icon}"
android:label="${label}"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
请注意里面的两行:
android:icon="${icon}"
android:label="${label}"
这是占位符,就可以通过gradle来定向定义
- build.gradle部分
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.test.nicole.mytest"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors{
aaa{
manifestPlaceholders = [label:"哈哈哈", icon:"@mipmap/ic_launcher"]
}
bbb{
manifestPlaceholders = [label:"呵呵呵", icon:"@mipmap/v3"]
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
}
注意这块:
productFlavors{
aaa{
manifestPlaceholders = [label:"哈哈哈", icon:"@mipmap/ic_launcher"]
}
bbb{
manifestPlaceholders = [label:"呵呵呵", icon:"@mipmap/v3"]
}
}
这是形成这两个apk的关键,通过manifestPlaceholders这个清单占位符来定义所对应的apk的app名称和图标。