How can I adb install an apk to multiple connected devices?

后端 未结 20 1822
余生分开走
余生分开走 2020-11-30 19:03

I have 7 devices plugged into my development machine.

Normally I do adb install and can install to just a single device.

Now

相关标签:
20条回答
  • 2020-11-30 19:21

    well its simple you can create a installapk.bat file that can do the job for multiple apk to multiple connected devices open installapk.bat with notepad++ and copy paste this code

    FOR /F "skip=1"  %%x IN ('adb devices') DO start adb -s %%x install -r Facebook.apk
    FOR /F "skip=1"  %%x IN ('adb devices') DO start adb -s %%x install -r Instagram.apk
    FOR /F "skip=1"  %%x IN ('adb devices') DO start adb -s %%x install -r Messenger.apk
    FOR /F "skip=1"  %%x IN ('adb devices') DO start adb -s %%x install -r Outlook.apk
    FOR /F "skip=1"  %%x IN ('adb devices') DO start adb -s %%x install -r Viber.apk
    FOR /F "skip=1"  %%x IN ('adb devices') DO start adb -s %%x install -r WhatsApp.apk
    
    0 讨论(0)
  • 2020-11-30 19:22

    The other answers were very useful however didn't quite do what I needed. I thought I'd post my solution (a shell script) in case it provides more clarity for other readers. It installs multiple apks and any mp4s

    echo "Installatron"
    
    for SERIAL in $(adb devices | tail -n +2 | cut -sf 1);
    do 
      for APKLIST in $(ls *.apk);
      do
      echo "Installatroning $APKLIST on $SERIAL"
      adb -s $SERIAL install $APKLIST
      done
    
      for MP4LIST in $(ls *.mp4);
      do
      echo "Installatroning $MP4LIST to $SERIAL"
      adb -s $SERIAL push $MP4LIST sdcard/
      done
    done
    
    echo "Installatron has left the building"
    

    Thank you for all the other answers that got me to this point.

    0 讨论(0)
  • 2020-11-30 19:22

    I was wanting to log what was happening whilst installing, also needed it to be slightly comprehendable. Ended up with:

    echo "Installing app on all connected devices."
    
    adb devices | tail -n +2 | cut -sf 1 | xargs -I % sh -c '{ \
        echo "Installing on %"; \
        adb -s % \
            install myApp.apk; \    
    ; }'
    

    Tested on Linux & Mac

    0 讨论(0)
  • 2020-11-30 19:23

    Use this command-line utility: adb-foreach

    0 讨论(0)
  • 2020-11-30 19:23

    -Get all the apk stored in .apk folder

    -Install and replace app on devices

    getBuild() {
        for entry in .apk/*
        do
            echo "$entry"
        done
        return "$entry"
    }
    
    newBuild="$(getBuild)"
    
    adb devices | while read line
    do
      if [! "$line" = ""] && ['echo $line | awk "{print $2}"' = "device"]
      then
          device='echo $line | awk "{print $1}"'
          echo "adb -s $device install -r $newbuild"
          adb -s $device install -r $newbuild
      fi
    done
    
    0 讨论(0)
  • 2020-11-30 19:24

    I liked workingMatt's script but thought it could be improved a bit, here's my modified version:

    #!/bin/bash
    
    install_to_device(){
    local prettyName=$(adb -s $1 shell getprop ro.product.model)
    echo "Starting Installatroning on $prettyName"
    for APKLIST in $(find . -name "*.apk" -not -name "*unaligned*");
      do
      echo "Installatroning $APKLIST on $prettyName"
      adb -s $1 install -r $APKLIST
      adb -s $1 shell am start -n com.foo.barr/.FirstActivity;
      adb -s $1 shell input keyevent KEYCODE_WAKEUP
      done
      echo "Finished Installatroning on $prettyName"
    }
    
    echo "Installatron"
    gradlew assembleProdDebug
    
    for SERIAL in $(adb devices | tail -n +2 | cut -sf 1);
    do 
      install_to_device $SERIAL&
    done
    

    My version does the same thing except:

    • it finds the apks from the root of the project
    • it installs to every device simultaneously
    • it excludes the "unaligned" versions of the apks (these were just being installed over by the aligned versions anyway
    • it shows the readable names for the phones instead if their device ids

    There's a few ways it could still be improved but I'm quite happy with it.

    0 讨论(0)
提交回复
热议问题