The launcher name on Android 11 is returned as com.android.settings instead of the actual launcher package name
getLauncherPackagename() returns com.android.settings. So mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT); doesn't really wait for the launcher package.
I believe this is due to the package visibility changes on Android 11.
Our test setup consisted of this check before starting with the tests:
String currentPackageName = device.getCurrentPackageName(); if (currentPackageName != null) { assertEquals(packageName, currentPackageName); }
This failed as currentPackageName returned as com.google.android.apps.nexuslauncher and getLauncherPackagename() returned com.android.settings.
I created an issue in IssueTracker about this here but sadly the Google engineer who picked it up marked it as "Won't fix (Intended behaviour)" 😔 In hindsight, now that I know a bit more about the issue, I probably should have created it as an issue in the uiautomator library.
I think this behavior is WAI. At least on Android 11 com.android.settings does define a launcher activity as well. During the boot process once the phone is unlocked, this settings FallbackHome disables itself, allowing the com.google.android.apps.nexuslauncher activity to take over.
The device continues to report com.android.settings as the launcher package name even after the boot process has completed and the phone is unlocked. The only way I've found to get the UiDevice.getLauncherPackageName() method to return the correct launcher package name, as further described here, is to add the following item in my app's AndroidManifest.xml file:
<queries>
<intent>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
</intent>
</queries>
However, as I'm sure most developers would agree, it's not a great solution to have to add an entry in the app's manifest file which is only for testing.
I'll raise an issue in the android-test repo this weekend proposing that the implementation of the UiDevice.getLauncherPackageName() method should be revised to work on Android 11+ devices or that its supporting documentation comment should be revised to call out its limitations.
I've created an issue in the android-test GitHub repo here.
Thank you @adil-hussain-84 for creating the issue.
Going back to your previous point, you could also create a test specific manifest file and add in the modifications there.
@pkarmaka I wish it worked that way. Sadly, entries in the androidTest/AndroidManifest.xml file are not picked up by the application under test. You can verify this by moving the debug/AndroidManifest.xml file in the UiAutomatorExperiments/app2 application module so it sits at androidTest/AndroidManifest.xml. You'll see that the test methods in the UiAutomatorTest test class start to fail.