我想从命令行设置Android开发环境,遇到以下问题:

wget http://dl.google.com/android/android-sdk_r22.0.5-linux.tgz

解压完成后,运行

tools/android update sdk --no-ui

但是,它跑起来太慢了

Fetching https://dl-ssl.google.com/android/repository/addons_list-2.xml

结果是在文件夹build-tools中什么都没有,我想要的是apapt和apkbuilder,因为我想从命令行构建apk而没有ant。


当前回答

当我试图从命令行安装所有Android SDK相关的东西时,我遇到了一个很棒的信息来源,就是这个Dockerfile。在Dockerfile中,您可以看到作者执行了一个命令来安装平台工具和构建工具,而没有任何其他交互。在行动计划提出的情况下,该命令将被调整为:

echo y | $ANDROID_HOME/tools/android update sdk --all --filter build-tools-21.1.0 --no-ui

其他回答

默认情况下,命令行中的SDK管理器不包括列表中的构建工具。它们属于“过时”的类别。要查看所有可用的下载,使用

android list sdk --all

然后从命令行获取列表中的一个包,使用:

android update sdk -u -a -t <package no.>

其中-u代表——no-ui, -a代表——all, -t代表——filter。

如果你需要安装多个包,请:

android update sdk -u -a -t 1,2,3,4,..,n 

在1、2、…,n为上面list命令列出的包号

我就遇到了这个问题,所以我最终通过读取和解析可用工具列表来编写了一行bash dirty解决方案:

 tools/android update sdk -u -t $(android list sdk | grep 'Android SDK Build-tools' | sed 's/ *\([0-9]\+\)\-.*/\1/')

如果你安装了sdkmanager(我使用MAC)

执行sdkmanager——list命令列出可用的包。

如果要安装构建工具,请从可用软件包列表中复制首选版本。

要安装首选版本,请运行

sdkmanager "build-tools;27.0.3"

大多数答案似乎忽略了这样一个事实,即您可能需要在没有超级用户权限的无头环境中运行更新,这意味着脚本必须自动回答所有y/n许可提示。

下面是一个例子。

FILTER=tool,platform,android-20,build-tools-20.0.0,android-19,android-19.0.1

( sleep 5 && while [ 1 ]; do sleep 1; echo y; done ) \
    | android update sdk --no-ui --all \
    --filter ${FILTER}

No matter how many prompts you get, all of those will be answered. This while/sleep loop looks like simulation of the yes command, and in fact it is, well almost. The problem with yes is that it floods stdout with 'y' and there is virtually no delay between sending those characters and the version I had to deal with had no timeout option of any kind. It will "pollute" stdout and the script will fail complaining about incorrect input. The solution is to put a delay between sending 'y' to stdout, and that's exactly what while/sleep combo does.

expect在一些linux发行版上默认是不可用的,我没有办法将它作为我的CI脚本的一部分安装,所以必须使用最通用的解决方案,没有什么比简单的bash脚本更通用的了,对吗?

事实上,我在博客上写过(NSBogan),如果你感兴趣,可以在这里查看更多细节。

我更喜欢放一个脚本来安装我的依赖

喜欢的东西:

#!/usr/bin/env bash
#
# Install JUST the required dependencies for the project.
# May be used for ci or other team members.
#

for I in android-25 \
         build-tools-25.0.2  \
         tool \
         extra-android-m2repository \
         extra-android-support \
         extra-google-google_play_services \
         extra-google-m2repository;

 do echo y | android update sdk --no-ui --all --filter $I ; done

https://github.com/caipivara/android-scripts/blob/master/install-android-dependencies.sh