舊有app在Android 12 閃退問題& app Splash screens

現在越來越多手機開始轉成android 12. 於是我就把target sdk = 31 試試看.

android { 
    compileSdkVersion 31

    defaultConfig {
        targetSdkVersion 31
    }
}

結果舊有的app都開始發生了閃退現象.

就有使用者跟我反映app當機問題出現.因為這屬於不可歸咎app設計的問題..必須要在android 12的機器上跑才會發生,而且android developer console中Pre-launch report 也沒測試出問題.

但一跑android 12模擬器就會發生當機,所以真的會有問題.

只能說寫app要處理的突發情況有很多,這是其一.

android app發佈控制台好的地方是當你上傳apk時,他都會幫你自動做Pre-launch 測試.
這次android 12會突然當機的問題,理應要先被Pre-launch 被發現才對.
我檢查後才發現,原來連Pre-launch都沒測試到最新的android 12.
他只測試到android 11. 但目前很多新機子已經開始系統更新到android 12去了.這就是問題會發生的原因.

----

error message:

java.lang.IllegalArgumentException: com.mark.electricitybillcalculator: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.

    Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.


Solution:

修正android 12上當機閃退問題(Fixed app crash issue on android 12 devices)

在 app\build.gradle 的 dependencies  增加:

//最新版本可參考此連結

dependencies {
   
def work_version = "2.7.0"

   
// (Java only)
    implementation
"androidx.work:work-runtime:$work_version"

   
// Kotlin + coroutines
    implementation
"androidx.work:work-runtime-ktx:$work_version"

}

 ----

android 12 new Splash screens feature: app起動畫面

在解上面閃退的問題時,發現android 12多新增了新功能Splash screens:

參考1  , 參考2

用這個後會比較好,有個統一的實作規範,由android控制,將來更新會方便很多.

且admob的open ads也要求要有起動畫面.
----

1.add dependencies: 

dependencies {
...
    implementation 'androidx.core:core-splashscreen:1.0.0-alpha02'
}

2. 在styles.xml新增:

<style name="Theme.App.Starting" parent="Theme.SplashScreen"> 

 <!--// Set the splash screen background, animated icon, and animation duration.--> 
  <item name="windowSplashScreenBackground">@color/colorAccent</item> 
 
<!--// Use windowSplashScreenAnimatedIcon to add either a drawable or an // animated drawable. One of these is required.--> 
 
<item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item> 
 <item name="windowSplashScreenAnimationDuration">200</item>
 
<!--// Set the theme of the Activity that directly follows your splash screen.--> 
<item name="postSplashScreenTheme">@style/AppTheme</item> 

 </style>


3. In the manifest, replace the theme:

<manifest>
<application android:theme="@style/Theme.App.Starting">
<!-- or -->
<activity android:theme="@style/Theme.App.Starting">
...

4.在 起動 Activity 的 setContentView()之前一行新增setContentView()..

public class SplashScreenSampleActivity extends Activity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
         
super.onCreate(savedInstanceState);

         
// Handle the splash screen transition.
         
SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
         setContentView
(R.layout.main_activity);
   
}
}


非常簡易就可設定好SplashScreen,不用寫一大堆程式碼.

留言

這個網誌中的熱門文章

最爛的銀行服務-玉山銀行

Mark App Design Apps - Terms and Privacy Policy (服務條款,隱私權政策)

SMR疊瓦式hdd致命缺陷被解決????!!!

ios app 上架時app icon要注意事項

更改google drive預設存放目錄位置Change Google Drive Default Folder Location in Windows

google play 正式發布前測試報告...非常好用.

app bundle and bundletool. 關於aab安裝問題

關於google play console app應用程式簽署

Google Play badge徽章產生器