Android 13 (SDK 33): PackageManager.getPackageInfo(String, int) deprecated. what is the alternative?
Asked Answered
F

3

64

Starting from API level 33 the getPackageInfo(String, int) method of PackageManager class is deprecated. Documentation suggests to use getPackageInfo(String, PackageInfoFlags) instead. But that function is only available from API level 33.

My current code:

val pInfo = context.packageManager.getPackageInfo(context.packageName, 0)

Is this how it should be now?

val pInfo = context.getPackageInfo()

@Suppress("DEPRECATION")
fun Context.getPackageInfo(): PackageInfo {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0))
    } else {
        packageManager.getPackageInfo(packageName, 0)
    }
}
Fictitious answered 17/8, 2022 at 11:55 Comment(2)
You may need to catch NameNotFoundException exception - if packageName turns into a param.Rostellum
For reference: developer.android.com/reference/android/content/pm/…Equalizer
R
21

Is this how it should be now?

Yes, though I've gotten out of the practice of using TIRAMISU in favor of the actual underlying Int.

Ideally, Google would add stuff to PackageManagerCompat for these changes, and perhaps they will now that Android 13 is starting to ship to users.

Rigveda answered 17/8, 2022 at 12:8 Comment(3)
I filed a feature request for a new PackageManagerCompat function in androidx.core (issuetracker.google.com/issues/246845196).Andraandrade
Isn't it safe to use the TIRAMISU-constant in older android-versions that don't know about it, ie doesn't the compiler inline the constant with its Int-value?Santo
@arne.jans: Yes -- the OP's concern was about needing to call both flavors of getPackageInfo().Rigveda
D
62

If you are using Kotlin, you can add extension function to your project:

fun PackageManager.getPackageInfoCompat(packageName: String, flags: Int = 0): PackageInfo =
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(flags.toLong()))
    } else {
        @Suppress("DEPRECATION") getPackageInfo(packageName, flags)
    }

and after just call packageManager.getPackageInfoCompat(packageName) or add another flag, if you need.

Downpour answered 9/12, 2022 at 9:59 Comment(4)
Do I have to check Android version here? Wouldn't it just work for older Android versions if I use the new API?Maidenhead
@Ali Kazi no, since older Android versions don't have the new API, it was introduced in API-level 33 (aka Android 13 Tiramisu), usage of the new API will crash on older Android versions with a NoSuchMethodError. Also, the Android linter should mark that with an error or at least a warning in your code immediately.Santo
You could use the old API for now, but it is risky, since we do not know when Google will delete the old API in favor of the new API, since a deprecation is a marker that tells us Google wants to delete this code at some time in a future android version.Santo
Depending on what you set your targetSdkVersion to in your build.gradle, it is possible that Android selects certain backward-compatibility mechanisms that prevent a crash, but Google Playstore policy will get updated in the future so that you can't release or update an app with a legacy targetSdkVersion (at the moment, the minimum value for targetSdkVersion is 31, if you want to release for Play Store).Santo
R
21

Is this how it should be now?

Yes, though I've gotten out of the practice of using TIRAMISU in favor of the actual underlying Int.

Ideally, Google would add stuff to PackageManagerCompat for these changes, and perhaps they will now that Android 13 is starting to ship to users.

Rigveda answered 17/8, 2022 at 12:8 Comment(3)
I filed a feature request for a new PackageManagerCompat function in androidx.core (issuetracker.google.com/issues/246845196).Andraandrade
Isn't it safe to use the TIRAMISU-constant in older android-versions that don't know about it, ie doesn't the compiler inline the constant with its Int-value?Santo
@arne.jans: Yes -- the OP's concern was about needing to call both flavors of getPackageInfo().Rigveda
A
0

Sometime this kind of issue you will see when you are using lower version gradle like 6.9 or below then this and in your android studio you have configured latest java sdk. so to solve just lower your java version from android studio. screen shot:

enter image description here

Actress answered 11/4 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.