2022年12月11日 星期日

[Android][App] 傳說小工具進版 targetSDKVersion 31 後遇到的問題

Photo by Malte Luk

這次進版問題特別多,就把這些過程都記錄下來,希望也幫助到遇到類似問題的人

這邊也帶出個問題,就是平常用的測試手機 HTC UU 是 Android 8 的版本實在太舊了,測不出問題 XD,話說現在還有人在用 Android 8 的手機嗎?

我的開發環境:Android Studio 3.6.1, Java

問題:
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.

有趣了,我的 source code 裡沒有用到 PendingIntenet,居然要我加 FLAG,後來才發現,原來是使用的 library 裡有用到...

處理方式:
發現 app 已經非常久沒使用,之前用的 library 都太舊了或無法使用,先改用 AndroidX 並將舊的 library 換掉

使用 AndroidX,參考 https://developer.android.com/jetpack/androidx?hl=zh-tw
gradle.properties
+android.useAndroidX=true
+android.enableJetifier=true

若有 library 因此找不到,可參考 https://developer.android.com/jetpack/androidx/migrate/class-mappings?hl=zh-tw 來替換

app\build.gradle
defaultConfig {
    -minSdkVersion 16
    +minSdkVersion 21
    +multiDexEnabled true
}
dependencies {
    +implementation 'androidx.work:work-runtime-ktx:2.7.0'
    +implementation 'androidx.work:work-runtime:2.7.1'
}

build.gradle
dependencies {
    -classpath 'com.android.tools.build:gradle:3.2.1'
    -classpath 'com.google.gms:google-services:3.2.0'
    +classpath 'com.android.tools.build:gradle:3.6.4'
    +classpath 'com.google.gms:google-services:4.3.14'
}

升級 gradle(利用 Android Studio 在 build 的時候的提示做的)

問題:
java.lang.NoSuchMethodError: No static method metafactory

處理方式:
build.gradle
android{
   compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_8
      targetCompatibility JavaVersion.VERSION_1_8
   }
}

問題:
Error inflating class com.google.android.gms.ads.AdView

處理方式:
build.gradle
dependencies {
    -api 'com.google.android.gms:play-services-ads:11.8.0'
    +api 'com.google.android.gms:play-services-ads:21.3.0'
}
加入以下在 AndroidManifest.xml 裡
<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>

問題:
error: incompatible types: String cannot be converted to OnInitializationCompleteListener        MobileAds.initialize(this, "xxxx");

處理方式:
使用下面方式替換原有寫法
AdView mAdView = (AdView) findViewById(R.id.adView);        mAdView.setAdUnitId(getString(R.string.banner_ad_unit_id));


Reference
Firebase For Flutter Execution failed for task ':app:transformClassesWithDexForDebug'
feedbackGet started with Google Analytics




沒有留言:

張貼留言