如何从命令行安装旧版本的 Android 构建工具?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26016770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:56:15  来源:igfitidea点击:

How to install old version of Android build tools from command line?

androidandroid-sdk-toolsbuild-tools

提问by akhy

I am installing android SDK to create an automated build server. I got into a problem where many Gradle-based Android projects I manage rely on different build tools version. Some of the projects still requiring old build-tools version (e.g. v19.1.0).

我正在安装 android SDK 来创建一个自动构建服务器。我遇到了一个问题,我管理的许多基于 Gradle 的 Android 项目依赖于不同的构建工具版本。一些项目仍然需要旧的构建工具版本(例如 v19.1.0)。

My android-sdk package was downloaded when build-tools version 20 has just released, so that's the only version available to download via android update sdk -u --filter build-tools.

我的 android-sdk 软件包是在 build-tools 版本 20 刚刚发布时下载的,因此这是唯一可通过android update sdk -u --filter build-tools.

I have tried to invoke android update sdk -u --filter build-tools-19.1.0but it didn't work. Is there an easy way to install old version of Android build tools from command line?

我试图调用,android update sdk -u --filter build-tools-19.1.0但没有奏效。有没有一种简单的方法可以从命令行安装旧版本的 Android 构建工具?

Note: installing via GUI is not possible

注意:无法通过 GUI 安装

回答by wtekiela

Assuming you have downloaded the Android SDK for linux, you have two options to update from command line.

假设您已经下载了适用于 linux 的 Android SDK,您有两个选项可以从命令行进行更新。

Download using androidtool from the sdk

使用android工具从 sdk下载

Instead of running the android sdk manager with a GUI, you also have an option to run in headless mode. You just need to specify -u(or --no-ui) flag.

除了使用 GUI 运行 android sdk 管理器之外,您还可以选择在无头模式下运行。您只需要指定-u(或--no-ui)标志。

To list packages available for installation or upgrade:

列出可用于安装或升级的软件包:

$ cd android-sdk-linux
$ ./tools/android list sdk --all
Refresh Sources:
  (...)
  Fetching URL: https://dl.google.com/android/repository/repository-11.xml
  (...)
Packages available for installation or update: 166
   1- Android SDK Tools, revision 24.4.1
   2- Android SDK Tools, revision 25.0.9 rc10
   3- Android SDK Platform-tools, revision 23.1
   4- Android SDK Platform-tools, revision 24 rc1
   5- Android SDK Build-tools, revision 24 rc1
   6- Android SDK Build-tools, revision 23.0.2
   7- Android SDK Build-tools, revision 23.0.1
   8- Android SDK Build-tools, revision 23 (Obsolete)
  (...)
 162- Google Play Billing Library, revision 5
 163- Google Play Licensing Library, revision 2
 164- Android Auto API Simulators, revision 1
 165- Google Web Driver, revision 2
 166- Intel x86 Emulator Accelerator (HAXM installer), revision 6.0.1

To download specific packages you need to specify the number of the item you want to install from the list in the following command:

要下载特定软件包,您需要在以下命令的列表中指定要安装的项目编号:

$ ./tools/android update sdk -u -a -t [NO_OF_ITEM_TO_BE_INSTALLED]

Example: if I wanted to install andorid sdk build tools revision 23.0.1, I would type in:

示例:如果我想安装 andorid sdk 构建工具修订版 23.0.1,我会输入:

$ ./tools/android update sdk -u -a -t 7

Manual download

手动下载

As not every package (especially the old ones) is listed when you do android sdk list, you can also download things manually. You can manually open the XML file that is shown when during android sdk list- https://dl.google.com/android/repository/repository-11.xml

由于并非每个包(尤其是旧包)在您执行时都列出android sdk list,您也可以手动下载内容。您可以手动打开显示的 XML 文件android sdk list- https://dl.google.com/android/repository/repository-11.xml

You can find there links to various types of things to download, e.g:

您可以在那里找到指向要下载的各种类型内容的链接,例如:

To manually download e.g. version 19.0.1 of build tools

手动下载例如版本 19.0.1 的构建工具

$ cd android-sdk-linux/build-tools
$ wget http://dl.google.com/android/repository/build-tools_r19.0.1-linux.zip
$ unzip build-tools_r19.0.1-linux.zip
$ mv android-4.4.2 19.0.1
$ rm build-tools_r19.0.1-linux.zip

Side note (ubuntu):

旁注(ubuntu):

If you're running debian/ubuntu 64-bit, to run aaptyou will need to install additional packages:

如果您运行的是 debian/ubuntu 64 位,要运行aapt您需要安装其他软件包:

sudo apt-get install lib32stdc++6
sudo apt-get install lib32z1

If you're running CentOs (RedHat), to run aaptyou will need to install additional packages:

如果你正在运行 CentOs (RedHat),aapt你需要安装额外的软件包才能运行:

sudo yum install zlib.i686 libstd++.i686

While running aapt, if you get an error with GLIBC 2.14 and you dont wont to upgrade your locale GLIBC. Then u need to download the following packages for sdk (build-tool, platform-tool) :

在运行时aapt,如果 GLIBC 2.14 出现错误并且您不会升级您的语言环境 GLIBC。然后你需要为 sdk (build-tool, platform-tool) 下载以下包:

build-tool : http://dl.google.com/android/repository/build-tools_r23.0.2-linux.zip

构建工具:http: //dl.google.com/android/repository/build-tools_r23.0.2-linux.zip

platform-tool : https://dl.google.com/android/repository/platform-tools_r23.0.1-linux.zip

平台工具:https: //dl.google.com/android/repository/platform-tools_r23.0.1-linux.zip

回答by rgulia

What you want is to be able to obtain the same functionality of the SDK Manager GUI in a command line.

您想要的是能够在命令行中获得与 SDK Manager GUI 相同的功能。

Issue this command to query all available packages from the repository. The query will return the packages with a index number on the left.

发出此命令以从存储库中查询所有可用包。查询将返回带有左侧索引号的包。

[rgulia@xinu ~]$ android list sdk --all 
Refresh Sources:
  Fetching https://dl-ssl.google.com/android/repository/addons_list-2.xml
  Validate XML
  Parse XML
  Fetched Add-ons List successfully
  …
  Validate XML: https://dl-ssl.google.com/android/repository/sys-img/x86/addon-x86.xml
  Parse XML:    https://dl-ssl.google.com/android/repository/sys-img/x86/addon-x86.xml
  Packages available for installation or update: 138
  1- Android SDK Tools, revision 24.1.2
  2- Android SDK Platform-tools, revision 22
  3- Android SDK Build-tools, revision 22.0.1
  4- Android SDK Build-tools, revision 22 (Obsolete)
  5- Android SDK Build-tools, revision 21.1.2
  ….

You can use a grep command to narrow your search. For example:

您可以使用 grep 命令缩小搜索范围。例如:

[rgulia@xinu ~]$ android list sdk --all | grep -i tools | grep 21
   5- Android SDK Build-tools, revision 21.1.2
   6- Android SDK Build-tools, revision 21.1.1 (Obsolete)
   7- Android SDK Build-tools, revision 21.1 (Obsolete)
   8- Android SDK Build-tools, revision 21.0.2 (Obsolete)
   9- Android SDK Build-tools, revision 21.0.1 (Obsolete)
  10- Android SDK Build-tools, revision 21 (Obsolete)

Finally, install the desired package by supplying its index number in this command.

最后,通过在此命令中提供其索引号来安装所需的包。

[rgulia@xinu ~]$ android update sdk -u -a -t 5

The options have this meaning:

这些选项具有以下含义:

-u (--no-ui)  # Headless mode
-a (--all)    # Includes all packages, included the obsolete ones
-t (--filter) # in this example we have filtered by package index, i.e. 5 

回答by akhy

Answering my own question, I have just found that one of the best way to achieve my goal is by using SDK manager Gradle plugin by Jake Wharton.

回答我自己的问题,我刚刚发现实现我的目标的最佳方法之一是使用Jake Wharton 的 SDK 管理器 Gradle 插件

It's as simple as adding Gradle plugin. Any required SDK packages will be installed on demand to SDK location specified in local.properties.

就像添加 Gradle 插件一样简单。任何需要的 SDK 包都将按需安装到 中指定的 SDK 位置local.properties

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:0.12.+'
    classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+'
  }
}

apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.application'

This approach works great especially in headless CI environment. Just don't forget to add ANDROID_HOMEenvironment variable just in case you don't have local.propertiesalready (which most likely happens on CI environment)

这种方法非常有效,尤其是在无头 CI 环境中。只是不要忘记添加ANDROID_HOME环境变量,以防万一你还没有local.properties(这很可能发生在 CI 环境中)

回答by uthomas

Based on: How to install Android SDK Build Tools on the command line?and others here is my version:

基于:如何在命令行安装Android SDK Build Tools?其他人是我的版本:

#!/bin/bash -e

installAndroidSdk() {
    local url=http://dl.google.com/android/android-sdk_r24.3-linux.tgz

    sudo apt-get install -y lib32stdc++6 lib32z1

    # Install android SDK
    (
        cd /opt

        curl -o android.tgz -sL $url

        tar xzf android.tgz

        mv android-sdk-linux/ android

        rm android.tgz

        echo "export ANDROID_HOME=/opt/android" > /etc/profile.d/android.sh
        echo "export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools" >> /etc/profile.d/android.sh
        source /etc/profile.d/android.sh

        android list sdk --all --extended > /tmp/android-skds

        modules=( platform-tools build-tools-22.0.1 android-22 )
        for module in ${modules[@]}; do
            moduleId=$(less /tmp/android-skds | sed -n "s/id: \(.*\) or \"$module\"//p")
            if [[ ! -z "$moduleId" ]]; then
                expect -c "
                    set timeout -1   ;
                    spawn android update sdk -u -a -t $moduleId;
                    expect { 
                        \"Do you accept the license\" { exp_send \"y\r\" ; exp_continue }
                        eof
                    }
                "
            else
                echo "[WARNING] - $module was not installed."
            fi
        done

        echo "export PATH=$PATH:$ANDROID_HOME/build-tools/22.0.1" | sudo tee -a /etc/profile.d/android.sh

        rm -f /tmp/android-skds
        sudo apt-get install -y android-tools-adb
    )
    return $?
}