Tuesday, March 29, 2016

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

Leave a Comment

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

Answers 1

Not sure if i understood the problem, but if you're running the gradle script by command line, you can always pass a property to it.. ie:

gradle somegradletask -P**propertyDeviceId=123456789** 

You just need to get the Id from the adb shell first, pass it on to the gradle script. Then, inside the gradle script use the propertyDeviceId

androidDeviceId = propertyDevicecId; commandLine file('../beforeInstall.sh').absolutePath, sdkDir <androidDeviceId> 

Happy Coding (or hacking) ;)

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment