How to know the Android device id in the building process of Android Studio

Viewed 794

I have been developing an Android home app using Android Studio. The app is kind of special app, I need to modify the android device with adb shell commands before installing the apk file. In order to archive this, I wrote a gradle task in build.gradle file, then execute a bash script file. This approach is fine except when only one device is connected.

However when multiple android devices are connected, the shell script does not work because I can not specify the android device id.

The question is how I can get the android device id in build.gralde file using Android Studio?

build.gradle

task clearHome(type: Exec) {

    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
    def sdkDir = properties.getProperty('sdk.dir')

    // I'd like to know <android device id> that I selected using Android Stdio.
    commandLine file('../beforeInstall.sh').absolutePath, sdkDir <android device id>
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn clearHome
}

beforeInstall.sh

#!/bin/bash

ADB=$1/platform-tools/adb
ADB_SHELL="$ADB -s $2 shell"

$ADB_SHELL pm enable com.android.launcher
$ADB_SHELL am start -n com.android.launcher/com.android.launcher2.Launcher
1 Answers
Related