美文网首页
使用命令行安装Android SDK

使用命令行安装Android SDK

作者: Domibaba | 来源:发表于2023-07-12 21:04 被阅读0次

  当前官网已经不提供单独的Android SDK下载包,有两种方式来安装Android SDK:一种就是界面安装方式,需要先下载Android Stutio;另一种就是命令行安装方式,使用sdkmanager来安装。

  只需要Android SDK的时候,就可以采用命令行安装的方式,本文将介绍如何使用命令行安装最新的Android SDK

前提条件

已经安装好JDK,并配置好JAVA_HOME环境变量,通过在命令行输入java -version确认,本文安装的JDKJDK 1.8.0_111

  根据操作系统选择不同的包即可,如下图所示:


下载页面
  • 解压下载的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:平台工具包,诸如adbAndroidshell命令
  • 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

相关文章

网友评论

      本文标题:使用命令行安装Android SDK

      本文链接:https://www.haomeiwen.com/subject/xmpeudtx.html