开发者

install and run an app on multiple devices with a single click under eclipse

I am wondering if there is a way to install and run an android application on multiple devices/emulator with a single click under Eclipse.

When I am testing a layout on multiple (n) 开发者_运维百科devices I am doing right now n times run-as (ctrl-F11 short cut in fact), choose the correct device and then check my layout on each device.

It would be great to use a single command to install and launch on all devices connected to my computer.

Does a such thing exist?


If you using eclipse, just do below:

  1. open 'project properties' dialog.
  2. under 'Run/Debug settings', select your project and edit.
  3. another dialog should pop up, select 'target' tab, select 'Launch on all compatible devics/AVD's '.

  4. Apply and you should get all of devices install when Run/Debug.


The simplest way is to have a batch file (or shell script) with several lines

adb -s SERIAL_NO install workspace/YOUR_PROJECT/bin/YOUR_APK.apk

Make sure that Eclipse is set to build the project automatically. Then, it's not one-click, but close:

  • Ctl-Shift-S to save all,
  • Alt-TAB to get to the command prompt,
  • up arrow enter to rerun the script.
  • Enjoy. 5 seconds tops ;)


I just finished the script Aleadam advice me to do.

A simple loop on each device connected to the computer with install and start.

Script andinstall:

#!/bin/bash

if [ $# -lt 3 ] ;then
    echo "usage $0 pathToTheFileToInstall packagename fullPackageActivityName"
    exit 1
fi

FILEPATH=$1
PACKAGE=$2
ACTIVITYNAME=$3

APK=`ls $FILEPATH/*.apk`

installOnSingleDevice(){
    echo "Installing on $DEVICE -------"
    echo "adb -s $1 install -r $APK"
    adb -s $1 install -r $APK 

    echo "adb -s $1 shell am start -n $2/$3"
    adb -s $1 shell am start -n $2/$3
    echo "--------------------------"
}

if [ $? -eq 0 ] ;then
    adb devices | grep device | grep -v attached | cut -f 1 > tmpfile
    while read line; 
    do 
        installOnSingleDevice $line $PACKAGE $ACTIVITYNAME&
    done < tmpfile
    rm tmpfile
else
    echo "No apk at this path (${FILEPATH})"
fi

Usage example:

andinstall ~/workspace/myapp/bin/ fr.openium.myapp fr.openium.myapp.activity.ActivitySplash


You should be able to add an Ant XML file to your Eclipse project, and fill it with Ant tasks that can run an external program from command line. Probably you should have a separate task item for each device you want to run on, and then a parent task containing all of them.

Then add a single custom builder step to Eclipse project, choosing Ant as builder and pointing to the XML file you added previously (see here)

I'm not a big expert on Ant, but at some point I needed to add a custom task to build a Jar out of my project code from within Eclipse, so that I could run it everytime I needed it (like here). This procedure worked quite well.

The Ant task that should do for you is Exec: http://ant.apache.org/manual/Tasks/exec.html. You might want to have a look at http://ant.apache.org/manual/using.html for a simple starter.

Your XML could be something like the following (I haven't tried it though):

<?xml version="1.0"?>
<project name="...use same name as your project for simplicity..." default="runmultidevices">
  <property name="myapk" location="workspace/YOUR_PROJECT/bin/YOUR_APK.apk"/>
  <target name="runmultidevices" description="Run on multiple devices" depends="rundevice1 rundevice2">
  </target>
  <target name="rundevice1" description="Run on device 1">
    <exec executable="adb">
      <arg line="-s SERIAL_NO_1 install ${myapk}" />
    </exec>
  </target>
  <target name="rundevice2" description="Run on device 2">
    <exec executable="adb">
      <arg line="-s SERIAL_NO_2 install ${myapk}" />
    </exec>
  </target>
</project>

In order to not manually write the package location for each project, there seems to be not such a great integration between Eclipse and Ant. You might try the following suggestions:

  • use the native Ant ${basedir} property as in here
  • pass an Eclipse variable as an additional parameter when invoking Ant as in here: -Dworkspace_loc=${workspace_loc}
  • access Eclipse .project file from within Ant, using a specific XML parsing facility as in here


Here is my solution to easily build, install and launch an Android application on multiple devices with exactly two shortcuts STRG+S and STRG+ALT+F10 ;)

1. Build APK

The normal behavior of an Android Build in Eclipse is, that the APK will not be created. But in the preferences is the option to disabled that behavior.

  • Window -> Preferences -> Android -> Build -> Skip packaging and dexing until export or launch

=> After deselecting that option, only a Strg+S is needed to create the APK file after a change in the code.

2. Install and Launch

To install and automatically launch the app for multiple devices, the easiest way is to use the command line and a Windows Batch script in my opinion:

<!-- language: Batch script -->
:: This five lines are used to minimize the 
:: command lines directly after the start
if not "%minimized%"=="" goto :minimized
set minimized=true
start /min cmd /C "%~dpnx0"
goto :EOF
:minimized

:: Path to the ADB and to the APK file
@set ADB="D:/Android/sdk/platform-tools/adb.exe"
@set APK="D:/workspace_android/SomeApp/bin/SomeApp.apk"

:: AndroidManifest.xml: <manifest package="com.example.appname">
:: The launching activity: <activity android:name=".activities.HomeActivity">
@set ACTIVITY=at.example.appname/.activities.HomeActivity

:: Install APK on all devices
%ADB% devices | tail -n +2 | cut -sf 1 | xargs -iX %ADB% -s X install -r %APK%
:: Launch App on all devices
%ADB% devices | tail -n +2 | cut -sf 1 | xargs -iX %ADB% -s X shell am start -a android.intent.action.MAIN -n %ACTIVITY%

In my case I have three devices. For faster access to one device, I used the following code instead of the loop in upper code. First, I install and launch the app on the fastest device and after that on the second and so on. I am sure there are better ways instead of using tail, head and xargs, but I don't know that much about Batch files, but it just runs. ;)

<!-- language: Batch script -->
:: NEXUS 5
:: This command reinstalls the APK on the Nexus 5 device
%ADB% devices | tail -n +2 | head -n +1 | cut -sf 1 | xargs -iX %ADB% -s X install -r     %APK%
:: This command launch the application on the Nexus 5 device
%ADB% devices | tail -n +2 | head -n +1 | cut -sf 1 | xargs -iX %ADB% -s X shell am start -a android.intent.action.MAIN -n %ACTIVITY%

:: Galaxy Tab
%ADB% devices | tail -n -2 | cut -sf 1 | xargs -iX %ADB% -s X install -r %APK%
%ADB% devices | tail -n -2 | cut -sf 1 | xargs -iX %ADB% -s X shell am start -a android.intent.action.MAIN -n %ACTIVITY%

:: Optimus 3D
%ADB% devices | tail -n +3 | head -n +1 | cut -sf 1 | xargs -iX %ADB% -s X install -r %APK%
%ADB% devices | tail -n +3 | head -n +1 | cut -sf 1 | xargs -iX %ADB% -s X shell am start -a android.intent.action.MAIN -n %ACTIVITY%

After having the Windows Batch script, create a shortcut of that file. Right click the shortcut file and select properties. There you can specify a global shortcut key for example STRG+ALT+F10.

After changing some code in Eclipse, you only have to press STRG+S and then STRG+ALT+F10 and the app will be finally launched on all devices. Yeah! :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜