我想从命令行设置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。
灵感来自@i4niac和@Aurélien Lambert的回答,这是我想到的
csv_update_numbers=$(./android list sdk --all | grep 'Android SDK Build-tools' | grep -v 'Obsolete' | sed 's/\(.*\)\- A.*/\1/'|sed '/^$/d'|sed -e 's/^[ \t]*//'| tr '\n' ',')
csv_update_numbers_without_trailing_comma=${csv_update_numbers%?}
( sleep 5 && while [ 1 ]; do sleep 1; echo y; done ) \
| ./android update sdk --all -u -t $csv_update_numbers_without_trailing_comma
解释
在android list sdk——all命令的结果中获得一个以逗号分隔的数字列表,这些数字是构建工具包的索引(忽略过时的包)。
保持每隔几毫秒向终端抛出'y'以接受许可证。
构建工具在默认情况下不能自动下载,就像Nate在https://stackoverflow.com/a/19416222/1104031的帖子中说的那样。
但是我写了一个小工具,为你做一切
我在https://stackoverflow.com/a/17863931/1104031的帖子中使用了“期望”工具作为danb。
你只需要android-sdk和python27。
这个脚本将安装所有的构建工具,所有的sdk和自动构建所需的一切:
import subprocess,re,sys
w = subprocess.check_output(["android", "list", "sdk", "--all"])
lines = w.split("\n")
tools = filter(lambda x: "Build-tools" in x, lines)
filters = []
for tool in tools:
m = re.search("^\s+([0-9]+)-", tool)
tool_no = m.group(1)
filters.append(tool_no)
if len(filters) == 0:
raise Exception("Not found build tools")
filters.extend(['extra', 'platform', 'platform-tool', 'tool'])
filter = ",".join(filters)
expect= '''set timeout -1;
spawn android update sdk --no-ui --all --filter %s;
expect {
"Do you accept the license" { exp_send "y\\r" ; exp_continue }
eof
}''' % (filter)
print expect
ret = subprocess.call(["expect", "-c", expect])
sys.exit(ret)
大多数答案似乎忽略了这样一个事实,即您可能需要在没有超级用户权限的无头环境中运行更新,这意味着脚本必须自动回答所有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),如果你感兴趣,可以在这里查看更多细节。