眾所周知,相同包名的APP,是不能同時安裝的,但是我們實際開發(fā)中,測試同學(xué)往往在測試環(huán)境沒問題,上了生產(chǎn)環(huán)境,卻發(fā)現(xiàn)了bug,這時候就只能卸載生產(chǎn)環(huán)境的包,再去安裝測試環(huán)境。如果開發(fā)流程中缺少自動化打包或者測試同學(xué)不保存蒲公英二維碼,這時候就會產(chǎn)生多余時間成本。那么有沒有一種可能,同時安裝測試與生產(chǎn)環(huán)境的包呢?
第一步:在項目的build.gradle增加
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable true
zipAlignEnabled true //是否支持zip
resValue("string", "ENVIRONMENT", "RELEASE")
buildConfigField "boolean", "LOG_DEBUG", "false"
resValue "string", "name", "release"http://必須保證資源文件存在,否則會報錯
resValue "int", "logo", "release"http://必須保證資源文件存在,否則會報錯
signingConfig signingConfigs.release
}
debug {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
minifyEnabled false
zipAlignEnabled true
debuggable true
applicationIdSuffix ".debug"
resValue("string", "ENVIRONMENT", "DEBUG")
resValue "string", "name", "debug"http://必須保證資源文件存在,否則會報錯
}
}
以上操作適用于大部分同學(xué),但如果你的APP中含有 ContentProvider或者FileProvider(Android7.0文件適配必備),也就是和包名相關(guān)的;或者你想更直接點(diǎn),直接區(qū)分測試與生產(chǎn)的app名及圖標(biāo),那么你可能需要如下操作了:
第二步:
<provider
android:name="com.huawei.hms.update.provider.UpdateProvider"
android:authorities="{applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
適配也很簡單,將 authorities=”" 里的包名改為 ${applicationId} 即可
都到這一步了,那不如更友好點(diǎn),讓測試同學(xué)更好辨認(rèn):
修改app.build文件
buildTypes {
release {
...
manifestPlaceholders = [icon: "@mipmap/logo",
//這里也可以加入FileProvider的不同包名
//例如: fileProvider: "com.petterp.release.provider"
//根據(jù)自己的需求來定
name: "I貝康"]
...
}
debug {
...
manifestPlaceholders = [icon: "@mipmap/logo_debug",
//這里也可以加入FileProvider的不同包名
//例如: fileProvider: "com.petterp.debug.provider"
//根據(jù)自己的需求來定
name: "I貝康測試"]
...
}
}
第三步:
<application
android:name=".application.SampleApplication"
android:allowBackup="true"
android:appComponentFactory="任意值"
android:hardwareAccelerated="true"
android:icon="{name}"http://重點(diǎn)
android:largeHeap="true"
android:supportsRtl="true"
android:testOnly="false"
android:theme="@style/AppTheme"
tools:replace="android:label,android:icon">//重點(diǎn)

注意:1.這樣包名會增加.debug所以使用包名的地方請注意
2.定義變量
buildTypes {
release {
buildConfigField "boolean", "debug", "true"
}
debug {
buildConfigField "boolean", "debug", "false"
}
}
獲取BuildConfig.debug