应用场景
虽然 Android Studio 给我们提供了可视化页面来管理 SDK,但还是会有在命令行管理 SDK 的需求,比如在远程管理 Jenkins 打包服务器上的 SDK
历史
在 SDK tools 25.2.3 版本以前 SDK 管理命令是 android,在 tools 目录下,这时候是和其它功能共用 android 命令
从 25.2.3 开始就提供了 sdkmanager 命令专门来管理 SDK
由于 android 命令已经是过去式,这里就不再阐述了,给出以下参考链接
参考:
sdkmanager
sdkmanager 命令专门用来管理 SDK,位于 tools/bin/ 目录下,官方虽然提供了使用文档,但是不够详细,一些特殊的场景没有给出解决办法
参考:
列出所有已安装、可安装、可更新的包
$ sdkmanager --list
结果如下(做了简化,仅供参考)
Installed packages:
Path | Version | Description | Location
------- | ------- | ------- | -------
add-ons;addon-g..._apis-google-22 | 1.0.0 | Google APIs, Android 22 | add-ons/addon-g...apis-google-22/
build-tools;25.0.2 | 25.0.2 | Android SDK Build-Tools 25.0.2 | build-tools/25.0.2/
docs | 1 | Documentation for Android SDK | docs/
emulator | 26.0.0 | Android Emulator | emulator/
extras;android;m2repository | 47.0.0 | Android Support Repository | extras/android/m2repository/
extras;google;m2repository | 55 | Google Repository | extras/google/m2repository/
extras;m2reposi...ut-solver;1.0.2 | 1 | Solver for ConstraintLayout 1.0.2 | extras/m2reposi...t-solver/1.0.2/
extras;m2reposi...nt-layout;1.0.2 | 1 | ConstraintLayout for Android 1... | extras/m2reposi...t-layout/1.0.2/
patcher;v1 | 1 | SDK Patch Applier v1 | patcher/v1/
patcher;v4 | 1 | SDK Patch Applier v4 | patcher/v4/
platform-tools | 25.0.5 | Android SDK Platform-Tools | platform-tools/
platforms;android-25 | 3 | Android SDK Platform 25, rev 3 | platforms/android-25/
sources;android-25 | 1 | Sources for Android 25 | sources/android-25/
system-images;a...gle_apis;x86_64 | 4 | Google APIs Intel x86 Atom_64 ... | system-images/a...le_apis/x86_64/
tools | 26.0.2 | Android SDK Tools | tools/
Available Packages:
Path | Version | Description
------- | ------- | -------
add-ons;addon-g..._apis-google-24 | 1 | Google APIs
add-ons;addon-g...e_gdk-google-19 | 11 | Glass Development Kit Preview
build-tools;26.0.0 | 26.0.0 | Android SDK Build-Tools 26
cmake;3.6.4111459 | 3.6.4111459 | CMake 3.6.4111459
docs | 1 | Documentation for Android SDK
emulator | 26.0.3 | 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...
extras;google;g...e_play_services | 42 | Google Play services
extras;google;instantapps | 1.0.0 | Instant Apps Development SDK
extras;google;m2repository | 55 | Google Repository
extras;google;m...t_apk_expansion | 1 | Google Play APK Expansion library
extras;google;market_licensing | 1 | Google Play Licensing Library
extras;google;play_billing | 5 | Google Play Billing Library
extras;google;simulators | 1 | Android Auto API Simulators
extras;google;webdriver | 2 | Google Web Driver
extras;intel;Ha...ecution_Manager | 6.1.1 | Intel x86 Emulator Accelerator...
extras;m2reposi...ut-solver;1.0.0 | 1 | Solver for ConstraintLayout 1.0.0
extras;m2reposi...er;1.0.0-alpha4 | 1 | com.android.support.constraint...
extras;m2reposi...ut-solver;1.0.2 | 1 | Solver for ConstraintLayout 1.0.2
extras;m2reposi...nt-layout;1.0.0 | 1 | ConstraintLayout for Android 1...
extras;m2reposi...out;1.0.0-beta5 | 1 | ConstraintLayout for Android 1...
extras;m2reposi...nt-layout;1.0.2 | 1 | ConstraintLayout for Android 1...
lldb;2.3 | 2.3.3614996 | LLDB 2.3
ndk-bundle | 15.1.4119039 | NDK
patcher;v4 | 1 | SDK Patch Applier v4
platform-tools | 26.0.0 | Android SDK Platform-Tools
platforms;android-26 | 1 | Android SDK Platform 26
sources;android-25 | 1 | Sources for Android 25
system-images;a...s_playstore;x86 | 4 | Google Play Intel x86 Atom Sys...
tools | 26.0.2 | Android SDK Tools
Available Updates:
ID | Installed | Available
------- | ------- | -------
emulator | 26.0.0 | 26.0.3
platform-tools | 25.0.5 | 26.0.0
system-images;android-25;google_apis;x86_64 | 4 | 6
done
安装
$ sdkmanager "platforms;android-26"
双引号里面的就是Path那一列显示的值,同理要安装其他的包就使用对应的Path
更新
$ sdkmanager "system-images;android-25;google_apis;x86_64"
更新和安装是一样的写法,sdkmanager 会自动下载最新版替换已安装的版本
一次更新全部可更新的包
$ sdkmanager --update
删除
$ sdkmanager --uninstall "sources;android-25"
同意许可
我们在使用可视化页面安装或更新的时候会有一个让我们接受许可的步骤,在命令行也是有的,只需输入"y"确认即可
你还可以通过 --licnses 参数一次查看全部可接受的许可并输入"y"接受,如下:
$ sdkmanager --licenses
6 of 6 SDK package licenses not accepted.
Review licenses that have not been accepted (y/N)?
有多少个许可就需要输入多少次"y"
有一种特殊的情况,当我们将这些命令写在一个脚本文件里去执行的时候许可会被自动拒绝,那么我们怎么才能在脚本里需要许可的时候自动接受呢,如下:
$ yes | sdkmanager "platforms;android-26"
--licnses 参数可以使用
$ yes | sdkmanager --licenses
Path无效
细看上面 --list 的输出结果就能发现,有一些 Path 由于过长,输出的时候中间用 "..." 给替代了,这样 的Path 是无法使用的,这时候需要加上 --verbose 参数输出完整的 Path
$ sdkmanager --list --verbose
结果如下(做了简化,仅供参考):
Available Packages:
--------------------------------------
add-ons;addon-google_apis-google-24
Description: Google APIs
Version: 1
add-ons;addon-google_gdk-google-19
Description: Glass Development Kit Preview
Version: 11
build-tools;26.0.0
Description: Android SDK Build-Tools 26
Version: 26.0.0
extras;m2repository;com;android;support;constraint;constraint-layout;1.0.2
Description: ConstraintLayout for Android 1.0.2
Version: 1
Dependencies:
extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.2
sources;android-25
Description: Sources for Android 25
Version: 1
system-images;android-26;google_apis_playstore;x86
Description: Google Play Intel x86 Atom System Image
Version: 4
Dependencies:
patcher;v4
tools
Description: Android SDK Tools
Version: 26.0.2
Dependencies:
patcher;v4
emulator
platform-tools Revision 20
Available Updates:
--------------------------------------
emulator
Local Version: 26.0.0
Remote Version: 26.0.3
system-images;android-25;google_apis;x86_64
Local Version: 4
Remote Version: 6
done
使用代理
早些时候 SDK 里的包都存在dl-ssl.google.com 上,国内无法访问,需要配置代理访问国内的镜像
现如今 SDK 里的包都存在 dl.google.com 上,国内可以直接访问
但可能还是会有需要代理的需求,如下配置即可:
$ sdkmanager --list --proxy-host mirrors.neusoft.edu.cn --proxy-port 80 -s
网友评论