1.AndroidManifest.xml
<application/>标签里,activity标签同级添加
<meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" />
Ps:UMENG_CHANNEL_VALUE 为自定义名字,gradle文件中
例:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" 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> <meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" /> </application>
2.build.gradle
android目录下defaultConfig同级添加:
// 渠道 Flavors,配置不同的渠道 productFlavors { GooglePlay {} xiaomi {} umeng {} qihu360 {} wandoujia {} yingyongbao {} //其他... } //批量处理 productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] } //打包重命名 applicationVariants.all { variant -> if (variant.buildType.name == "release") { variant.outputs.all { output -> def fileName = output.outputFile.name if (fileName.endsWith(".apk")) { def apkName = "${variant.productFlavors[0].name}-v${variant.versionName}-${releaseTime()}.apk"; outputFileName = apkName } } } }
android同级添加
def releaseTime() { return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC")) }
完毕;
这时我的报错了:
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
解决方法:https://blog.csdn.net/syif88/article/details/75009663
module的gradle文件中:
defaultConfig {
applicationId "*****"
minSdkVersion **
targetSdkVersion **
versionCode **
versionName "**"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//版本名后面添加一句话,意思就是flavor dimension 它的维度就是该版本号,这样维度就是都是统一的了
flavorDimensions "versionCode"
}