使用命令行安装Android SDK
当前官网已经不提供单独的Android SDK
下载包,有两种方式来安装Android SDK
:一种就是界面安装方式,需要先下载Android Stutio
;另一种就是命令行安装方式,使用sdkmanager
来安装。
只需要Android SDK
的时候,就可以采用命令行安装的方式,本文将介绍如何使用命令行安装最新的Android SDK
。
前提条件
已经安装好
JDK
,并配置好JAVA_HOME
环境变量,通过在命令行输入java -version
确认,本文安装的JDK
为JDK 1.8.0_111
。
-
进入官网(点击这里),下载
commandlinetools
压缩包。
根据操作系统选择不同的包即可,如下图所示:
![](https://img.haomeiwen.com/i11416464/a6266a4d39724d53.png)
-
解压下载的
commandlinetools_XXX_latest.zip
,并拷贝解压后内容到ANDROID_HOME
指示的环境变量路径下。
ANDROID_HOME
是指示Android SDK
所在路径的一个环境变量,需要手动配置(环境变量的配置方法请自行搜索解决)。假定当前ANDROID_HOME
环境变量路径为~/android_sdk/
。
解压commandlinetools_XXX_latest.zip
后,将cmdline-tools
文件夹拷贝到~/android_sdk/
下,在cmdline-tools
文件夹下,新建目录latest
,并将cmdline-tools
目录下原有的内容(包括 lib
目录、bin
目录、NOTICE.txt
文件和 source.properties
文件)移动到新创建的latest
目录中,此时的目录结构如下:
> ls ~/android_sdk/cmdline-tools/latest
NOTICE.txt bin lib source.properties
此时SDK
包命令行管理工具sdkmanager
在~/android_sdk/cmdline-tools/latest/bin
下:
>ls ~/android_sdk/cmdline-tools/latest/bin
apkanalyzer avdmanager lint profgen retrace screenshot2 sdkmanager
-
SDK
包命令行管理工具sdkmanager
的使用
列出可安装的包
~/android_sdk/cmdline-tools/latest/bin/sdkmanager --list
可安装的包列表主要包含以下几类:
build-tools
:构建Android
应用所需包platform-tools
:平台工具包,诸如adb
等Android
的shell
命令platforms
:安卓API
system-images
:系统镜像,创建模拟器(AVD
)时需要tools
:包括uiautomatorviewer
界面元素定位工具
安装方法很简单,命令是:
~/android_sdk/cmdline-tools/latest/bin/sdkmanager
package
package就是使用sdkmanager --list
查询到的包全名,例如:
~/android_sdk/cmdline-tools/latest/bin/sdkmanager "build-tools;33.0.1"
~/android_sdk/cmdline-tools/latest/bin/sdkmanager "platforms;android-33"
建议上面介绍的包都下载一次,注意版本最好匹配。
-
AVD
工具avdmanager
的使用
avdmanager
是创建设备模拟器的工具,具体用法可以参考:
➜>~/android_sdk/cmdline-tools/latest/bin/avdmanager --help
Usage:
avdmanager [global options] [action] [action options]
Global options:
-s --silent : Silent mode, shows errors only.
-v --verbose : Verbose mode, shows errors, warnings and all messages.
--clear-cache: Clear the SDK Manager repository manifest cache.
-h --help : Help on a specific command.
Valid actions are composed of a verb and an optional direct object:
- list : Lists existing targets or virtual devices.
- list avd : Lists existing Android Virtual Devices.
- list target : Lists existing targets.
- list device : Lists existing devices.
- create avd : Creates a new Android Virtual Device.
- move avd : Moves or renames an Android Virtual Device.
- delete avd : Deletes an Android Virtual Device.
创建模拟器,主要是指定名称和镜像,可以通过avdmanager craete avd
查看创建需要的参数:
➜>~/android_sdk/cmdline-tools/latest/bin/avdmanager create avd
Usage:
avdmanager [global options] create avd [action options]
Global options:
-s --silent : Silent mode, shows errors only.
-v --verbose : Verbose mode, shows errors, warnings and all messages.
--clear-cache: Clear the SDK Manager repository manifest cache.
-h --help : Help on a specific command.
Action "create avd":
Creates a new Android Virtual Device.
Options:
-c --sdcard : Path to a shared SD card image, or size of a new sdcard for
the new AVD.
-g --tag : The sys-img tag to use for the AVD. The default is to
auto-select if the platform has only one tag for its system
images.
-p --path : Directory where the new AVD will be created.
-k --package : Package path of the system image for this AVD (e.g.
'system-images;android-19;google_apis;x86').
-n --name : Name of the new AVD. [required]
--skin : The optional name of a skin to use with this device.
-f --force : Forces creation (overwrites an existing AVD)
-b --abi : The ABI to use for the AVD. The default is to auto-select the
ABI if the platform has only one ABI for its system images.
-d --device : The optional device definition to use. Can be a device index
or id.
例如:
~/android_sdk/cmdline-tools/latest/bin/avdmanager create avd -n
Adnroid5-k
system-images;android-21;default;x86_64