androidubuntuadbudevfastboot

Automatically installing multiple apps on Android devices when connected to a linux system


I had written some scripts using Ubuntu 14.04 and adb to automatically configure 100´s of Android smartphones. To achieve that I used an udev rule to identify if an USB device is attached to the pc and if that's the case I call my scripts to install some apps onto the devices.

So far, so good but there are some problems. At the moment we are connecting 5 devices at the same time, but it happens quite often that on one of the devices one ore more apps are not being installed. It does not follow a pattern, it just happens randomly. Sometimes also the performance of the computer decreases during the day.

Here is my udev rule:

ACTION=="add", ENV{DEVTYPE}=="usb_device", ATTRS{idVendor}!="1d6b", ATTRS{idVendor}!="203a", ATTRS{idVendor}=="****", ATTRS{idProduct}=="****", RUN+="/usr/local/bin/selectDevices.sh"

The first script in /usr/local/bin/ to identify the devices on witch the apps should be installed

#!/bin/bash

sleep 2
Pid=/home/android/Schreibtisch/PID

for DEVICE in `adb devices | tail -n +2 | grep device | awk '{print $1}'`;
do

  if [ ! -f $Pid/$DEVICE.pid ];
  then

    touch $Pid/$DEVICE.pid
    sh /usr/local/bin/touchDevices.sh $DEVICE

  fi

done

and the last script to install the apps

#!/bin/bash
cd /home/android/Desktop/Apps

for APK in $(ls *.apk);
do
    adb -s $1 install $APK
done

Edit 1: I´ve tried it like Alex P. suggested it or in my interpretation of his answer. Pass the serial to a tmp script and the tmp script is calling the actual installation script with at now. Before the installation script starts it sleeps 2 seconds. But the problems still exist. Maybe I need to log what's happening during the installation but at the moment I have no clue how to do that.

Edit 2: After some time I think I found something but I still have no idea why. I think adb has problems when two devices want to install one app at the same time. I Managed it to receive an error message like rm failed for /data/local/tmp/foo.apk, No such file or directory. After that I switcht to first copy the Package to the devices and then install them with the shell package manager, but still no solution.


Solution

  • I run multiple linux systems configured to automatically run adb and fastboot commands on device enumeration which process hundreds of Android device connections per day without a hitch. Based on my experience I have the following advice for you:

    Your install script should look like this:

    APKDIR="/full/path/to/apks"
    PIDDIR="/full/path/to/pid"
    ADB="/full/path/to/adb -s usb:$1"
    
    if [ ! -f $PIDDIR/$2.pid ]; then
        touch $PIDDIR/$2.pid
        CMD="sleep 5"
        for APK in $APKDIR/*.apk; do CMD="$CMD; $ADB install $APK"; done
        echo "$CMD" | at -M now 2>/dev/null
    fi
    

    Add the call to your install script to the end of the udev rule:

    , RUN+="/bin/sh /full/path/to/the/script.sh %k $env{ID_SERIAL_SHORT}"

    If this setup still does not produce stable results - try increasing the sleep duration in the script.

    Also it helps to assign bigger number (90+) to your udev rule