我想从命令行设置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。


当前回答

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

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

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

要安装首选版本,请运行

sdkmanager "build-tools;27.0.3"

其他回答

正如在其他响应中所述,构建工具需要安装——all标志。你也最好使用一个-t过滤器标志来避免安装所有的包,但是没有办法过滤所有的构建工具。

在AOSP错误跟踪器中已经有针对这两点的特性请求。请随意投票给他们,这可能会在某一天实现:

https://code.google.com/p/android/issues/detail?id=78765 https://code.google.com/p/android/issues/detail?id=58337

Android SDK Tools 25.2.3版本(及更高版本)包含了新的工具sdkmanager,它简化了从命令行安装构建工具的任务。 它位于android_sdk/tools/bin文件夹中。

用法(来自文档):

List installed and available packages: sdkmanager --list [options] \ [--channel=channel_id] // Channels: 0 (stable), 1 (beta), 2 (dev), or 3 (canary) Use the channel option to include a package from a channel up to and including channel_id. For example, specify the canary channel to list packages from all channels. Install packages: sdkmanager packages [options] The packages argument is an SDK-style path, wrapped in quotes (for example, "build-tools;25.0.0" or "platforms;android-25"). You can pass multiple package paths, separated with a space, but they must each be wrapped in their own set of quotes.

示例用法(在我的Mac上):

alex@mbpro:~/sdk/tools/bin$ ls ../../build-tools/  
25.0.0/   
alex@mbpro:~/sdk/tools/bin$ ./sdkmanager "build-tools;25.0.2"  
done   
alex@mbpro:~/sdk/tools/bin$ ls ../../build-tools/  
25.0.0/ 25.0.2/

您还可以指定各种选项,例如强制所有连接使用HTTP(——no_https),或者为了使用代理服务器(——proxy_host=address和——proxy_port=port)。

要检查可用选项,使用——help标志。在我的机器(Mac)上,输出如下:

alex@mbpro:~/sdk/tools/bin$ ./sdkmanager --help
Usage: 
  sdkmanager [--uninstall] [<common args>] \
    [--package_file <package-file>] [<packages>...]
  sdkmanager --update [<common args>]
  sdkmanager --list [<common args>]

In its first form, installs, or uninstalls, or updates 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), currently installed packages are
    updated to the latest version.
In its third form, all installed and available packages are printed out.

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.

ADB Build-Tools不会自动下载,通过命令android update sdk——no-ui

因此安装build - tool类型(在控制台):

android list sdk --all

记住项目前面列出的数字,并执行以下操作:

android update sdk -u --all --filter <number>

命令应该在/YourFolder/android-sdk-linux/tools中输入

对于远程文件夹(例如ssh打开的服务器)键入:

**./android** list sdk --all
**./android** update sdk -u --all --filter <number>

在终端输入简单的ADB包列表:

android list sdk

安装所有软件包:

android update sdk --no-ui

或带过滤器(逗号为分隔符):

android update sdk --no-ui --filter 3,5,8,14

构建工具在默认情况下不能自动下载,就像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)

正如在其他回答中提到的,你可以使用——filter选项来限制安装的包:

Android SDK更新过滤器…

其他答案没有提到可以使用常量字符串标识符而不是用于筛选器选项的索引(索引将会更改)。这对于无人值守或脚本安装很有帮助。过滤选项:

... 这也接受'list sdk——extended'返回的标识符。

Android列表SDK——全部——扩展:

Packages available for installation or update: 97
----------
id: 1 or "tools"
     Type: Tool
     Desc: Android SDK Tools, revision 22.6.2
----------
id: 2 or "platform-tools"
     Type: PlatformTool
     Desc: Android SDK Platform-tools, revision 19.0.1
----------
id: 3 or "build-tools-19.0.3"
     Type: BuildTool
     Desc: Android SDK Build-tools, revision 19.0.3

然后你可以使用字符串id作为过滤器选项来精确指定你想要的版本:

Android更新SDK——过滤工具,平台工具,build-tools-19.0.3等