Darshaan Aghicha
Darshaan Aghicha

Reputation: 101

Android Studio: PackageManager package not found | How to access packages on Android 11?

I am trying to run this sample code from TutorialPoint but it is always showing toast

Intent launch = getPackageManager().getLaunchIntentForPackage("com.google.android.youtube");
    if (launch != null) {
        startActivity(launch);
    } else {
        Toast.makeText(P01.this,
                "There is an issue running this code!",
                Toast.LENGTH_SHORT).show();
    }

Can someone please help with how to rectify it!

After requesting help over here, I searched for something relevant on Stack Overflow and came across this post. I used it to display all packages on my device, but instead, it only listed a handful:

ResolveInfo{f1539ce com.android.settings/.Settings m=0x108000}
ResolveInfo{6fa90ef com.android.vending/.AssetBrowserActivity m=0x108000}
ResolveInfo{8b2b7fc com.android.stk/.mtk.StkLauncherActivityII m=0x108000}
ResolveInfo{10f4e85 com.dsaghicha.uscs407practicals/.MainActivity m=0x108000}

The last one being my app's package. If I run the above code on any one of these packages it will run

Apparently, I am using Android 11 and there is a privacy policy that restricts package visibility on Android 11. Read more Now, how to access the package on Android 11?

Upvotes: 0

Views: 3772

Answers (1)

Darshaan Aghicha
Darshaan Aghicha

Reputation: 101

In Android 11, a new Privacy Policy Update restricts package visibility to your android app.

So, you have to explicitly request access to the package by declaring it in the AndroidManifest.xml

<queries>
    <package android:name="com.google.android.youtube"/>
</queries>

Adding this piece of code to AndroidManifest.xml resolved the issue in my case.

Resources:

Upvotes: 6

Related Questions