如何adb将apk安装到多个连接的设备上?

时间:2011-12-22 23:10:55

标签: java android eclipse adb adt

我有7台设备插入我的开发机器。

通常我会adb install <path to apk>并且只能安装到一个设备上。

现在我想在我所有的7台连接设备上安装我的apk。我怎样才能在一个命令中执行此操作?我也许想要运行一个脚本。

21 个答案:

答案 0 :(得分:73)

您可以使用adb devices获取已连接设备的列表,然后为列出的每台设备运行adb -s DEVICE_SERIAL_NUM install...

像(bash):

adb devices | tail -n +3 | cut -sf 1 -d " " | xargs -iX adb -s X install ...

评论表明,对于较新版本,这可能会更好:

adb devices | tail -n +2 | cut -sf 1 | xargs -iX adb -s X install ...

对于Mac OSX(未在Linux上测试):

adb devices | tail -n +2 | cut -sf 1 | xargs -I {} adb -s {} install ...

答案 1 :(得分:12)

其他答案非常有用,但并不能完全满足我的需要。我想我会发布我的解决方案(一个shell脚本),以防它为其他读者提供更多的清晰度。它安装了多个apks和任何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"

感谢所有其他答案让我达到这一点。

答案 2 :(得分:10)

这是一个根据kichik的回应定制的功能性一行命令(谢谢!):

  

adb devices |尾巴-n + 2 | cut -sf 1 | xargs -iX adb -s X install -r * .apk

但如果您碰巧使用Maven,那就更简单了:

  

mvn android:deploy

答案 3 :(得分:6)

另一个简短的选择......我在这个页面上偶然发现-s $SERIAL必须在实际的adb命令之前出现!谢谢stackoverflow!

for SERIAL in $(adb devices | grep -v List | cut -f 1);
do `adb -s $SERIAL install -r /path/to/product.apk`;
done

答案 4 :(得分:6)

Dave Owens的通用解决方案在所有设备上运行任何命令:

for SERIAL in $(adb devices | grep -v List | cut -f 1);
do echo adb -s $SERIAL $@;
done

将它放在像“adb_all”这样的脚本中,并使用与单个设备的adb相同的方式。

我发现的另一个好处是为每个命令分叉后台进程,并等待它们完成:

for SERIAL in $(adb devices | grep -v List | cut -f 1);
do adb -s $SERIAL $@ &
done

for job in `jobs -p`
do wait $job
done

然后,您可以轻松创建脚本来安装应用并启动活动

./adb_all_fork install myApp.apk
./adb_all_fork shell am start -a android.intent.action.MAIN -n my.package.app/.MainActivity

答案 5 :(得分:3)

我喜欢workingMatt's script,但认为可以改进一下,这是我的修改版本:

#!/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

我的版本做了同样的事情,除了:

  • 它从项目的根目录中找到apks
  • 它同时安装到每个设备
  • 它排除了apks的“未对齐”版本(这些版本只是被对齐版本安装了
  • 如果他们的设备ID
  • ,它会显示手机的可读名称

有几种方法可以改进,但我对它很满意。

答案 6 :(得分:2)

以下命令应该有效:

$ adb devices | tail -n +2 | head -n -1 | cut -f 1 | xargs -I X adb -s X install -r path/to/your/package.apk

adb devices返回设备列表。使用tail -n +2从第2行开始并使用-n -1来删除末尾的最后一个空行。通过使用默认制表符分隔符进行剪切管道,可以获得第一列作为连续符号。

xargs用于为每个序列运行adb命令。如果不重新安装,请删除-r选项。

答案 7 :(得分:2)

使用this script即可:

adb+ install <path to apk>

干净,简单。

答案 8 :(得分:2)

如果您不想使用未启用adb的设备;使用这个

的Mac / Linux的

adb devices | grep device | grep -v devices | awk '{print$1}' | xargs -I {} adb -s {} install path/to/yourApp.apk 

adb devices | grep device | grep -v devices | cut -sf 1 | xargs -I {} adb -s {} install path/to/yourApp.apk

答案 9 :(得分:1)

使用此命令行实用程序:adb-foreach

答案 10 :(得分:0)

关键是在单独的流程(&amp;)中启动adb

我想出了以下脚本,同时在我的所有连接设备上启动安装,最后在每个设备上启动已安装的应用程序:

#!/bin/sh

function install_job { 

    adb -s ${x[0]} install -r PATH_TO_YOUR_APK
    adb -s ${x[0]} shell am start -n "com.example.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER

}


#iterate over devices IP-addresses or serial numbers and start a job 

while read LINE
do
    eval x=($LINE)
    install_job ${x[0]} > /dev/null 2>&1 &
done <<< "`adb devices |  cut -sf 1`"

echo "WATING FOR INSTALLATION PROCESSES TO COMPLETE"
wait

echo "DONE INSTALLING"

注1: STDOUT和STDERR被抑制。你不会看到任何&#34; adb install&#34;运作结果。如果你真的需要

,这可能会有所改善

注2:您还可以通过提供args而不是硬编码的路径和活动名称来改进脚本。

那样你:

  1. 不必在每台设备上手动执行安装
  2. 不必等待一次安装才能执行另一次安装(adb任务并行启动)

答案 11 :(得分:0)

我从@WorkingMatt 添加到 the answer

我更新了他的答案以另外做以下事情

  1. 尝试通过开放端口 5555 连接到本地网络上的所有设备。警告:这可能存在安全风险。小心连接到任意端口,除非您知道它是安全的。我没有接受过网络安全方面的培训,所以请谨慎对待我的建议。
  2. 如果安装了软件包,则卸载以前版本的软件包(在我的情况下,我正在卸载以删除以前的应用数据)
#!/bin/bash
echo "Installatron2"

# Connect to all devices on the local network (in our case 192.168.0.0)
# This section requires nmap (You may need sudo apt install nmap)
echo "Scanning the network for connected debuggable devices"
ADDRESSES=$(nmap --open -p 5555 192.168.0/24 -oG - | grep "/open" | awk '{ print $2 }')
for ADDRESS in $ADDRESSES;
do
  adb connect $ADDRESS
done

# Print devices connected to
echo "Connected to the following devices"
echo "$(adb devices)"

# Iterate through all apks in current directory
for SERIAL in $(adb devices | tail -n +2 | cut -sf 1);
do
  for APKLIST in $(ls *.apk);
  do
  #Get the package name from the apk file (Needs sudo apt install aapt)
  package=$(aapt dump badging "$APKLIST" | awk '/package/{gsub("name=|'"'"'","");  print $2}')

  # Optionally uninstalls the pre-existing version of this package (In case you do not want to retain data)
  echo "Uninstalling $package on $SERIAL"
  adb uninstall $package

  # Now install with replacement to the same device
  echo "Installatroning $APKLIST on $SERIAL"
  adb -s $SERIAL install -r $APKLIST
  done
done

echo "Installatron2 has left the building"

答案 12 :(得分:0)

我想记录一下安装过程中发生的情况,还需要对它有所了解。结束于:

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; \    
; }'

在Linux和Mac上测试

答案 13 :(得分:0)

这里是bash,用于在所有连接的设备上安装和运行apk

使用

p

installAndRunApk.sh

nick@nickolay:/home/workspace/MyProject$ > bash path/to/installAndRunApk.sh

答案 14 :(得分:0)

非常简单,您可以创建一个installapk.bat文件,该文件可以将多个apk应用于多个连接的设备,并使用notepad ++打开installapk.bat并复制粘贴此代码

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

答案 15 :(得分:0)

由于我无法评论@Tom的答案,因此这在OSX 10.13上对我有效

adb devices | tail -n +2 | cut -sf 1 | xargs -IX adb -s X install -r path/to/apk.apk

(将小我变成大我)

答案 16 :(得分:0)

这个命令很完美 var filtrooo = document.getElementById("filtrooo"); function functionName() { document.getElementById("textField").value ="txt"; } filtrooo.addEventListener("click", functionName);

答案 17 :(得分:0)

源自此处:Make The Previous Post A Mass APK Installer That Does Not Uses ADB Install-Multi Syntax

@echo off

:loop
      ::-------------------------- has argument ?
      if ["%~1"]==[""] (
        echo done.
        goto end
      )
      ::-------------------------- argument exist ?
      if not exist %~s1 (
        echo error "%~1" does not exist in file-system.
      ) else (
        echo "%~1" exist
        if exist %~s1\NUL (
          echo "%~1" is a directory
        ) else (
          echo "%~1" is a file! - time to install:
          call adb install %~s1
        )
      )
      ::--------------------------
      shift
      goto loop


:end

pause

::: ##########################################################################
::: ##                                                                      ##
::: ##  0. run:   adb devices   - to start the deamon and list your device  ##
::: ##                                                                      ##
::: ##  1. drag&drop ANY amount of files (APK) over this batch files,       ##
::: ##                                                                      ##
::: ##       - it will install them one by one.                             ##
::: ##       - it just checks if file exists.                               ##
::: ##       - it does not checks if it is a valid APK package              ##
::: ##       - it does not checks if package-already-installed              ##
::: ##       - if there is an error you can always press [CTRL]+[C]         ##
::: ##           to stop the script, and continue from the next one,        ##
::: ##           some other time.                                           ##
::: ##       - the file is copied as DOS's 8.3 naming to you                ##
::: ##           don't need to worry about wrapping file names or renaming  ##
::: ##           them, just drag&drop them over this batch.                 ##
::: ##                                                                      ##
::: ##                                   Elad Karako 1/1/2016               ##
::: ##                                   http://icompile.eladkarako.com     ##
::: ##########################################################################

答案 18 :(得分:0)

PowerShell解决方案

function global:adba() {
    $deviceIds = iex "adb devices" | select -skip 1 |  %{$_.Split([char]0x9)[0].Trim() } | where {$_ -ne "" }
    foreach ($deviceId in $deviceIds) {
        Echo ("--Executing on device " + $deviceId + ":---")
        iex ("adb -s $deviceId " + $args)
    }
}

将它放在您的个人资料文件(notepad $PROFILE)中,重新启动您的shell,然后您可以使用以下命令调用安装:

adba install yourApp.apk

答案 19 :(得分:0)

使用Android Debug Bridge版本1.0.29,试试这个bash script

APK=$1

if [ ! -f `which adb` ]; then
    echo 'You need to install the Android SDK before running this script.';
    exit;
fi

if [ ! $APK ]; then
    echo 'Please provide an .apk file to install.'
else
    for d in `adb devices | ack -o '^\S+\t'`; do
        adb -s $d install $APK;
    done
fi

不确定它是否适用于早期版本。

答案 20 :(得分:-2)

- 获取apk文件夹中存储的所有.apk

- 在设备上安装和更换应用

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