用CLI安装Android SDK

在进行个人的Android开发环境准备时,通过Android Studio可以在一个漂亮的图形界面,安装、配置Android SDK。 但它也可以通过纯CLI的方式完成,有时反而更方便。

Bootstrap

在已经有tools组件后,自然就可以在CLI管理Android SDK的所有组件。 但是,在一无所有时,自然需要自举(Bootstrap),初始化一个tools组件。

wget https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip
unzip sdk-tools-linux-3859397.zip

解压后,在./tools/bin/sdkmanager位置的这个可执行文件,就是管理Android SDK的CLI工具。

以上是tools的26.0.1Linux版本,在预订要放置Android SDK的目录,下载后解压,即安装完成。 这个版本来源于最新的tools下载页面。 如果与本文的发布时间相隔甚远,可以通过访问这个页面,获取较新的下载链接。

当然,访问该页面需要自备墙梯。 下载却不用,因为域名是dl.google.com,可见俺们的墙也是很鸡贼的。 为了省事,这里把Mac和Windows的下载链接一并给出。 后面的介绍仍然以Linux为例。

Platform SDK tools package Link
Windows sdk-tools-windows-3859397.zip https://dl.google.com/android/repository/sdk-tools-windows-3859397.zip
Mac sdk-tools-darwin-3859397.zip https://dl.google.com/android/repository/sdk-tools-darwin-3859397.zip
Linux sdk-tools-linux-3859397.zip https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip

(不要担忧这个版本会过时,因为已经过时了。不过自举完成后,就可自我更新。)

Licenses

要使用Android SDK的相关组件前,需要接受5个用户协议。 懂法律的朋友可以仔细看一下。 看不懂,或者信任Google的朋友,为了避免麻烦,可以统一接受这5个协议。

yes | ./tools/bin/sdkmanager --licenses

更新sdkmanager

由于文档总是滞后于开发,所以刚下载的tools,往往是旧版本。 在《SDK Tools Release Notes | Android Studio》页面,可以看到最新发布的tools版本信息。

孤刚下载的是26.0.1,而最新已经是26.1.1了。 它对sdkmanager的界面显示做了一些优化。 26.0.1版本的--list对显示做了折叠,基本没法用,必须再加上--verbose参数才能得到完整的可用信息。 所以,这个更新还是很有必要的。 (它也会顺便安装emulatorpatcher;v4platform-tools三个组件。)

$ ./tools/bin/sdkmanager tools
done

可以通过--help,查看sdkmanager的帮助文档。 也可访问《sdkmanager | Android Studio》,在线查看。

列出SDK组件

在以前,都是使用./tools/android来做SDK管理,但近期已被废弃。 比如,列出SDK的组件。

$ ./tools/android list sdk
*************************************************************************
The "android" command is deprecated.
For manual SDK, AVD, and project management, please use Android Studio.
For command-line tools, use tools/bin/sdkmanager and tools/bin/avdmanager
*************************************************************************
"android" SDK commands can be translated to sdkmanager commands on a best-effort basis.
Continue? (This prompt can be suppressed with the --use-sdk-wrapper command-line argument
or by setting the USE_SDK_WRAPPER environment variable) [y/N]:

现在使用./tools/bin/sdkmanager --list,来列出已安装和可安装的组件。 sdkmanager替代了原./tools/android的SDK管理功能。 (avdmanager替代了虚拟设备的管理功能。)

sdkmanager可以按版本查看、安装、卸载Android的组件。 以下列出当前(2018年3月)的可用组件:

Path Version Description
add-ons;addon-google_apis-google-15 3 Google APIs
add-ons;addon-google_apis-google-16 4 Google APIs
add-ons;addon-google_apis-google-17 4 Google APIs
add-ons;addon-google_apis-google-18 4 Google APIs
add-ons;addon-google_apis-google-19 20 Google APIs
add-ons;addon-google_apis-google-21 1 Google APIs
add-ons;addon-google_apis-google-22 1 Google APIs
add-ons;addon-google_apis-google-23 1 Google APIs
add-ons;addon-google_apis-google-24 1 Google APIs
add-ons;addon-google_gdk-google-19 11 Glass Development Kit Preview
build-tools;19.1.0 19.1.0 Android SDK Build-Tools 19.1
build-tools;20.0.0 20.0.0 Android SDK Build-Tools 20
build-tools;21.1.2 21.1.2 Android SDK Build-Tools 21.1.2
build-tools;22.0.1 22.0.1 Android SDK Build-Tools 22.0.1
build-tools;23.0.1 23.0.1 Android SDK Build-Tools 23.0.1
build-tools;23.0.2 23.0.2 Android SDK Build-Tools 23.0.2
build-tools;23.0.3 23.0.3 Android SDK Build-Tools 23.0.3
build-tools;24.0.0 24.0.0 Android SDK Build-Tools 24
build-tools;24.0.1 24.0.1 Android SDK Build-Tools 24.0.1
build-tools;24.0.2 24.0.2 Android SDK Build-Tools 24.0.2
build-tools;24.0.3 24.0.3 Android SDK Build-Tools 24.0.3
build-tools;25.0.0 25.0.0 Android SDK Build-Tools 25
build-tools;25.0.1 25.0.1 Android SDK Build-Tools 25.0.1
build-tools;25.0.2 25.0.2 Android SDK Build-Tools 25.0.2
build-tools;25.0.3 25.0.3 Android SDK Build-Tools 25.0.3
build-tools;26.0.0 26.0.0 Android SDK Build-Tools 26
build-tools;26.0.1 26.0.1 Android SDK Build-Tools 26.0.1
build-tools;26.0.2 26.0.2 Android SDK Build-Tools 26.0.2
build-tools;26.0.3 26.0.3 Android SDK Build-Tools 26.0.3
build-tools;27.0.0 27.0.0 Android SDK Build-Tools 27
build-tools;27.0.1 27.0.1 Android SDK Build-Tools 27.0.1
build-tools;27.0.2 27.0.2 Android SDK Build-Tools 27.0.2
build-tools;27.0.3 27.0.3 Android SDK Build-Tools 27.0.3
cmake;3.6.4111459 3.6.4111459 CMake 3.6.4111459
docs 1 Documentation for Android SDK
emulator 27.1.12 Android Emulator
extras;android;gapid;1 1.0.3 GPU Debugging tools
extras;android;gapid;3 3.1.0 GPU Debugging tools
extras;android;m2repository 47.0.0 Android Support Repository
extras;google;auto 1.1 Android Auto Desktop Head Unit emulator
extras;google;google_play_services 46 Google Play services
extras;google;instantapps 1.1.0 Instant Apps Development SDK
extras;google;m2repository 58 Google Repository
extras;google;market_apk_expansion 1 Google Play APK Expansion library
extras;google;market_licensing 1 Google Play Licensing Library
extras;google;simulators 1 Android Auto API Simulators
extras;google;webdriver 2 Google Web Driver
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0 1 Solver for ConstraintLayout 1.0.0
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha2 1 com.android.support.constraint:constraint-layout-solver:1.0.0-alpha
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha3 1 com.android.support.constraint:constraint-layout-solver:1.0.0-alpha
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha4 1 com.android.support.constraint:constraint-layout-solver:1.0.0-alpha
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha5 1 Solver for ConstraintLayout 1.0.0-alpha5
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha6 1 Solver for ConstraintLayout 1.0.0-alpha6
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha7 1 Solver for ConstraintLayout 1.0.0-alpha7
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha8 1 Solver for ConstraintLayout 1.0.0-alpha8
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-alpha9 1 Solver for ConstraintLayout 1.0.0-alpha9
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta1 1 Solver for ConstraintLayout 1.0.0-beta1
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta2 1 Solver for ConstraintLayout 1.0.0-beta2
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta3 1 Solver for ConstraintLayout 1.0.0-beta3
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta4 1 Solver for ConstraintLayout 1.0.0-beta4
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.0-beta5 1 Solver for ConstraintLayout 1.0.0-beta5
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.1 1 Solver for ConstraintLayout 1.0.1
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.2 1 Solver for ConstraintLayout 1.0.2
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0 1 ConstraintLayout for Android 1.0.0
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha2 1 com.android.support.constraint:constraint-layout:1.0.0-alpha2
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha3 1 com.android.support.constraint:constraint-layout:1.0.0-alpha3
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha4 1 com.android.support.constraint:constraint-layout:1.0.0-alpha4
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha5 1 ConstraintLayout for Android 1.0.0-alpha5
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha6 1 ConstraintLayout for Android 1.0.0-alpha6
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha7 1 ConstraintLayout for Android 1.0.0-alpha7
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha8 1 ConstraintLayout for Android 1.0.0-alpha8
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-alpha9 1 ConstraintLayout for Android 1.0.0-alpha9
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta1 1 ConstraintLayout for Android 1.0.0-beta1
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta2 1 ConstraintLayout for Android 1.0.0-beta2
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta3 1 ConstraintLayout for Android 1.0.0-beta3
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta4 1 ConstraintLayout for Android 1.0.0-beta4
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.0-beta5 1 ConstraintLayout for Android 1.0.0-beta5
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.1 1 ConstraintLayout for Android 1.0.1
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.2 1 ConstraintLayout for Android 1.0.2
lldb;2.0 2.0.2558144 LLDB 2.0
lldb;2.1 2.1.2852477 LLDB 2.1
lldb;2.2 2.2.3271982 LLDB 2.2
lldb;2.3 2.3.3614996 LLDB 2.3
lldb;3.0 3.0.4213617 LLDB 3.0
ndk-bundle 16.1.4479499 NDK
patcher;v4 1 SDK Patch Applier v4
platform-tools 27.0.1 Android SDK Platform-Tools
platforms;android-10 2 Android SDK Platform 10
platforms;android-11 2 Android SDK Platform 11
platforms;android-12 3 Android SDK Platform 12
platforms;android-13 1 Android SDK Platform 13
platforms;android-14 4 Android SDK Platform 14
platforms;android-15 5 Android SDK Platform 15
platforms;android-16 5 Android SDK Platform 16
platforms;android-17 3 Android SDK Platform 17
platforms;android-18 3 Android SDK Platform 18
platforms;android-19 4 Android SDK Platform 19
platforms;android-20 2 Android SDK Platform 20
platforms;android-21 2 Android SDK Platform 21
platforms;android-22 2 Android SDK Platform 22
platforms;android-23 3 Android SDK Platform 23
platforms;android-24 2 Android SDK Platform 24
platforms;android-25 3 Android SDK Platform 25
platforms;android-26 2 Android SDK Platform 26
platforms;android-27 1 Android SDK Platform 27
platforms;android-7 3 Android SDK Platform 7
platforms;android-8 3 Android SDK Platform 8
platforms;android-9 2 Android SDK Platform 9
sources;android-15 2 Sources for Android 15
sources;android-16 2 Sources for Android 16
sources;android-17 1 Sources for Android 17
sources;android-18 1 Sources for Android 18
sources;android-19 2 Sources for Android 19
sources;android-20 1 Sources for Android 20
sources;android-21 1 Sources for Android 21
sources;android-22 1 Sources for Android 22
sources;android-23 1 Sources for Android 23
sources;android-24 1 Sources for Android 24
sources;android-25 1 Sources for Android 25
sources;android-26 1 Sources for Android 26
sources;android-27 1 Sources for Android 27
system-images;android-10;default;armeabi-v7a 4 ARM EABI v7a System Image
system-images;android-10;default;x86 4 Intel x86 Atom System Image
system-images;android-10;google_apis;armeabi-v7a 5 Google APIs ARM EABI v7a System Image
system-images;android-10;google_apis;x86 5 Google APIs Intel x86 Atom System Image
system-images;android-14;default;armeabi-v7a 2 ARM EABI v7a System Image
system-images;android-15;default;armeabi-v7a 4 ARM EABI v7a System Image
system-images;android-15;default;mips 1 MIPS System Image
system-images;android-15;default;x86 4 Intel x86 Atom System Image
system-images;android-15;google_apis;armeabi-v7a 5 Google APIs ARM EABI v7a System Image
system-images;android-15;google_apis;x86 5 Google APIs Intel x86 Atom System Image
system-images;android-16;default;armeabi-v7a 4 ARM EABI v7a System Image
system-images;android-16;default;mips 1 MIPS System Image
system-images;android-16;default;x86 5 Intel x86 Atom System Image
system-images;android-16;google_apis;x86 5 Google APIs Intel x86 Atom System Image
system-images;android-17;default;armeabi-v7a 5 ARM EABI v7a System Image
system-images;android-17;default;mips 1 MIPS System Image
system-images;android-17;default;x86 3 Intel x86 Atom System Image
system-images;android-17;google_apis;armeabi-v7a 5 Google APIs ARM EABI v7a System Image
system-images;android-17;google_apis;x86 5 Google APIs Intel x86 Atom System Image
system-images;android-18;default;armeabi-v7a 4 ARM EABI v7a System Image
system-images;android-18;default;x86 3 Intel x86 Atom System Image
system-images;android-18;google_apis;armeabi-v7a 5 Google APIs ARM EABI v7a System Image
system-images;android-18;google_apis;x86 5 Google APIs Intel x86 Atom System Image
system-images;android-19;default;armeabi-v7a 5 ARM EABI v7a System Image
system-images;android-19;default;x86 6 Intel x86 Atom System Image
system-images;android-19;google_apis;armeabi-v7a 33 Google APIs ARM EABI v7a System Image
system-images;android-19;google_apis;x86 33 Google APIs Intel x86 Atom System Image
system-images;android-21;android-tv;armeabi-v7a 3 Android TV ARM EABI v7a System Image
system-images;android-21;android-tv;x86 3 Android TV Intel x86 Atom System Image
system-images;android-21;default;armeabi-v7a 4 ARM EABI v7a System Image
system-images;android-21;default;x86 5 Intel x86 Atom System Image
system-images;android-21;default;x86_64 5 Intel x86 Atom_64 System Image
system-images;android-21;google_apis;armeabi-v7a 25 Google APIs ARM EABI v7a System Image
system-images;android-21;google_apis;x86 25 Google APIs Intel x86 Atom System Image
system-images;android-21;google_apis;x86_64 25 Google APIs Intel x86 Atom_64 System Image
system-images;android-22;android-tv;armeabi-v7a 1 Android TV ARM EABI v7a System Image
system-images;android-22;android-tv;x86 3 Android TV Intel x86 Atom System Image
system-images;android-22;default;armeabi-v7a 2 ARM EABI v7a System Image
system-images;android-22;default;x86 6 Intel x86 Atom System Image
system-images;android-22;default;x86_64 6 Intel x86 Atom_64 System Image
system-images;android-22;google_apis;armeabi-v7a 19 Google APIs ARM EABI v7a System Image
system-images;android-22;google_apis;x86 19 Google APIs Intel x86 Atom System Image
system-images;android-22;google_apis;x86_64 19 Google APIs Intel x86 Atom_64 System Image
system-images;android-23;android-tv;armeabi-v7a 12 Android TV ARM EABI v7a System Image
system-images;android-23;android-tv;x86 14 Android TV Intel x86 Atom System Image
system-images;android-23;android-wear;armeabi-v7a 6 Android Wear ARM EABI v7a System Image
system-images;android-23;android-wear;x86 6 Android Wear Intel x86 Atom System Image
system-images;android-23;default;x86 10 Intel x86 Atom System Image
system-images;android-23;default;x86_64 10 Intel x86 Atom_64 System Image
system-images;android-23;google_apis;armeabi-v7a 26 Google APIs ARM EABI v7a System Image
system-images;android-23;google_apis;x86 26 Google APIs Intel x86 Atom System Image
system-images;android-23;google_apis;x86_64 26 Google APIs Intel x86 Atom_64 System Image
system-images;android-24;android-tv;x86 15 Android TV Intel x86 Atom System Image
system-images;android-24;default;arm64-v8a 7 ARM 64 v8a System Image
system-images;android-24;default;armeabi-v7a 7 ARM EABI v7a System Image
system-images;android-24;default;x86 8 Intel x86 Atom System Image
system-images;android-24;default;x86_64 8 Intel x86 Atom_64 System Image
system-images;android-24;google_apis;arm64-v8a 20 Google APIs ARM 64 v8a System Image
system-images;android-24;google_apis;armeabi-v7a 20 Google APIs ARM EABI v7a System Image
system-images;android-24;google_apis;x86 20 Google APIs Intel x86 Atom System Image
system-images;android-24;google_apis;x86_64 20 Google APIs Intel x86 Atom_64 System Image
system-images;android-24;google_apis_playstore;x86 19 Google Play Intel x86 Atom System Image
system-images;android-25;android-tv;x86 9 Android TV Intel x86 Atom System Image
system-images;android-25;android-wear-cn;armeabi-v7a 4 China version of Android Wear ARM EABI v7a System Image
system-images;android-25;android-wear-cn;x86 4 China version of Android Wear Intel x86 Atom System Image
system-images;android-25;android-wear;armeabi-v7a 3 Android Wear ARM EABI v7a System Image
system-images;android-25;android-wear;x86 3 Android Wear Intel x86 Atom System Image
system-images;android-25;google_apis;arm64-v8a 11 Google APIs ARM 64 v8a System Image
system-images;android-25;google_apis;armeabi-v7a 11 Google APIs ARM EABI v7a System Image
system-images;android-25;google_apis;x86 11 Google APIs Intel x86 Atom System Image
system-images;android-25;google_apis;x86_64 11 Google APIs Intel x86 Atom_64 System Image
system-images;android-25;google_apis_playstore;x86 9 Google Play Intel x86 Atom System Image
system-images;android-26;android-tv;x86 7 Android TV Intel x86 Atom System Image
system-images;android-26;android-wear-cn;x86 4 China version of Android Wear Intel x86 Atom System Image
system-images;android-26;android-wear;x86 4 Android Wear Intel x86 Atom System Image
system-images;android-26;google_apis;x86 8 Google APIs Intel x86 Atom System Image
system-images;android-26;google_apis;x86_64 8 Google APIs Intel x86 Atom_64 System Image
system-images;android-26;google_apis_playstore;x86 7 Google Play Intel x86 Atom System Image
system-images;android-27;android-tv;x86 2 Android TV Intel x86 Atom System Image
system-images;android-27;google_apis;x86 4 Google APIs Intel x86 Atom System Image
system-images;android-27;google_apis_playstore;x86 3 Google Play Intel x86 Atom System Image
tools 26.1.1 Android SDK Tools

以上表格为命令行显示整理后的结果,分三列。 第二列是版本,第三列是描述,和UI上的安卓管理器的显示类似。

第一列Path,含义是组件名称,可能还会加上版本,以分号;隔开。 这其实也是本地安装路径,只要把分号;看作/。 把Path的内容直接放到./tools/bin/sdkmanager后,就可安装这个组件。

安装组件

直接在sdkmanager后,加上SDK组件的Path,就可以安装,一次一个。 但是注意,由于这里的Path愚蠢地以分号;来作为分隔符,所以需要加上单引号''或双引号"",否则Shell会识别有误。 以下以build-tools为例:

$ ./tools/bin/sdkmanager build-tools;27.0.3
Warning: Failed to find package build-tools
bash: 27.0.3: command not found
$ ./tools/bin/sdkmanager 'build-tools;27.0.3'
[=======================================] 100% Unzipping... android-8.1.0/jack-j

26.1.1版本的CLI显示,仍然有很多需要忍受的问题。)

卸载组件

如果不是硬盘不够,通常是不需要卸载的。 版本敏感的SDK组件,都已经分版本放置了,多版本可以共存。 所以SDK只需要不断更新即可。

但如果确实要卸载,可以用--uninstall参数。

$ ./tools/bin/sdkmanager --uninstall 'build-tools;27.0.3'
[=======================================] 100% Fetch remote repository...

(忍……)

更新组件

有些组件是统一用一个版本的,比如toolsplatform-tools。 有些组件本身是对应某个Android版本、甚至架构的,比如platforms;android-27system-images;android-27;google_apis_playstore;x86。 如果要统一更新所有已安装组件,可以用--update参数。

$ ./tools/bin/sdkmanager --update
[=======================================] 100% Computing updates...

(继续忍……)

总结

Android SDK组件繁多,又有必要和可选之分。 有这样一个CLI的方式进行管理,在进行大规模或重复性环境准备时,是很方便的。

就是这CLI的品质……


相关笔记