209

现在我必须下载并安装 Android SDK 和AVD Manager,然后通过 UI 安装 API、工具。有没有办法自动化这个过程?

4

11 回答 11

395

更新

最新版本引入sdkmanager了一个命令行工具,可让您查看、安装、更新和卸载 Android SDK 的软件包。

sdkmanager工具在 Android SDK 工具包(25.2.3 及更高版本)中提供,位于android_sdk/tools/bin/.

  sdkmanager [--uninstall] [<common args>] [--package_file <file>] [<packages>...]
  sdkmanager --update [<common args>]
  sdkmanager --list [<common args>]
  sdkmanager --licenses [<common args>]

In its first form, installs, or uninstalls, or updates packages.
    By default, the listed packages are installed or (if already installed)
    updated to the latest version.

    --uninstall: uninstalled listed packages.

    <package> is a sdk-style path (e.g. "build-tools;23.0.0" or
             "platforms;android-23").
    <package-file> is a text file where each line is a sdk-style path
                   of a package to install or uninstall.
    Multiple --package_file arguments may be specified in combination
    with explicit paths.

In its second form (with --update), all installed packages are
    updated to the latest version.

In its third form, all installed and available packages are printed
    out.

In its fourth form (with --licenses), show and offer the option to
     accept licenses for all available packages that have not already been
     accepted.

Common Arguments:
    --sdk_root=<sdkRootPath>: Use the specified SDK root instead of the SDK
                              containing this tool

    --channel=<channelId>: Include packages in channels up to <channelId>.
                           Common channels are:
                           0 (Stable), 1 (Beta), 2 (Dev), and 3 (Canary).

    --include_obsolete: With --list, show obsolete packages in the
                        package listing. With --update, update obsolete
                        packages as well as non-obsolete.

    --no_https: Force all connections to use http rather than https.

    --proxy=<http | socks>: Connect via a proxy of the given type.

    --proxy_host=<IP or DNS address>: IP or DNS address of the proxy to use.

    --proxy_port=<port #>: Proxy port to connect to.

* If the env var REPO_OS_OVERRIDE is set to "windows",
  "macosx", or "linux", packages will be downloaded for that OS.

所以,更新包运行

sdkmanager --update

要接受许可证,

yes | sdkmanager --licenses

旧答案

(请注意:该android命令已弃用!)

离自动化越近的可能是:

android update sdk --no-ui

android为自动更新提供这些选项:

Action "update sdk":
  Updates the SDK by suggesting new platforms to install if available.
Options:
  -f --force    Forces replacement of a package or its parts, even if something has been modified
  -u --no-ui    Updates from command-line (does not display the GUI)
  -o --obsolete Installs obsolete packages
  -t --filter   A filter that limits the update to the specified types of packages in the form of
                a comma-separated list of [platform, tool, platform-tool, doc, sample, extra]
  -s --no-https Uses HTTP instead of HTTPS (the default) for downloads
  -n --dry-mode Simulates the update but does not download or install anything

如果您想列出可用于安装的软件包,您可以使用

android list sdk

您将获得一个有序的包列表,例如

Packages available for installation or update: 9
   1- ARM EABI v7a System Image, Android API 15, revision 2
   2- Intel x86 Atom System Image, Android API 15, revision 1
   3- Android Support, revision 8
   4- Google AdMob Ads SDK, revision 6
   5- Google Analytics SDK, revision 2
   6- Google Play APK Expansion Library, revision 1
   7- Google Play Billing Library, revision 2
   8- Google Play Licensing Library, revision 2
   9- Google Web Driver, revision 2

如果您使用该--filter选项,您也可以仅将更新限制为所需的组件

android update sdk --filter <component> --no-ui

其中组件是一个或多个

  • 返回的数字android list sdk(即1,也称为包索引
  • 添加在
  • 文档
  • 额外的
  • 平台
  • 平台工具
  • 样本
  • 来源
  • 系统映像
  • 工具

或者可以是一个或多个特定标识符。例如,如果你只想下载一小组特定的包,你可以这样做:

android update sdk -u --filter platform-tools,android-16,extra-android-support

您将获得平台工具、API 级别 16 和支持包 JAR 文件。如果您只构建构建机器并且必须为下载所有您永远不会使用的额外内容付费,这真的很方便。

要查看可用选项,您可以使用 --help,例如

android --help list sdk

       Usage:
       android [global options] list sdk [action options]
       Global options:
  -h --help       : Help on a specific command.
  -v --verbose    : Verbose mode, shows errors, warnings and all messages.
     --clear-cache: Clear the SDK Manager repository manifest cache.
  -s --silent     : Silent mode, shows errors only.

                   Action "list sdk":
  Lists remote SDK repository.
Options:
  -o --obsolete  : Deprecated. Please use --all instead.
  -a --all       : Lists all available packages (including obsolete and
                   installed ones)
     --proxy-host: HTTP/HTTPS proxy host (overrides settings if defined)
     --proxy-port: HTTP/HTTPS proxy port (overrides settings if defined)
  -s --no-https  : Uses HTTP instead of HTTPS (the default) for downloads.
  -e --extended  : Displays extended details on each package
  -u --no-ui     : Displays list result on console (no GUI) [Default: true]
于 2011-01-13T16:10:38.267 回答
50

这对我不起作用...

echo "y" | android ....

所以我最终来到了这里:

expect -c '
set timeout -1   ;
spawn sudo /opt/android-sdk/tools/android update sdk -u;
expect {
    "Do you accept the license" { exp_send "y\r" ; exp_continue }
    eof
}
'
于 2013-07-25T16:44:03.703 回答
46

我使用它在Travis CI上安装和更新 SDK :

curl --location http://dl.google.com/android/android-sdk_r22.3-linux.tgz | tar -x -z -C $HOME
export ANDROID_HOME=$HOME/android-sdk-linux
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
( sleep 5 && while [ 1 ]; do sleep 1; echo y; done ) | android update sdk --no-ui --filter platform-tool,android-19,sysimg-19,build-tools-19.0.1
于 2014-02-20T14:03:51.360 回答
16

要使用“y”回答所有许可证,您可以在脚本中尝试:

(while :
do
  echo 'y'
  sleep 2
done) | android update sdk -u .....
于 2013-09-23T13:28:03.047 回答
5

对于仍在寻找下载所有 Android 软件包的方法的任何人,我已经编写了一个脚本来做到这一点。它将下载所有未过时的软件包。

#!/binbash
# Install all non-obsolete Android SDK packages.
# author: Tai Le Tien (letientai299 at gmail.com)

function install_sdk {
  android update sdk -u -s -a -t "$1"
}

function fetch_non_obsoled_package_indices {
  # Fetch the SDK list using non-https connections
  android list sdk -u -s -a |\
    # Filter obsoleted packages
    sed '/\(Obsolete\)/d' |\
    # Filter to take only the index number of package
    sed 's/^[ ]*\([0-9]*\).*/\1/' |\
    # Remove the empty lines
    sed -n 's/^[^ $]/\0/p'
}

for package_index in  $(fetch_non_obsoled_package_indices)
do
  echo "====================================================================="
  echo "Start to install package:  ${package_index}"
  echo "====================================================================="
  # Auto accept license
  echo -e "y" | install_sdk "${package_index}"
  echo
  echo
done

您也可以在我的GitHub 存储库中看到它

好的:

  • 不依赖expect
  • 无头。

缺点:

  • 您仍然必须手动安装基本 SDK,并放入android您的路径。
  • 该脚本仅适用于 Unix。
于 2016-03-23T22:41:41.300 回答
4

在较新的 Android 版本(例如25.2.5)中,我们应该使用sdkmanager(而不是android命令)。

安装包示例:

android-sdk/tools/bin/sdkmanager "extras;android;m2repository"

获取所有可用软件包列表的命令:

android-sdk/tools/bin/sdkmanager --verbose --list

此网页列出了 SDK 工具的下载链接:

这是一个开源存储库docker-android的链接,它可以在 Docker 映像中安装 android。

您还可以在这个 SO 问题中找到答案:自动接受所有有用的SDK 许可证。

于 2017-03-17T11:20:44.473 回答
3

Android Plugin for Gradle版本 2.2.0 开始,缺少的 SDK 组件会自动下载

于 2016-10-04T13:57:02.037 回答
0

仅下载所需的非 {obsolete, source, emulator-image, doc} 包的脚本:

#!/bin/bash
set -e

# cd into where tools/android can be found
if [[ -d "$ANDROID_HOME" ]]; then
  cd "$ANDROID_HOME"
elif [[ -x "$(dirname "$0")/tools/android" ]]; then
  cd "$(dirname "$0")"
else
  echo "FAILED: Cannot find ANDROID_HOME/tools/android"
  exit 1
fi

android () {
  "$(dirname $0)/tools/android" "$@"
}

needed_packages () {
  android list sdk -u -s -e         \
    | grep '^id:'                   \
    | cut -d'"' -f2                 \
    | grep -v 'source'              \
    | grep -v 'sys-img'             \
    | grep -v 'doc'                 \
    | paste -d, -s -
}

main () {
  (while : ; do
  echo 'y'
  sleep 1
  done) | android update sdk -u -s -a -t "$(needed_packages)"
}

main

有些部分取自其他答案。

于 2016-06-19T07:51:51.563 回答
0

sdkmanager.bat --licenses在 Windows 上自动化提示(假设您正在通过自动化安装以构建基础架构)......不要运行它。不要浪费时间试图弄清楚如何y进入它。我试过; 惨败。

而是-您自己运行一次,并注意它会生成文件C:\android\android-sdk\licenses(您正在运行的位置C:\android\android-sdk\tools\bin\sdkmanager.bat-您的安装根目录可能会有所不同)。

获取这些文件,并将它们放置在您可以从自动设置脚本中获取它们的位置。就个人而言,Ansible是我的毒药,所以:

# Note to future-us:
# These are magical files generated by running `c:/android/android-sdk/tools/bin/sdkmanager.bat --licenses`
# This, delightfully, is interactive, and wants to _actually_ read the keyboard buffer.
# That's reputedly possible via SendKeys. I elected to not try that.
# So, instead:
# 1) remote to an instance like a cave-dweller
# 2) run `c:/android/android-sdk/tools/bin/sdkmanager.bat --licenses` in a prompt.
# 3) _actually type_ `y` however many godforsaken times you need to.
# 4) meticulously harvest `c:/android/android-sdk/licenses/*` to this task.
#    (you don't need the newline that they thoughtfully put before the hash in each file).
- name: set up android licenses by hand
  win_lineinfile:
    path: c:/android/android-sdk/licenses/{{ item.name }}
    line: "{{ item.line }}"
    create: true
  with_items:
    - {name: "android-googletv-license", line: "SOME HASH"}
    - {name: "android-sdk-license", line: "SOME OTHER HASH"}
    ...
于 2019-01-28T21:45:21.263 回答
0

我也对此感到沮丧,并构建了一个名为 Gradle 的插件com.quittle.setup-android-sdk,该插件将检测并安装您需要的内容。它适用于 Windows、OS X 和 Linux,如果您使用 Gradle 构建,则不需要任何额外的依赖项。

如果您有兴趣,可以在这里查看我的文档:https ://github.com/quittle/gradle-setup-android-sdk

于 2020-05-15T08:38:54.847 回答
-1

对于新手 Android 开发人员,但经验丰富的 Java 开发人员,即使您克服了之前答案中的所有噩梦,也很难知道哪些依赖项。

由于上述噩梦,我一位同事建议我使用 Android Studio(基于IntelliJ IDEA :-) 。

我听从了他的建议。

但我不接受安装的默认值,并尝试将其安装在我的软件驱动器中。原来是一场噩梦。SDK 对话似乎挂起,根本不直观。这就是我最终来到这里的原因。

看完上面的内容,我又给了 Studio 一次尝试,这次它接受了安装的所有默认值。

嘿 PRESTO ...它在几个对话中处理了所有 SDK 依赖项(我猜是核心依赖项),而没有提示,即Ctrl++ ShiftSSDK。

因此,我会向新手推荐它。这是下载时布丁的证明:

在此处输入图像描述

我下载并安装的 Studio 版本:

在此处输入图像描述

窗户版本:

在此处输入图像描述

在它做了好事之后:

在此处输入图像描述

于 2018-06-21T19:41:23.600 回答