'How to debug Apps in Android Enterprise Work Profile

I've just started developing with Android Enterprise and need to deploy an app in work profile.

My policy (see below) allows installation of apk with debug build variant. Starting a debug session via Android Studio now install the app both in private and work profile which means the policy is active on the work profile. However,the debug session always connects to the app in private profile.

Is it possible to start the debug session directly to the app instance on the work profile?

I've additionally tried with user flag:

adb shell am start -n "com.siemens.fileshare/com.siemens.fileshare.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D -W --user 10

The connection, however, times out.

Here is my policy:

{
  "name": "enterprises/some123/policies/policy1",
  "version": "11",
  "applications": [
    {
      "packageName": "com.google.samples.apps.iosched",
      "installType": "FORCE_INSTALLED"
    },
    {
      "packageName": "com.mycompany.myapp",
      "installType": "AVAILABLE"
    }
  ],
  "debuggingFeaturesAllowed": true
}


Solution 1:[1]

Here is how it worked for me.

Attach a mobile phone with work profile to the PC and make sure USB debugging is available.

Build a fresh debug.apk

Following commands have to be entered in Terminal of Android Studio.

a) Check users tux@C:>adb shell pm list users

Users:

UserInfo{0:owner:13} running UserInfo{12:Work profile:30} running

b) Install App tux@C:>adb install --user 12 "c:\dev\source\test\ShareXml\app\build\outputs\apk\debug\app-debug.apk"

c) Start the debugger tux@C:> adb shell am start --user 12 -n "com.mycompany.myapp/SomeActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D

d) Connect the process manually to the debugger in Android Studio (see the attached screen shot) enter image description here

This post helped me solve the issue: Debugging with Android Studio stuck at "Waiting For Debugger" forever

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1