简体   繁体   English

如何在命令行上安装 Android SDK Build Tools?

[英]How to install Android SDK Build Tools on the command line?

I want to setup the Android dev environment from command line, and encounter the following issue:我想从命令行设置Android开发环境,遇到以下问题:

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

after extract the file, run解压文件后,运行

tools/android update sdk --no-ui

However, it is too slow on running但是,运行速度太慢

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

The result is that nothing in folder build-tools, and I want is aapt and apkbuilder, since I want to build apk from command line without ant.结果是文件夹 build-tools 中没有任何内容,我想要的是 aapt 和 apkbuilder,因为我想在没有 ant 的情况下从命令行构建 apk。

By default, the SDK Manager from the command line does not include the build tools in the list.默认情况下,命令行中的 SDK 管理器不包括列表中的构建工具。 They're in the "obsolete" category.它们属于“过时”类别。 To see all available downloads, use要查看所有可用的下载,请使用

android list sdk --all

And then to get one of the packages in that list from the command line, use:然后要从命令行获取该列表中的包之一,请使用:

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

Where -u stands for --no-ui, -a stands for --all and -t stands for --filter.其中-u 代表--no-ui,-a 代表--all,-t 代表--filter。

If you need to install multiple packages do:如果您需要安装多个软件包,请执行以下操作:

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

Where 1,2,..,n is the package number listed with the list command above其中 1,2,..,n 是上面 list 命令列出的包号

As mentioned in other answers, you can use the --filter option to limit the installed packages:正如其他答案中提到的,您可以使用--filter选项来限制已安装的软件包:

android update sdk --filter ...

The other answers don't mention that you can use constant string identifiers instead of indexes (which will change) for the filter options.其他答案没有提到您可以为过滤器选项使用常量字符串标识符而不是索引(这将改变)。 This is helpful for unattended or scripted installs.这对于无人值守或脚本安装很有帮助。 Man for --filter option:使用--filter选项的人:

... This also accepts the identifiers returned by 'list sdk --extended'. ...这也接受'list sdk --extended'返回的标识符。

android list sdk --all --extended : android list sdk --all --extended

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

Then you can use the string ids as the filter options to precisely specify the versions you want:然后您可以使用字符串 ids 作为过滤器选项来精确指定您想要的版本:

android update sdk --filter tools,platform-tools,build-tools-19.0.3 etc android update sdk --filter tools,platform-tools,build-tools-19.0.3

Version 25.2.3 (and higher) of Android SDK Tools package contains new tool - sdkmanager - which simplifies this task of installing build-tools from the command line.版本 25.2.3(及更高版本)的Android SDK Tools包包含新工具 - sdkmanager - 简化了从命令行安装构建工具的任务。
It is located in android_sdk/tools/bin folder.它位于android_sdk/tools/bin文件夹中。

Usage (from documentation ):用法(来自文档):

 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" ). packages参数是一个 SDK 样式的路径,用引号括起来(例如, "build-tools;25.0.0""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.您可以传递多个包路径,用空格分隔,但每个路径都必须包含在自己的一组引号中。

Example usage (on my Mac):示例用法(在我的 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/

You can also specify various options , for example to force all connections to use HTTP ( --no_https ), or in order to use proxy server ( --proxy_host=address and --proxy_port=port ).您还可以指定各种选项,例如强制所有连接使用 HTTP ( --no_https ),或者为了使用代理服务器 ( --proxy_host=address--proxy_port=port )。

To check the available options, use the --help flag.要检查可用选项,请使用--help标志。 On my machine (Mac), the output is as following:在我的机器(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 Will Not be downloaded automatically, by command android update sdk --no-ui ADB Build-Tools 不会自动下载,通过命令 android update sdk --no-ui

So for installing Buil-Tool type (in console):因此,对于安装 Buil-Tool 类型(在控制台中):

android list sdk --all

Remember the number that is listed before the item and execute the following:记住项目之前列出的数字并执行以下操作:

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

commands should be typed in /YourFolder/android-sdk-linux/tools命令应该在 /YourFolder/android-sdk-linux/tools 中输入

Also for remote folder (server opened by ssh for example) type:同样对于远程文件夹(例如由 ssh 打开的服务器)键入:

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

For simple list of ADB packages type in terminal:对于终端中的 ADB 包类型的简单列表:

android list sdk

for install all packages:安装所有软件包:

android update sdk --no-ui

Or with filters (comma is separator):或使用过滤器(逗号是分隔符):

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

A great source of information I came across while trying to install everything Android SDK related from the command line, was this Dockerfile.我在尝试从命令行安装与 Android SDK 相关的所有内容时遇到的一个重要信息来源是这个Dockerfile。 Inside the Dockerfile you can see that the author executes a single command to install platform tools and build tools without any other interaction.在 Dockerfile 中可以看到作者执行一个命令来安装platform toolsbuild tools没有任何其他交互。 In the case the OP has put forth, the command would be adapted to:在 OP 提出的情况下,该命令将适用于:

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

I just had a heck of a time getting android sdk dependencies installed via command line and since the documentation that comes with the tools and online are woefully lacking, I thought I'd post what I discovered here.我只是有一段时间通过命令行安装 android sdk 依赖项,并且由于工具和在线随附的文档非常缺乏,我想我会在这里发布我发现的内容。

I'm working with android sdk r24.4.1 for linux .我正在使用android sdk r24.4.1 for linux There are two commands that you can run to list the available packages:您可以运行两个命令来列出可用的包:

android list sdk

and the more exhaustive:以及更详尽的:

android list sdk --all

The package numbers for specific packages differ for each command above!上面每个命令的特定包的包号都不同! For example, the former lists package API 23.1 revision 3 as package #3 and the latter lists it as #29 .例如,前者将包API 23.1 revision 3列为包#3 ,后者将其列为#29

Now, there are two different ways to install using the android command.现在,有两种不同的方法可以使用 android 命令进行安装。

tools/android update sdk --no-ui --filter <package number>

and

tools/android update sdk -u -a -t <package number>

Given that the install commands each can take the package # as a parameter, which package number do you use?鉴于每个安装命令都可以将包 # 作为参数,您使用哪个包号? After much online searching and trial and error, I discovered that经过大量的在线搜索和反复试验,我发现

android update sdk --no-ui --filter uses the package numbers from android list sdk android update sdk --no-ui --filter使用android list sdk的包号

and

android update sdk -u -a -t uses the package numbers from android list sdk --all android update sdk -u -a -t使用android list sdk --all的包号

In other words - to install API 23.1 revision 3 you can do either:换句话说 - 要安装API 23.1 revision 3您可以执行以下任一操作:

android update sdk --no-ui --filter 3

or要么

android update sdk -u -a -t 29

Crazy, but it works.疯狂,但它有效。

If you have sdkmanager installed (I'm using MAC)如果您安装了sdkmanager (我使用的是 MAC)

run sdkmanager --list to list available packages.运行sdkmanager --list以列出可用的包。

If you want to install build tools, copy the preferred version from the list of packages available.如果要安装构建工具,请从可用软件包列表中复制首选版本。

To install the preferred version run要安装首选版本,请运行

sdkmanager "build-tools;27.0.3"

Most of the answers seem to ignore the fact that you may need to run the update in a headless environment with no super user rights, which means the script has to answer all the y/n license prompts automatically.大多数答案似乎忽略了一个事实,即您可能需要在没有超级用户权限的无头环境中运行更新,这意味着脚本必须自动回答所有y/n许可证提示。

Here's the example that does the trick.这是一个可以解决问题的例子。

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 .这个while/sleep循环看起来像yes命令的模拟,实际上它几乎是. 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. yes的问题在于它用'y'淹没了标准输出,并且在发送这些字符和我必须处理的版本之间几乎没有延迟,没有任何类型的超时选项。 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.解决方案是在将'y'发送到标准输出之间设置延迟,而这正是while/sleep组合所做的。

expect is not available by default on some linux distros and I had no way to install it as part of my CI scripts, so had to use the most generic solution and nothing can be more generic than simple bash script, right?在某些 linux 发行版上, expect在默认情况下不可用,我无法将它安装为我的 CI 脚本的一部分,所以不得不使用最通用的解决方案,没有什么比简单的 bash 脚本更通用的了,对吧?

As a matter of fact, I blogged about it ( NSBogan ), check it out for more details here if you are interested.事实上,我写了一篇关于它的博客( NSBogan ),如果你有兴趣,请在此处查看更多详细信息。

The "android" command is deprecated.不推荐使用“android”命令。

For command-line tools, use tools/bin/sdkmanager and tools/bin/avdmanager对于命令行工具,请使用tools/bin/sdkmanagertools/bin/avdmanager

If you do not need Android Studio, you can download the basic Android command line tools from developer.android.com in section Command line tools only .如果您不需要 Android Studio,您可以从developer.android.com 的仅命令行工具部分下载基本的 Android 命令行工具。

from CLI it should be somfing like:从 CLI 来看,它应该是这样的:

curl --output sdk-tools-linux.zip https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip

or要么

wget --output-document sdk-tools-linux.zip https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip

After that just unpack the archive to the target folder之后只需将存档解压缩到目标文件夹

unzip sdk-tools-linux.zip

And now we can install everything you need...现在我们可以安装您需要的一切...

./tools/bin/sdkmanager --install 'build-tools;29.0.2' 'platform-tools' 'platforms;android-29' 'tools'

You can get a complete list of packages using the command ./tools/bin/sdkmanager --list您可以使用命令./tools/bin/sdkmanager --list获取完整的软件包列表

Some packages require acceptance of the license agreement.某些软件包需要接受许可协议。 you can accept it interactively or just pass "y" to the input stream, like this(two agreements in case):您可以以交互方式接受它,也可以将“y”传递给输入流,如下所示(以防万一):

echo -ne "y\ny" | ./tools/bin/sdkmanager --install 'system-images;android-29;default;x86_64'

And of course, for your convenience, you can export variables such as ANDROID_HOME or ANDROID_SDK_ROOT (including doing it in ~/.profile or ~/.bash_profile) or patch the PATH variable - all this is at your discretion.当然,为了您的方便,您可以导出ANDROID_HOMEANDROID_SDK_ROOT等变量(包括在 ~/.profile 或 ~/.bash_profile 中执行)或修补PATH变量 - 所有这些都由您自行决定。

Script example:脚本示例:

mkdir /opt/android-sdk
cd /opt/android-sdk
curl --output sdk-tools-linux.zip https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
unzip sdk-tools-linux.zip
echo -ne "y" | ./tools/bin/sdkmanager --install 'build-tools;29.0.2' 'platform-tools' 'platforms;android-29' 'tools'

Requirements: curl(or wget) and unzip要求: curl(或 wget)和解压

Troubleshooting:故障排除:

if you see Warning: Could not create settings , you need to have the tools directory inside the cmdline-tools directory inside the ANDROID_HOME (create it if needed with this exact name) see Android Command line tools sdkmanager always shows: Warning: Could not create settings如果你看到Warning: Could not create settings ,你需要有tools的目录里面cmdline-tools的ANDROID_HOME(如果需要使用此确切名称创建)目录里面看到Android的命令行工具始终sdkmanager显示:警告:无法创建设置

However, it is too slow on running但是,运行速度太慢

Yes, I've had the same problem.是的,我遇到了同样的问题。 Some of the file downloads are extremely slow (or at least they have been in the last couple of days).某些文件下载速度非常慢(或者至少在过去几天内如此)。 If you want to download everything there's not a lot you can do about that.如果您想下载所有内容,那么您无能为力。

The result is that nothing in folder build-tools, and I want is aapt and apkbuilder, since I want to build apk from command line without ant.结果是文件夹 build-tools 中没有任何内容,我想要的是 aapt 和 apkbuilder,因为我想在没有 ant 的情况下从命令行构建 apk。

Did you let it run to completion?你让它运行完成了吗?

One thing you can do is filter the packages that are being downloaded using the -t switch.您可以做的一件事是使用-t开关过滤正在下载的软件包。

For example:例如:

tools/android update sdk --no-ui -t platform-tool

When I tried this the other day I got version 18.0.0 of the build tools installed.当我前几天尝试这个时,我安装了 18.0.0 版的构建工具。 For some reason the latest version 18.0.1 is not included by this filter and the only way to get it was to install everything with the --all switch.出于某种原因,此过滤器不包含最新版本 18.0.1,获得它的唯一方法是使用--all开关安装所有内容。

I prefer to put a script that install my dependencies我更喜欢放置一个脚本来安装我的依赖项

Something like:就像是:

#!/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 https://github.com/caipivara/android-scripts/blob/master/install-android-dependencies.sh

我刚刚遇到了这个问题,所以我最终通过阅读和解析可用工具列表编写了一个 1 行的 bash 脏解决方案:

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

Inspired from answers by @i4niac & @Aurélien Lambert, this is what i came up with灵感来自@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

Explanation解释

  • get a comma separated list of numbers which are the indexes of build tools packages in the result of android list sdk --all command (Ignoring obsolete packages).android list sdk --all命令(忽略过时的包)的结果中,获取一个逗号分隔的数字列表,这些数字是构建工具包的索引。
  • keep throwing 'y's at the terminal every few miliseconds to accept the licenses.每隔几毫秒继续在终端上扔“y”以接受许可证。

Download android SDK from developer.android.com (its currently a 149mb file for windows OS).从 developer.android.com 下载 android SDK(它目前是一个 149mb 的 Windows 操作系统文件)。 It is worthy of note that android has removed the sdkmanager GUI but has a command line version of the sdkmanager in the bin folder which is located inside the tools folder.值得注意的是,android 已经删除了 sdkmanager GUI,但在位于工具文件夹内的 bin 文件夹中有一个命令行版本的 sdkmanager。

  1. When inside the bin folder, hold down the shift key, right click, then select open command line here.在 bin 文件夹中,按住 shift 键,右键单击,然后选择在此处打开命令行。 Shift+right click >> open command line here. Shift+右键单击>>在此处打开命令行。
  2. When the command line opens, type sdkmanager click enter.当命令行打开时,输入sdkmanager点击回车。
  3. Then run type sdkmanager (space), double hyphen ( -- ), type list sdkmanager --list (this lists all the packages in the SDK manager)然后运行 ​​type sdkmanager (space), double hyphen ( -- ), type list sdkmanager --list (这会列出 SDK 管理器中的所有包)
  4. Type sdkmanager (space) then package name, press enter.输入 sdkmanager(空格),然后输入包名,按回车。 Eg.例如。 sdkmanager platform-tools (press enter) It will load licence agreement. sdkmanager platform-tools(按回车键)它将加载许可协议。 With options (y/n).带选项(是/否)。 Enter y to accept and it will download the package you specified.输入 y 接受,它将下载您指定的包。

For more reference follow official document here有关更多参考,请按照此处的官方文档进行操作

I hope this helps.我希望这有帮助。 :) :)

Build tools could not be downloaded automatically by default as Nate said in https://stackoverflow.com/a/19416222/1104031 post.正如 Nate 在https://stackoverflow.com/a/19416222/1104031帖子中所说,默认情况下无法自动下载构建工具。

But I wrote small tool that make everything for you但我写了一个小工具,为你做一切

I used "expect" tool as danb in https://stackoverflow.com/a/17863931/1104031 post.我在https://stackoverflow.com/a/17863931/1104031帖子中使用“期望”工具作为 danb。 You only need android-sdk and python27 , expect .你只需要 android-sdk 和python27expect

This script will install all build tools, all sdks and everything you need for automated build:此脚本将安装所有构建工具、所有 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)

As stated in other responses, the build tools requires the --all flag to be installed.正如其他回复中所述,构建工具需要安装--all标志。 You also better use a -t filter flag to avoid installing ALL the packages but there is no way to filter all the build tools.您还最好使用-t过滤器标志来避免安装所有软件包,但无法过滤所有构建工具。

There are already features requests for these two points in AOSP bug tracker. AOSP 错误跟踪器中已经有针对这两点的功能请求。 Feel free to vote for them, this might make them happen some day:随意投票给他们,这可能会让他们有一天发生:

我试过这个来更新所有,它奏效了!

echo y | $ANDROID_HOME/tools/android update sdk --no-ui

Try尝试

1. List all packages 1.列出所有包

android list sdk --all

2. Install packages using following command 2.使用以下命令安装软件包

android update sdk -u -a -t package1, package2, package3 //comma seperated packages obtained using list command 

To setup android environment without installating the whole android studio :要在不安装整个 android studio 的情况下设置 android 环境:

  1. Download JDK (version greater than 8)下载JDK(版本大于8)
  2. Download gradle from https://gradle.org/install/https://gradle.org/install/下载 gradle
  3. Download command line tools from https://developer.android.com/studio scroll down and download command line tools onlyhttps://developer.android.com/studio下载命令行工具向下滚动并仅下载命令行工具
  4. Setup the necessary environment variables设置必要的环境变量
  5. Download the necessary SDK tools下载必要的 SDK 工具

android update sdk安卓更新sdk

This command will update and install all latest release for SDK Tools, Build Tools,SDK platform tools.此命令将更新并安装 SDK 工具、构建工具、SDK 平台工具的所有最新版本。

It's Work for me.这是为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM