2011-01-13 114 views
198

現在我必須下載並安裝Android SDK abd AVD Manager,然後通過UI安裝API,工具。有沒有辦法讓這個過程自動化?有沒有辦法使android sdk安裝自動化?

+0

[如何在命令行上安裝Android SDK構建工具?](http://stackoverflow.com/questions/17963508/how-to-install-android-sdk-build-tools-on-the - 命令行) – 2016-06-25 14:19:07

+0

可能重複[「android更新sdk」在無頭Linux](http://stackoverflow.com/questions/2157089/android-update-sdk-on-headless-linux) – phunehehe 2016-08-16 13:01:47

回答

377

越接近你可以得到自動化可能是:

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 
    -u --no-ui Updates from command-line (does not display the GUI) 
    -o --obsolete Installs obsolete packages 
    -t --filter A filter that limits the update to the specified types of packages in the form of 
       a comma-separated list of [platform, tool, platform-tool, doc, sample, extra] 
    -s --no-https Uses HTTP instead of HTTPS (the default) for downloads 
    -n --dry-mode Simulates the update but does not download or install anything 

如果要列出可用的包進行安裝,您可以使用:

$ android update sdk --no-ui 

機器人進行自動更新提供這些選項

$ android list sdk 

您將獲得一個有序的軟件包列表,充足的

Packages available for installation or update: 9 
    1- ARM EABI v7a System Image, Android API 15, revision 2 
    2- Intel x86 Atom System Image, Android API 15, revision 1 
    3- Android Support, revision 8 
    4- Google AdMob Ads SDK, revision 6 
    5- Google Analytics SDK, revision 2 
    6- Google Play APK Expansion Library, revision 1 
    7- Google Play Billing Library, revision 2 
    8- Google Play Licensing Library, revision 2 
    9- Google Web Driver, revision 2 

您也可以更新只限制其所需的成分,如果你使用--filter選項

$ android update sdk --filter <component> --no-ui 

其中component是一個或多個

  • 數量由android list sdk返回(即,也稱爲包索引
  • 附加
  • DOC
  • 額外
  • 平臺
  • 平臺的工具
  • 樣品
  • 系統映像
  • 工具

或者可以是一個或多個特定標識符。舉例來說,如果你只是想下載一個小組特定的軟件包,你可以這樣做:

$ android update sdk -u --filter platform-tools,android-16,extra-android-support 

,你將剛纔得到的平臺工具,API級別16和支持jar包。如果你只是構建一臺生成機器,這將非常方便,並且必須支付下載所有額外的東西,你永遠不會使用。

要查看可以使用--help可用的選項,例如

$ android --help list sdk 

     Usage: 
     android [global options] list sdk [action options] 
     Global options: 
    -h --help  : Help on a specific command. 
    -v --verbose : Verbose mode, shows errors, warnings and all messages. 
    --clear-cache: Clear the SDK Manager repository manifest cache. 
    -s --silent  : Silent mode, shows errors only. 

        Action "list sdk": 
    Lists remote SDK repository. 
Options: 
    -o --obsolete : Deprecated. Please use --all instead. 
    -a --all  : Lists all available packages (including obsolete and 
        installed ones) 
    --proxy-host: HTTP/HTTPS proxy host (overrides settings if defined) 
    --proxy-port: HTTP/HTTPS proxy port (overrides settings if defined) 
    -s --no-https : Uses HTTP instead of HTTPS (the default) for downloads. 
    -e --extended : Displays extended details on each package 
    -u --no-ui  : Displays list result on console (no GUI) [Default: true] 

UPDATE

最新版本引入sdkmanager,一個命令行工具,允許您查看,安裝,更新和卸載Android SDK的軟件包。

sdkmanager工具在Android SDK工具包(25.2.3及更高版本)中提供,位於android_sdk/tools/bin/

sdkmanager [--uninstall] [<common args>] [--package_file <file>] [<packages>...] 
    sdkmanager --update [<common args>] 
    sdkmanager --list [<common args>] 
    sdkmanager --licenses [<common args>] 

In its first form, installs, or uninstalls, or updates packages. 
    By default, the listed packages are installed or (if already installed) 
    updated to the latest version. 

    --uninstall: uninstalled listed packages. 

    <package> is a sdk-style path (e.g. "build-tools;23.0.0" or 
      "platforms;android-23"). 
    <package-file> is a text file where each line is a sdk-style path 
        of a package to install or uninstall. 
    Multiple --package_file arguments may be specified in combination 
    with explicit paths. 

In its second form (with --update), all installed packages are 
    updated to the latest version. 

In its third form, all installed and available packages are printed 
    out. 

In its fourth form (with --licenses), show and offer the option to 
    accept licenses for all available packages that have not already been 
    accepted. 

Common Arguments: 
    --sdk_root=<sdkRootPath>: Use the specified SDK root instead of the SDK 
           containing this tool 

    --channel=<channelId>: Include packages in channels up to <channelId>. 
          Common channels are: 
          0 (Stable), 1 (Beta), 2 (Dev), and 3 (Canary). 

    --include_obsolete: With --list, show obsolete packages in the 
         package listing. With --update, update obsolete 
         packages as well as non-obsolete. 

    --no_https: Force all connections to use http rather than https. 

    --proxy=<http | socks>: Connect via a proxy of the given type. 

    --proxy_host=<IP or DNS address>: IP or DNS address of the proxy to use. 

    --proxy_port=<port #>: Proxy port to connect to. 

* If the env var REPO_OS_OVERRIDE is set to "windows", 
    "macosx", or "linux", packages will be downloaded for that OS. 

所以,要更新包中運行

$ sdkmanager --update 

接受許可證

$ yes | sdkmanager --licenses 
+1

輝煌,謝謝。 – Matthias 2011-01-14 08:14:43

+1

完美。完善。 – CodingWithoutComments 2011-08-04 21:17:46

+1

thx!這對於持續集成來說非常完美。 – Snicolas 2011-11-18 08:29:28

50

這對我沒有工作......

echo "y" | android .... 

所以我最終在這裏:

expect -c ' 
set timeout -1 ; 
spawn sudo /opt/android-sdk/tools/android update sdk -u; 
expect { 
    "Do you accept the license" { exp_send "y\r" ; exp_continue } 
    eof 
} 
' 
16

爲了回答與 'Y' 的所有許可,您可以在腳本試試這個:

(while : 
do 
    echo 'y' 
    sleep 2 
done) | android update sdk -u ..... 
44

我使用它來安裝和更新的SDK特拉維斯-CI

curl --location http://dl.google.com/android/android-sdk_r22.3-linux.tgz | tar -x -z -C $HOME 
export ANDROID_HOME=$HOME/android-sdk-linux 
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools 
(sleep 5 && while [ 1 ]; do sleep 1; echo y; done) | android update sdk --no-ui --filter platform-tool,android-19,sysimg-19,build-tools-19.0.1 
5

對於任何一個仍在搜索下載所有Android軟件包的方法,我已經寫了一個腳本來做到這一點。它將下載所有未廢棄的包。

#!/binbash 
# Install all non-obsolete android sdk packages. 
# author: Tai Le Tien (letientai299 at gmail.com) 

function install_sdk { 
    android update sdk -u -s -a -t "$1" 
} 

function fetch_non_obsoled_package_indices { 
    # Fetch the sdk list using non-https connections 
    android list sdk -u -s -a |\ 
    # Filter obsoleted packages 
    sed '/\(Obsolete\)/d' |\ 
    # Filter to take only the index number of package 
    sed 's/^[ ]*\([0-9]*\).*/\1/' |\ 
    # Remove the empty lines 
    sed -n 's/^[^ $]/\0/p' 
} 

for package_index in $(fetch_non_obsoled_package_indices) 
do 
    echo "=====================================================================" 
    echo "Start to install package: ${package_index}" 
    echo "=====================================================================" 
    # Auto accept license 
    echo -e "y" | install_sdk "${package_index}" 
    echo 
    echo 
done 

您還可以看到它在我的Github repo

好:

  • 不依賴於expect
  • 無頭。

的缺點:

  • 您仍然需要手動安裝基本的SDK,並把android到您的路徑。
  • 腳本只適用於unix。
0

然而,僅下載需要另一個腳本,非{obsolute,源,仿真圖像,文檔}包:

#!/bin/bash 
set -e 

# cd into where tools/android can be found 
if [[ -d "$ANDROID_HOME" ]]; then 
    cd "$ANDROID_HOME" 
elif [[ -x "$(dirname "$0")/tools/android" ]]; then 
    cd "$(dirname "$0")" 
else 
    echo "FAILED: Cannot find ANDROID_HOME/tools/android" 
    exit 1 
fi 

android() { 
    "$(dirname $0)/tools/android" "[email protected]" 
} 

needed_packages() { 
    android list sdk -u -s -e   \ 
    | grep '^id:'     \ 
    | cut -d'"' -f2     \ 
    | grep -v 'source'    \ 
    | grep -v 'sys-img'    \ 
    | grep -v 'doc'     \ 
    | paste -d, -s - 
} 

main() { 
    (while : ; do 
    echo 'y' 
    sleep 1 
    done) | android update sdk -u -s -a -t "$(needed_packages)" 
} 

main 

某些部分是從這個線程其他答案拍攝。

3

在較新的android版本(例如,25.2.5)我們應該使用sdkmanager(安裝包的

實例,而不是android命令):

android-sdk/tools/bin/sdkmanager "extras;android;m2repository" 

命令來獲取所有可用的軟件包的列表:

android-sdk/tools/bin/sdkmanager --verbose --list 

This web-page列表下載SDK-tools的鏈接:

這裏是一個鏈接到開源庫docker-android它可以將Android安裝在Docker鏡像中。

你也可以在this SO Question: Automatically accept all SDK licences找到答案有用。

相關問題