Android 12 需要更新適配點并不多,本篇主要介紹最常見的兩個需要適配的點:android:exported 和 SplashScreen 。
一、android:exported
它主要是設置 Activity 是否可由其他應用的組件啟動, “true” 則表示可以,而“false”表示不可以。
若為“false”,則 Activity 只能由同一應用的組件或使用同一用戶 ID 的不同應用啟動。
當然不止是 Activity, Service 和 Receiver 也會有 exported 的場景。
一般情況下如果使用了 intent-filter,則不能將 exported 設置為“false”,不然在 Activity 被調用時系統(tǒng)會拋出 ActivityNotFoundException 異常。
相反如果沒有 intent-filter,那就不應該把 Activity 的 exported 設置為true ,這可能會在安全掃描時被定義為安全漏洞。
而在 Android 12 的平臺上,也就是使用 targetSdkVersion 31 時,那么你就需要注意:
如果 Activity 、 Service 或 Receiver 使用 intent-filter ,并且未顯式聲明 android:exported 的值,App 將會無法安裝。
這時候你可能會選擇去 AndroidManifest 一個一個手動修改,但是如果你使用的 SDK 或者第三方庫沒有支持怎么辦?或者你想要打出不同 target 平臺的包?這時候下面這段 gradle 腳本可以給你省心:
com.android.tools.build:gradle:3.4.3 以下版本
/**
* 修改 Android 12 因為 exported 的構建問題
*/
android.applicationVariants.all { variant ->
variant.outputs.all { output ->
output.processResources.doFirst { pm ->
String manifestPath = output.processResources.manifestFile
def manifestFile = new File(manifestPath)
def xml = new XmlParser(false, true).parse(manifestFile)
def exportedTag = "android:exported"
///指定 space
def androidSpace = new groovy.xml.Namespace('http://schemas.android.com/apk/res/android', 'android')
def nodes = xml.application[0].'*'.findAll {
//挑選要修改的節(jié)點,沒有指定的 exported 的才需要增加
(it.name() == 'activity' || it.name() == 'receiver' || it.name() == 'service') && it.attribute(androidSpace.exported) == null
}
///添加 exported,默認 false
nodes.each {
def isMain = false
it.each {
if (it.name() == "intent-filter") {
it.each {
if (it.name() == "action") {
if (it.attributes().get(androidSpace.name) == "android.intent.action.MAIN") {
isMain = true
println("......................MAIN FOUND......................")
}
}
}
}
}
it.attributes().put(exportedTag, "${isMain}")
}
PrintWriter pw = new PrintWriter(manifestFile)
pw.write(groovy.xml.XmlUtil.serialize(xml))
pw.close()
}
}
}
com.android.tools.build:gradle:4.1.0 以上版本
/**
* 修改 Android 12 因為 exported 的構建問題
*/
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def processManifest = output.getProcessManifestProvider().get()
processManifest.doLast { task ->
def outputDir = task.multiApkManifestOutputDirectory
File outputDirectory
if (outputDir instanceof File) {
outputDirectory = outputDir
} else {
outputDirectory = outputDir.get().asFile
}
File manifestOutFile = file("$outputDirectory/AndroidManifest.xml")
println("----------- ${manifestOutFile} ----------- ")
if (manifestOutFile.exists() && manifestOutFile.canRead() && manifestOutFile.canWrite()) {
def manifestFile = manifestOutFile
///這里第二個參數(shù)是 false ,所以 namespace 是展開的,所以下面不能用 androidSpace,而是用 nameTag
def xml = new XmlParser(false, false).parse(manifestFile)
def exportedTag = "android:exported"
def nameTag = "android:name"
///指定 space
//def androidSpace = new groovy.xml.Namespace('http://schemas.android.com/apk/res/android', 'android')
def nodes = xml.application[0].'*'.findAll {
//挑選要修改的節(jié)點,沒有指定的 exported 的才需要增加
//如果 exportedTag 拿不到可以嘗試 it.attribute(androidSpace.exported)
(it.name() == 'activity' || it.name() == 'receiver' || it.name() == 'service') && it.attribute(exportedTag) == null
}
///添加 exported,默認 false
nodes.each {
def isMain = false
it.each {
if (it.name() == "intent-filter") {
it.each {
if (it.name() == "action") {
//如果 nameTag 拿不到可以嘗試 it.attribute(androidSpace.name)
if (it.attributes().get(nameTag) == "android.intent.action.MAIN") {
isMain = true
println("......................MAIN FOUND......................")
}
}
}
}
}
it.attributes().put(exportedTag, "${isMain}")
}
PrintWriter pw = new PrintWriter(manifestFile)
pw.write(groovy.xml.XmlUtil.serialize(xml))
pw.close()
}
}
}
}
這段腳本你可以直接放到 app/build.gradle 下執(zhí)行,也可以單獨放到一個 gradle 文件之后 apply 引入,它的作用就是:
在打包過程中檢索所有沒有設置 exported 的組件,給他們動態(tài)配置上 exported。這里有個特殊需要注意的是,因為啟動 Activity 默認就是需要被 Launcher 打開的,所以 "android.intent.action.MAIN" 需要 exported 設置為 true 。(PS:應該是用 LAUNCHER 類別,這里故意用 MAIN)
如果有需要,還可以自己增加判斷設置了 "intent-filter" 的才配置 exported 。
二、SplashScreen
Android 12 新增加了 SplashScreen 的 API,它包括啟動時的進入應用的動作、顯示應用的圖標畫面,以及展示應用本身的過渡效果。
它大概由如下 4 個部分組成,這里需要注意:
1 最好是矢量的可繪制對象,當然它可以是靜態(tài)或動畫形式。
2 是可選的,也就是圖標的背景。
與自適應圖標一樣,前景的三分之一被遮蓋 (3)。
4 就是窗口背景。
啟動畫面動畫機制由進入動畫和退出動畫組成。
進入動畫由系統(tǒng)視圖到啟動畫面組成,這由系統(tǒng)控制且不可自定義。
退出動畫由隱藏啟動畫面的動畫運行組成。如果要對其進行自定義,可以通過 SplashScreenView 自定義。

更詳細的介紹這里就不展開了,有興趣的可以自己看官方的資料:https://developer.android.com/guide/topics/ui/splash-screen,這里主要介紹下如何適配和使用的問題。
首先不管你的 TargetSDK 什么版本,當你運行到 Android 12 的手機上時,所有的 App 都會增加 SplashScreen 的功能。
如果你什么都不做,那 App 的 Launcher 圖標會變成 SplashScreen 界面的那個圖標,而對應的原主題下 windowBackground 屬性指定的顏色,就會成為 SplashScreen 界面的背景顏色。這個啟動效果在所有應用的冷啟動和熱啟動期間會出現(xiàn)。
其實不適配好像也沒啥問題。
關于如何遷移和使用 SplashScreen 可以查閱官方詳細文檔:https://developer.android.com/guide/topics/ui/splash-screen/migrate
另外還可以參考 《Jetpack新成員SplashScreen:打造全新的App啟動畫面》[8] 這篇文章,文章詳細介紹了如果使用官方的 Jetpack 庫來讓這個效果適配到更低的 Target 平臺。
而正常情況下我們可以做的就是:
- 1、升級
compileSdkVersion 31、targetSdkVersion 31&buildToolsVersion '31.0.0' - 2、 添加依賴
implementation "androidx.core:core-splashscreen:1.0.0-alpha02" - 3、增加
values-v31的目錄 - 4、添加
styles.xml對應的主題,例如:
<resources>
<style name="LaunchTheme" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/splashScreenBackground</item>
<!--<item name="windowSplashScreenAnimatedIcon">@drawable/splash</item>-->
<item name="windowSplashScreenAnimationDuration">500</item>
<item name="postSplashScreenTheme">@style/AppTheme</item>
</style>
</resources>
5、給你的啟動 Activity 添加這個主題,不同目錄下使用不同主題來達到適配效果。