简体   繁体   English

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

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

I am installing android SDK to create an automated build server.我正在安装 android SDK 来创建一个自动构建服务器。 I got into a problem where many Gradle-based Android projects I manage rely on different build tools version.我遇到了一个问题,我管理的许多基于 Gradle 的 Android 项目依赖于不同的构建工具版本。 Some of the projects still requiring old build-tools version (eg v19.1.0).一些项目仍然需要旧的构建工具版本(例如 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.0 but it didn't work.我试图调用android update sdk -u --filter build-tools-19.1.0但它没有用。 Is there an easy way to install old version of Android build tools from command line?有没有一种简单的方法可以从命令行安装旧版本的 Android 构建工具?

Note: installing via GUI is not possible注意:无法通过 GUI 安装

Assuming you have downloaded the Android SDK for linux, you have two options to update from command line.假设您已经下载了适用于 linux 的 Android SDK,您有两个选项可以从命令行进行更新。

Download using android tool 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.除了使用 GUI 运行 android sdk 管理器之外,您还可以选择在无头模式下运行。 You just need to specify -u (or --no-ui ) flag.您只需要指定-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 Android SDK build tools revision 23.0.1, I would type in:示例:如果我想安装 Android 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.由于在执行android sdk list时并未列出每个包(尤其是旧包),因此您也可以手动下载内容。 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 文件 - https://dl.google.com/android/repository/repository-11.xml

You can find there links to various types of things to download, eg:您可以在那里找到指向要下载的各种类型内容的链接,例如:

To manually download eg 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 aapt you 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 aapt you 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.在运行aapt ,如果您在 GLIBC 2.14 中遇到错误并且您不会升级您的语言环境 GLIBC。 Then u need to download the following packages for sdk (build-tool, platform-tool) :然后你需要为 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

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.您可以使用 grep 命令缩小搜索范围。 For example:例如:

[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 

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.就像添加 Gradle 插件一样简单。 Any required SDK packages will be installed on demand to SDK location specified in local.properties .任何必需的 SDK 包都将按需安装到local.properties指定的 SDK 位置。

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.这种方法非常有效,尤其是在无头 CI 环境中。 Just don't forget to add ANDROID_HOME environment variable just in case you don't have local.properties already (which most likely happens on CI environment)只是不要忘记添加ANDROID_HOME环境变量,以防万一您还没有local.properties (这很可能发生在 CI 环境中)

Based on: How to install Android SDK Build Tools on the command line?基于: 如何在命令行安装Android SDK Build Tools? and others here is my version:其他人是我的版本:

#!/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\"/\1/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 $?
}

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

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