Programming Tips - Android upgrade to API 31 issues.

Date: 2023may1 Q. Android upgrade to API 31 issues. A. Problem: "Could not find com.android.support:appcompat-v7:31.0.0" "Could not find com.android.support:support-v4:31.0.0" "Could not find com.android.support:support-v4:31" "Could not find com.android.support:design:31.0.0" "Could not find com.android.support:support-vector-drawable:31" Solution: Just remove: implementation 'com.android.support:appcompat-v7:31.0.0' implementation 'com.android.support:support-v4:31.0.0' implementation 'com.android.support:support-v4:31' implementation 'com.android.support:design:31.0.0' implementation 'com.android.support:support-vector-drawable:31' Problem: android:exported needs to be explicitly specified for element <activity#com.example.myapp.MainActivity>. Solution: Added to AndroidManifest.xml <activity android:exported="true" Problem: Manifest merger failed : android:exported needs to be explicitly specified for element <receiver#com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. Solution: Added to manifest: <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> ... <application> <receiver android:name="com.google.android.gms.measurement.AppMeasurementInstallReferrerReceiver" android:enabled="true" android:exported="false" tools:node="merge" tools:overrideLibrary="com.google.android.gms.measurement"> <intent-filter> <action android:name="com.google.android.gms.measurement.UPLOAD"/> </intent-filter> </receiver> <application> Problem: Android resource linking failed /layout/content_main.xml:16: error: attribute layout_behavior (aka com.example.myapp:layout_behavior) not found. error: failed linking file resources. Solution: Change app:layout_behavour to app:layout_behavior And ensure you have at the top: xmlns:app="http://schemas.android.com/apk/res-auto" Problem: package android.support.design.widget does not exist import android.support.design.widget.FloatingActionButton; Solution: Change implementation 'com.android.support:support-v4:28.0.0' to implementation 'androidx.legacy:legacy-support-v4:1.0.0' in build.gradle Or if you're using androidx: implementation 'com.google.android.material:material:1.2.1' Problem: Configuration `:app:debugRuntimeClasspath` contains AndroidX dependencies, but the `android.useAndroidX` property is not enabled, which may cause runtime issues. Set `android.useAndroidX=true` in the `gradle.properties` file and retry. Solution: Add android.useAndroidX=true to gradle.properties Problem: Your project has set `android.useAndroidX=true`, but configuration `:app:debugRuntimeClasspath` still contains legacy support libraries, which may cause runtime issues. This behavior will not be allowed in Android Gradle plugin 8.0. Please use only AndroidX dependencies or set `android.enableJetifier=true` in the `gradle.properties` file to migrate your project to AndroidX (see https://developer.android.com/jetpack/androidx/migrate for more info). The following legacy support libraries are detected: Solution: Add android.enableJetifier=true to gradle.properties Problem: ERROR:F:\AndroidStudioProjects\<myproject>\app\src\main\res\layout\content_main.xml:9: AAPT: error: resource string/appbar_scrolling_view_behavior (aka com.hogdex.HashPostFree:string/appbar_scrolling_view_behavior) not found. Solution: Add to build.gradle implementation 'com.google.android.material:material:1.5.0' Problem: Need to convert to androidx Solution: Select Menu Refactor > Convert to androidx And make sure to click on [Do Refactor] bottom left. In build.gradle add: dependencies { ... def appcompat_version = "1.3.1" implementation "androidx.appcompat:appcompat:$appcompat_version" // For loading and tinting drawables on older versions of the platform implementation "androidx.appcompat:appcompat-resources:$appcompat_version" } According to https://developer.android.com/jetpack/androidx/migrate Change imports https://developer.android.com/jetpack/androidx/migrate/class-mappings android.support.v7.app.AppCompatActivity androidx.appcompat.app.AppCompatActivity android.support.v7.app.ActionBar androidx.appcompat.app.ActionBar android.support.v7.app.AppCompatDelegate androidx.appcompat.app.AppCompatDelegate android.support.design.widget.FloatingActionButton com.google.android.material.floatingactionbutton.FloatingActionButton android.support.v7.widget.Toolbar androidx.appcompat.widget.Toolbar android.support.annotation.Nullable androidx.annotation.Nullable android.support.annotation.LayoutRes androidx.annotation.LayoutRes android.support.design.widget.AppBarLayout com.google.android.material.appbar.AppBarLayout android.support.design.widget.CoordinatorLayout androidx.coordinatorlayout.widget.CoordinatorLayout In your .java AND your .xml layouts In main layout <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <include layout="@layout/content_main_norm" /> </androidx.core.widget.NestedScrollView> as per https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout