android – 在Android 11上執行 getPackageInfo 顯示錯誤訊息 NameNotFoundException

Posted in :

將targetSdkVersion設置為30(Android 11)後,執行android.content.pm.PackageManager$NameNotFoundException時得到packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS)。

解法:
https://developer.android.com/preview/privacy/package-visibility:

Android 11 changes how apps can query and interact with other apps that the user has installed on a device. Using the new element, apps can define the set of other apps that they can access. This element helps encourage the principle of least privilege by telling the system which other apps to make visible to your app, and it helps app stores like Google Play assess the privacy and security that your app provides for users.

If your app targets Android 11, you might need to add the element in your app’s manifest file. Within the element, you can specify apps by package name or by intent signature.

如果只有特定的 package

更新AndroidManifest.xml:

<manifest ...>
    ...
    <queries>
        <package android:name="com.target-package" />
    </queries>
    ...
     <application ...>
    ...
</manifest>

如果想全部 package

AndroidManifest.xml中:

<manifest ...>
     ...
     <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
     ...
     <application ...>
     ...
</manifest>

相關文章:

NameNotFoundException when calling getPackageInfo on Android 11
https://stackoverflow.com/questions/62345805/

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *