buidl.gradle配置如下:
def releaseTime() {
return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName
def packageLastName = applicationId.substring(applicationId.lastIndexOf(".") + 1, applicationId.length())
println(">>>>>>>>>>>>>packageLastName:" + packageLastName)
if (variant.buildType.name == "release") {
fileName = packageLastName.toUpperCase() + "_${variant.productFlavors[0].name.toUpperCase()}_V${defaultConfig.versionName}_RELEASE_" + releaseTime() + ".apk"
} else {
fileName = packageLastName + "_${variant.productFlavors[0].name}" + "_v${defaultConfig.versionName}_${variant.buildType.name}_" +releaseTime() + ".apk"
}
if (variant.buildType.name == "release")
variant.getPackageApplication().outputDirectory = new File(project.rootDir.absolutePath + "/apk/" + variant.buildType.name)
outputFileName = fileName
}
}
}
//定义不同产品的不同维度,没有该需求,定义为空
flavorDimensions("")
//渠道Flavors,配置不同风格的app
productFlavors {
only_test {
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.releaseChannel]
}
meizu {
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.meizuChannel]
}
yyb{
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.yybChannel]
}
samsung {
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.meizuChannel]
}
jinli{
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.jinliChannel]
}
baidu{
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.baiduChannel]
}
ali{
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.aliChannel]
}
anzhi {
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.anzhiChannel]
}
official {
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.officialChannel]
}
hauwei {
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.hauweiChannel]
}
oppo {
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.oppoChannel]
}
vivo {
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.vivoChannel]
}
xiaomi {
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext.xiaomiChannel]
}
_360{
manifestPlaceholders = [ CHANNEL_ID: rootProject.ext._360Channel]
}
}
manifestPlaceholders字段记得在manifest.xml文件中配置
配置完后,我们就可以去获取CHANNEL_ID这个字段的值了,下面是获取的工具类:
public class AppChannelUtil {
public static String getChannelId() {
return getMetaDataStr("CHANNEL_ID");
}
public static String getMetaDataStr(String key) {
String resultData = "";
if (!TextUtils.isEmpty(key)) {
Bundle appInfoBundle = getAppInfoBundle();
if (appInfoBundle != null)
resultData = appInfoBundle.getString(key);
}
LogUtil.i("AppChannelUtil","渠道号--------"+resultData);
return resultData;
}
public static int getMetaDataInt(String key) {
int resultData = 0;
if (!TextUtils.isEmpty(key)) {
Bundle appInfoBundle = getAppInfoBundle();
if (appInfoBundle != null)
resultData = appInfoBundle.getInt(key);
}
return resultData;
}
private static Bundle getAppInfoBundle() {
//注意:这里ApplicationInfo不能直接 CommonApplication.mApplication.getApplicationInfo()这样获取,否则会获取不到meta_data的
ApplicationInfo applicationInfo = getAppInfo();
if (applicationInfo != null) {
return applicationInfo.metaData;
}
return null;
}
private static ApplicationInfo getAppInfo() {
PackageManager packageManager = CommonApplication.mApplication.getPackageManager();
ApplicationInfo applicationInfo = null;
if (packageManager != null) {
try {
applicationInfo = packageManager.getApplicationInfo(CommonApplication.mApplication.getPackageName(), PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
return applicationInfo;
}
}
获取其他数据类型的值也是如此!