对应安卓应用对比测试,有时候需要在同一个手机上安装release 和 debug 两个版本或者,有其他特殊定制需求版本,
此时两个apk的代码基本相同,但是由于包名相同,无法同时安装两个,此时可以通过如下配置:
buildTypes {
debug {
applicationIdSuffix "debug"
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
即通过applicationIdSuffix 属性配置后,编译时会自动修改manifest.xml中的包名:
需要注意的是,如果你的代码中有使用Provider, 那么一定要记住修改其authorities,否则安装时会还是会报错误:
因为存在相同的provider name, 所以方便的做法时将authorities 也修改为带有 applicationId 部分,然后只用修改这个Id 就可以实现同一个apk 安装多个不同版本了。
在Manifest中使用${applicationId}
如Provider在声明android:authorities(该值必须唯一)时,如前缀为写死的包名字符串,当出现需要同一工程分包、分渠道打包时,安装在同一android设备将导致 INSTALL FAILED CONFLICTING PROVIDER 的报错(使用adb安装会有提示),这时可以使用${applicationId},这将避免android:authorities值非唯一的问题。
<provider
android:name="xxxx.xxxx.xxx.xxxProvider"
android:authorities="${applicationId}.xxxx"
android:grantUriPermissions="true"
android:exported="false"/>12345
${applicationId}也可以用在Manifest中其他需要唯一的取值情况,这种使用方式很灵活。
ContentProvider中authority要与声明的一致。
authorities匹配是根据UriMather.addUri(authority,path,code)。需要动态获取包名,不能写固定值(com.xx.xx.provider)。
// TODO: Android7.0 upgrade
String authorities = context.getPackageName() + FILE_PROVIDER_AUTHORTIES;
Uri contentUri = FileProvider.getUriForFile(context, authorities, newFile);