2

I am writing a small gradle plugin where I need to use the adb command. My question is how does the installDebug task know all the devices? By default it will been installed on all devices. I found the installTask but there is no -s argument for specifying a device.

How did they manage that in the Android gradle plugin?

Bonus question: In Android Studio I can select a special device, how can I know that from the gradle script?

rekire
  • 47,260
  • 30
  • 167
  • 264

2 Answers2

2

InstallTask is not present in the latest sources. The InstallVariantTask is used instead of it. As you can see from its sources it checks ANDROID_SERIAL environment variable for specific device selection and if the variable is not present then the install task is run against all compatible devices (the same is done for uninstall and connectedAndroidTest tasks).

Bonus answer: that's not possible AFAIK. Android Studio does not install/launch the application via gradle. In .idea/workspace.xml file you will find the ANDROID_EXTENDED_DEVICE_CHOOSER_SERIALS property, which is updated at some point of time after your APK was deployed, but this info is not available during the gradle script run.

Volo
  • 28,673
  • 12
  • 97
  • 125
0

In more recent versions of Android Studio .idea/deploymentTargetDropDown.xml will contain a list of possible targets, the most recently selected target, and the time at which it was selected

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="deploymentTargetDropDown">
    <runningDeviceTargetSelectedWithDropDown>
      <Target>
        <type value="RUNNING_DEVICE_TARGET" />
        <deviceKey>
          <Key>
            <type value="SERIAL_NUMBER" />
            <value value="some_serial_number" />
          </Key>
        </deviceKey>
      </Target>
    </runningDeviceTargetSelectedWithDropDown>
    <timeTargetWasSelectedWithDropDown value="2023-02-22T12:03:45.955233Z" />
 </component>

The same file is also stored in the settings directory

It seems like an implementation detail not to be relied on for anything mission critical, but is populated as soon as you make a selection, making it useful for deployment related scripts

Selali Adobor
  • 2,060
  • 18
  • 30