android-test
android-test copied to clipboard
The UiDevice.getLauncherPackageName() method returns an incorrect value on Android 11+ devices
Description
The UiDevice.getLauncherPackageName() method returns an incorrect value on Android 11+ devices. Most likely, this is due to package visibility changes in Android 11. If the method cannot be fixed, it would be useful for its documentation comment to be updated to call out its limitations.
Steps to Reproduce
Call the UiDevice.getLauncherPackageName() method on an Android 11+ device (e.g. Pixel 4a or an emulator).
Expected Results
The method returns the device's default launcher package name. For example, on a Pixel 4a or emulator device this would be com.google.android.apps.nexuslauncher
.
Actual Results
The method returns com.android.settings
.
AndroidX Test and Android OS Versions
The latest version of the androidx.test.uiautomator:uiautomator
library (i.e. 2.2.0
) as well as older versions display the issue when run on Android 11+ devices.
Link to a public git repo demonstrating the problem:
- https://github.com/adil-hussain-84/UiAutomatorExperiments/tree/master/app1 – this repo demonstrates the problem.
- https://github.com/adil-hussain-84/UiAutomatorExperiments/tree/master/app2 – this repo demonstrates a workaround.
I just had a look into the implementation of the UiDevice.getLauncherPackageName() method and here's something which confuses me which I hope someone can clarify...
The UiDevice.getLauncherPackageName() method queries for the device's launcher package name based on the instrumentation's context (i.e. Instrumentation.getContext()) as opposed to the target application's context (i.e. Instrumentation.getTargetContext()). With this in mind, I'd expect the following queries
element to fix the UiDevice.getLauncherPackageName() method when it's added to the application's androidTest/AndroidManifest.xml
file:
<queries>
<intent>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
</intent>
</queries>
However, what I'm finding is that this queries
element has to be defined in the application's debug/AndroidManifest.xml
file to fix the UiDevice.getLauncherPackageName() method. (See UiAutomatorExperiments/app2 for an example.)
Why is it that the presence of the above queries
element does not fix the UiDevice.getLauncherPackageName() method when it's added to the application's androidTest/AndroidManifest.xml
file given that the UiDevice.getLauncherPackageName() method queries for the device's launcher package name based on the instrumentation's context?
add
<queries>
<intent>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
</intent>
</queries>
to app/src/debug/AndroidManifest.xml
will fix the bug
This is because the package visibility changes introduced in Android 11
Sorry for the lack of response here. If you're still having issues can you file a bug here https://issuetracker.google.com/issues/new?component=192731? androidx.test.uiautomator is not homed in this repository