android-test icon indicating copy to clipboard operation
android-test copied to clipboard

The UiDevice.getLauncherPackageName() method returns an incorrect value on Android 11+ devices

Open adil-hussain-84 opened this issue 3 years ago • 2 comments

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:

  1. https://github.com/adil-hussain-84/UiAutomatorExperiments/tree/master/app1 – this repo demonstrates the problem.
  2. https://github.com/adil-hussain-84/UiAutomatorExperiments/tree/master/app2 – this repo demonstrates a workaround.

adil-hussain-84 avatar Nov 01 '21 18:11 adil-hussain-84

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?

adil-hussain-84 avatar Nov 05 '21 12:11 adil-hussain-84

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

nlgtuankiet avatar Feb 15 '22 03:02 nlgtuankiet

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

brettchabot avatar Mar 27 '23 17:03 brettchabot