命令行下载更新Android SDK

皮安顺
2023-12-01

前序

最近需要在服务器上用Jenkins自动打包Android app,从google官网上下载的Linux版本sdk结果发现里面就只有一个tools目录有文件,其他的都没有。。。
无奈,服务器是没有界面的,之前都习惯用IDE去安装更新,现在尝试用命令行下载更新了。

下载Android SDK for Linux

从google的官网下载最新Linux版本SDK,由于dl.google.com域名一直没有被墙,所以才可以直接从官网下了。这点不错~

$ wget https://dl.google.com/android/android-sdk_r24.4.1-linux.tgz

解压
$ tar zxvf android-sdk_r24.4.1-linux.tgz

更新Android SDK

前面说到了,我们下载的这个包其实只有tools目录下才有东西。既然google给了我们这个,表示这里面肯定有可以更新SDK的工具啦。
其实就是tools/android这个文件

$ cd android-sdk-linux/tools

查看命令帮助
$ ./android --help
       Usage:
       android [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:
-    sdk              : Displays the SDK Manager window.
-    avd              : Displays the AVD Manager window.
-   list              : Lists existing targets or virtual devices.
-   list avd          : Lists existing Android Virtual Devices.
-   list target       : Lists existing targets.
-   list device       : Lists existing devices.
-   list sdk          : Lists remote SDK repository.
- create avd          : Creates a new Android Virtual Device.
-   move avd          : Moves or renames an Android Virtual Device.
- delete avd          : Deletes an Android Virtual Device.
- update avd          : Updates an Android Virtual Device to match the folders
                        of a new SDK.
- create project      : Creates a new Android project.
- update project      : Updates an Android project (must already have an
                        AndroidManifest.xml).
- create test-project : Creates a new Android project for a test package.
- update test-project : Updates the Android project for a test package (must
                        already have an AndroidManifest.xml).
- create lib-project  : Creates a new Android library project.
- update lib-project  : Updates an Android library project (must already have
                        an AndroidManifest.xml).
- create uitest-project: Creates a new UI test project.
- update adb          : Updates adb to support the USB devices declared in the
                        SDK add-ons.
- update sdk          : Updates the SDK by suggesting new platforms to install
                        if available.

我们需要关注 list 和 update 。

查看当前可安装的SDK版本
$ ./android list sdk

Refresh Sources:
  Fetching https://dl.google.com/android/repository/addons_list-2.xml
  Validate XML
  Parse XML
  Fetched Add-ons List successfully
  Refresh Sources
  Fetching URL: https://dl.google.com/android/repository/repository-11.xml
  Validate XML: https://dl.google.com/android/repository/repository-11.xml
  Parse XML:    https://dl.google.com/android/repository/repository-11.xml
  Fetching URL: https://dl.google.com/android/repository/addon.xml
  Validate XML: https://dl.google.com/android/repository/addon.xml
  Parse XML:    https://dl.google.com/android/repository/addon.xml
  Fetching URL: https://dl.google.com/android/repository/glass/addon.xml
  Validate XML: https://dl.google.com/android/repository/glass/addon.xml
  Parse XML:    https://dl.google.com/android/repository/glass/addon.xml
  Fetching URL: https://dl.google.com/android/repository/extras/intel/addon.xml
  Validate XML: https://dl.google.com/android/repository/extras/intel/addon.xml
  Parse XML:    https://dl.google.com/android/repository/extras/intel/addon.xml
  Fetching URL: https://dl.google.com/android/repository/sys-img/android/sys-img.xml
  Validate XML: https://dl.google.com/android/repository/sys-img/android/sys-img.xml
  Parse XML:    https://dl.google.com/android/repository/sys-img/android/sys-img.xml
  Fetching URL: https://dl.google.com/android/repository/sys-img/android-wear/sys-img.xml
  Validate XML: https://dl.google.com/android/repository/sys-img/android-wear/sys-img.xml
  Parse XML:    https://dl.google.com/android/repository/sys-img/android-wear/sys-img.xml
  Fetching URL: https://dl.google.com/android/repository/sys-img/android-tv/sys-img.xml
  Validate XML: https://dl.google.com/android/repository/sys-img/android-tv/sys-img.xml
  Parse XML:    https://dl.google.com/android/repository/sys-img/android-tv/sys-img.xml
  Fetching URL: https://dl.google.com/android/repository/sys-img/google_apis/sys-img.xml
  Validate XML: https://dl.google.com/android/repository/sys-img/google_apis/sys-img.xml
  Parse XML:    https://dl.google.com/android/repository/sys-img/google_apis/sys-img.xml
Packages available for installation or update: 41
   1- Android SDK Tools, revision 25.2.2
   2- Android SDK Platform-tools, revision 24.0.4
   3- Android SDK Build-tools, revision 24.0.3
   4- Documentation for Android SDK, API 24, revision 1
   5- SDK Platform Android 7.0, API 24, revision 2
   6- SDK Platform Android 6.0, API 23, revision 3
   7- SDK Platform Android 5.1.1, API 22, revision 2
   8- SDK Platform Android 5.0.1, API 21, revision 2
   9- SDK Platform Android 4.4W.2, API 20, revision 2
  10- SDK Platform Android 4.4.2, API 19, revision 4
  11- SDK Platform Android 4.3.1, API 18, revision 3
  12- SDK Platform Android 4.2.2, API 17, revision 3
  13- SDK Platform Android 4.1.2, API 16, revision 5
  14- SDK Platform Android 4.0.3, API 15, revision 5
  15- SDK Platform Android 4.0, API 14, revision 4
  16- SDK Platform Android 3.2, API 13, revision 1
  17- SDK Platform Android 3.1, API 12, revision 3
  18- SDK Platform Android 3.0, API 11, revision 2
  19- SDK Platform Android 2.3.3, API 10, revision 2
  20- SDK Platform Android 2.3.1, API 9, revision 2
  21- SDK Platform Android 2.2, API 8, revision 3
  22- SDK Platform Android 2.1, API 7, revision 3
  23- Google APIs, Android API 24, revision 1
  24- Google APIs, Android API 23, revision 1
  25- Google APIs, Android API 22, revision 1
  26- Google APIs, Android API 21, revision 1
  27- Google APIs, Android API 19, revision 20
  28- Glass Development Kit Preview, Android API 19, revision 11
  29- Google APIs, Android API 18, revision 4
  30- Google APIs, Android API 17, revision 4
  31- Google APIs, Android API 16, revision 4
  32- Google APIs, Android API 15, revision 3
  33- Android Support Repository, revision 38
  34- Android Auto Desktop Head Unit emulator, revision 1.1
  35- Google Play services, revision 33
  36- Google Repository, revision 36
  37- Google Play APK Expansion library, revision 1
  38- Google Play Licensing Library, revision 1
  39- Google Play Billing Library, revision 5
  40- Android Auto API Simulators, revision 1
  41- Google Web Driver, revision 2

因为是首次安装,所有有非常多的版本可下载。
我们可以有2个选择:

  • 安装所有版本的SDK
  • 只安装我们需要SDK版本
    因为是在服务器上,建议直接安装所有版本的吧,不然后续可能有些app需要这个版本,又有的需要那个版本。还不如一开始就全部安装好。

更新命令

根据上面的工具使用帮助,可以看到更新命令是使用update sdk
但是这个命令又是如何使用的呢?查看帮助

$ ./android update sdk --help

Usage:
       android [global options] update sdk [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 "update sdk":
  Updates the SDK by suggesting new platforms to install if available.
Options:
  -f --force     : Forces replacement of a package or its parts, even if
                   something has been modified.
  -n --dry-mode  : Simulates the update but does not download or install
                   anything.
     --proxy-host: HTTP/HTTPS proxy host (overrides settings if defined)
  -s --no-https  : Uses HTTP instead of HTTPS (the default) for downloads.
  -t --filter    : A filter that limits the update to the specified types of
                   packages in the form of a comma-separated list of
                   [platform, system-image, tool, platform-tool, doc, sample,
                   source]. This also accepts the identifiers returned by
                   'list sdk --extended'.
  -u --no-ui     : Updates from command-line (does not display the GUI)
     --proxy-port: HTTP/HTTPS proxy port (overrides settings if defined)
  -p --obsolete  : Deprecated. Please use --all instead.
  -a --all       : Includes all packages (such as obsolete and non-dependent
                   ones.)

在这里,重点关注-u-t参数就好。

-u --no-ui  表示在命令行环境下使用,刚好符合我们服务器环境
-t --filter 表示过滤,只安装指定版本的sdk 

好了,我们现在就开始更新吧

首先下载更新全部可安装的SDK版本,不指定过滤即可
$ ./android update sdk -u

经过漫长时间的等待基本都安装好了。。
如果只需要安装指定版本的话,就需要过滤了,如何过滤呢?我们上面已经介绍查看可安装更新sdk版本命令list sdk了。根据查看到的序号过滤即可。

只安装指定序号的版本
$ ./android  update sdk -u -t 序号
如:安装Build-tools, revision 24.0.3
$ ./android update sdk -u -t 3
需要同意license,输入 y 回车即可

安装后可跳转到上一级目录查看是否已经有了。

$ cd ..
$ ls
add-ons  build-tools  platforms  SDK Readme.txt  temp  tools
$ cd build-tools
$ ls
24.0.3

可以看到安装成功了。

总结

通过这些我们也可以推测出其实那些IDE图形界面底层调用的也是这些命令吧。。

 类似资料: