How to install old version of Android build tools

2019-01-30 09:01发布

问题:

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).

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.

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

Note: installing via GUI is not possible

回答1:

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

Download using android tool from the 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.

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:

$ ./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

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

  • ADK
    • http://dl.google.com/android/ADT-XX.0.0.zip
  • SDK Tools
    • http://dl.google.com/android/repository/tools_rXX-linux_x86.zip
    • http://dl.google.com/android/repository/tools_rXX-windows.zip
    • http://dl.google.com/android/repository/tools_rXX-macosx.zip
  • Platform Tools
    • http://dl.google.com/android/repository/platform-tools_rXX-linux_x86.zip
    • http://dl.google.com/android/repository/platform-tools_rXX-windows.zip
    • http://dl.google.com/android/repository/platform-tools_rXX-macosx.zip
  • Build Tools
    • http://dl.google.com/android/repository/build-tools_rXX-linux.zip
    • http://dl.google.com/android/repository/build-tools_rXX-windows.zip
    • http://dl.google.com/android/repository/build-tools_rXX-macosx.zip
  • Platforms
    • http://dl.google.com/android/repository/android-XX.zip

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

$ 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):

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

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:

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) :

build-tool : 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



回答2:

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

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:

[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 


回答3:

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.

It's as simple as adding Gradle plugin. Any required SDK packages will be installed on demand to SDK location specified in 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_HOME environment variable just in case you don't have local.properties already (which most likely happens on CI environment)



回答4:

Based on: How to install Android SDK Build Tools on the command line? 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 $?
}