1.使用多渠道
productFlavors {
baidu {
resValue("string", "channel_name", "baidu")
// 这是专门为当前的渠道设置的包名
applicationId "xxx.xxx.xxx"
}
google {
resValue("string", "channel_name", "google")
}
}
使用多渠道后就会有baiduRelease baiduDebug googleRelease googleDebug等四种组合。
对于产生apk的命令响应的也有gradle assembleBaidu,gradle assembleBaiduRelease,gradle assembleBaiduDebug等。
美团的快速多渠道打包工具
https://github.com/Meituan-Dianping/walle
本质应该是在打了一个基础包之后,通过在apk包中META-INF文件中增加具有渠道标记的空文件。然后在java代码中获取到这个空文件名,来知道是哪一个渠道包。
下面是在java代码中获取apk中的内容的代码,能够遍历apk中的所有的文件,如果找到mChannel标记的文件,就截串获取:
public static String getChannel(Context context) {
ApplicationInfo applicationInfo = context.getApplicationInfo();
String sourceDir = applicationInfo.sourceDir;
String ret = "";
ZipFile zipFile = null;
try {
zipFile = new ZipFile(sourceDir);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
String entryName = zipEntry.getName();
Log.e("TAG", "entryName===="+entryName);
if (entryName.startsWith("mChannel")) {
ret = entryName;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
String[] split = ret.split("_");
if (split != null && split.length >= 2) {
return ret.substring(split[0].length() + 1);
} else {
return "";
}
}
2.测试类的使用
在这个方法中只能是纯java的代码,不能用android的特性,比如Log
依赖testImplementation ‘junit:junit:4.12’
@Test
public void foo1(){
System.out.print("eeeeeeee");
}
3.lint的使用
lintOptions{
abortOnError true
// 当出现警告的时候,也被当做错误对待
warningsAsErrors true
// 这是单独的检查
// check "NewApi"
// 可以使用set集合
// def checkSet=new HashSet<String>()
// checkSet.add("NewApi")
// checkSet.add("InlineApi")
// check=checkSet
// 或者使用
check "InlineApi","NewApi"
checkAllWarnings false
// 默认为true
checkReleaseBuilds true
}