As per documentation,
The instant apps are restricted to discover the list of installed apps on the device, unless the installed apps have made themselves discoverable to instant apps.
There is also a way to make our app discoverable to instant apps.
But when we use getPackageManager()
, the code crashes with the
error = Unfortunately, TheAppName instant app has stopped.
Nothing is printed in Logcat. Following is printed on the Debug console,
02/10 22:57:27: Launching instantapp
Side loading instant app.
Side loading instant app.
Launching deeplink: https://myapp.mycompany.com/example.
$ adb shell setprop log.tag.AppIndexApi VERBOSE
$ adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d https://myapp.mycompany.com/example -n "com.google.android.instantapps.supervisor/.UrlHandler"
Waiting for application to come online: com.mycompany.myapp.instantappscanner.test | com.mycompany.myapp.instantappscanner
Waiting for application to come online: com.mycompany.myapp.instantappscanner.test | com.mycompany.myapp.instantappscanner
Waiting for application to come online: com.mycompany.myapp.instantappscanner.test | com.mycompany.myapp.instantappscanner
Could not connect to remote process. Aborting debug session.
Code:
PackageManager packageManager = getPackageManager();
List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d(TAG, "Installed package :" + packageInfo.packageName);
Log.d(TAG, "Source dir : " + packageInfo.sourceDir);
}
Android Version : 6.0.1
Update
The reason for not printing Logs is debugger was not attached to the App. The logs from adb console shows:
02-11 21:06:56.347: E/AndroidRuntime(18033): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myapp.aninstantapp/com.mycompany.myapp.aninstantapp.feature.MainActivity}: java.lang.SecurityException: Method class android.content.pm.IPackageManager$Stub$Proxy.getInstalledApplications[int, int] not available to instant apps
Stack trace prints that the call is not allowed from an instant app, but what if few apps made themselves discoverable to instant apps? Rather than throwing exception method call should return a partial list of apps containing only apps which made themselves discoverable to instant apps?
Short Question
Need help executing following code from an instant app.
List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
It throws exception,
SecurityException: IPackageManager$Stub$Proxy.getInstalledApplications[int, int] not available to instant apps
It should not throw an exception because one can have few apps which made themselves discoverable to instant apps, as supported by documentation (above).