設定 Xamarin.Android 的 Android SDKSetting up the Android SDK for Xamarin.Android

Visual Studio 包含 Android SDK 管理員,可用來下載 Android SDK 工具、平台,以及開發 Xamarin.Android 應用程式所需的其他元件。Visual Studio includes an Android SDK Manager that you use to download Android SDK tools, platforms, and other components that you need for developing Xamarin.Android apps.

概觀Overview

本指南說明如何在 Visual Studio 和 Visual Studio for Mac 中使用 Xamarin Android SDK 管理員。This guide explains how to use the Xamarin Android SDK Manager in Visual Studio and Visual Studio for Mac.

注意

本指南適用於 Visual Studio 2019、Visual Studio 2017 和 Visual Studio for Mac。This guide applies to Visual Studio 2019, Visual Studio 2017, and Visual Studio for Mac.

Xamarin Android SDK 管理員 (與使用 .NET 的行動開發工作負載一併安裝) 可協助您下載開發 Xamarin.Android 應用程式所需的最新 Android 元件。The Xamarin Android SDK Manager (installed as part of the Mobile development with .NET workload) helps you download the latest Android components that you need for developing your Xamarin.Android app. 它會取代 Google 已過時的獨立 SDK 管理員。It replaces Google's standalone SDK Manager, which has been deprecated.

需求Requirements

若要使用 Xamarin Android SDK 管理員,您需要下列項目:To use the Xamarin Android SDK Manager, you will need the following:

  • Visual Studio 2019 Community、Professional 或 Enterprise。Visual Studio 2019 Community, Professional, or Enterprise.

  • 或是 Visual Studio 2017 (Community、Professional 或 Enterprise 版本)。OR Visual Studio 2017 (Community, Professional, or Enterprise edition). 需要 Visual Studio 2017 15.7 版或更新版本。Visual Studio 2017 version 15.7 or later is required.

  • Visual Studio Tools for Xamarin 4.10.0 版或更新版本 (隨著用 .NET 進行行動裝置開發工作負載安裝)。Visual Studio Tools for Xamarin version 4.10.0 or later (installed as part of the Mobile development with .NET workload).

Xamarin Android SDK 管理員也需要 Java 開發套件 (會隨 Xamarin.Android 自動安裝)。The Xamarin Android SDK Manager also requires the Java Development Kit (which is automatically installed with Xamarin.Android). 有多個 JDK 替代項目可供選擇:There are several JDK alternatives to choose from:

  • 預設情況下,Xamarin.Android 使用 JDK 8 ,如果您為 API 級別 24 或更高(JDK 8 還支援早於 24 的 API 級別),則需要JDK 8。By default, Xamarin.Android uses JDK 8, which is required if you are developing for API level 24 or greater (JDK 8 also supports API levels earlier than 24).

  • 如果您是針對 API 層級 23 或更早的層級進行開發,則可繼續使用 JDK 7You can continue to use JDK 7 if you are developing specifically for API level 23 or earlier.

  • 若您目前使用 Visual Stuidio 15.8 Preview 5 或更新版本,可嘗試使用 Microsoft 的 Mobile OpenJDK 散發來替代 JDK 8。If you are using Visual Studio 15.8 Preview 5 or later, you can try using Microsoft's Mobile OpenJDK Distribution rather than JDK 8.

重要

Xamarin.Android 不支援 JDK 9。Xamarin.Android does not support JDK 9.

SDK 管理員SDK Manager

若要在 Visual Studio 中啟動 SDK 管理員,按一下 [工具] > [Android] > [Android SDK 管理員]****:To start the SDK Manager in Visual Studio, click Tools > Android > Android SDK Manager:

Android SDK 管理員選單項目的位置Location of the Android SDK Manager menu item

Android SDK 管理員會在 [Android SDKs and Tools] (Android SDK 與工具)**** 畫面中開啟。The Android SDK Manager opens in the Android SDKs and Tools screen. 此畫面有兩個索引標籤 [平台]– **** 與 [工具]****:This screen has two tabs – Platforms and Tools:

Android SDK 管理員在 [平台] 索引標籤中開啟的螢幕擷取畫面Screenshot of the Android SDK Manager open in the Platforms tab

後續各節將更詳細說明 [Android SDK 及工具]**** 畫面。The Android SDKs and Tools screen is described in more detail in the following sections.

Android SDK 位置Android SDK location

Android SDK 位置可在 [Android SDKs and Tools] (Android SDK 與工具)**** 畫面頂端設定,如先前的螢幕擷取畫面所示。The Android SDK location is configured at the top of the Android SDKs and Tools screen, as seen in the previous screenshot. 您必須先正確設定此位置,[平台]**** 和 [工具]**** 索引標籤才能正常運作。This location must be configured correctly before the Platforms and Tools tabs will function properly. 您可能會因為下列一或多個原因,而需要設定 Android SDK 的位置:You may need to set the location of the Android SDK for one or more of the following reasons:

  1. Android SDK 管理員找不到 Android SDK。The Android SDK Manager was unable to locate the Android SDK.

  2. 您已將 Android SDK 安裝於替代 (非預設) 位置。You have installed the Android SDK in a alternate (non-default) location.

若要設定 Android SDK 的位置,請按一下 [Android SDK Location] (Android SDK 位置)**** 最右側的省略符號 (…) 按鈕。To set the location of the Android SDK, click the ellipsis (…) button to the far right of Android SDK Location. 這會開啟 [瀏覽資料夾]**** 對話方塊,以用來瀏覽至 Android SDK 位置。This opens the Browse For Folder dialog to use for navigating to the location of the Android SDK. 在下列螢幕擷取畫面中,會選取位於 Program Files (x86)\Android 下方的 Android SDK:In the following screenshot, the Android SDK under Program Files (x86)\Android is being selected:

尋找 Android SDK 之 Windows [瀏覽資料夾] 對話方塊的螢幕擷取畫面

當您按一下 [確定]**** 時,SDK 管理員將會管理安裝於所選位置的 Android SDK。When you click OK, the SDK Manager will manage the Android SDK that is installed at the selected location.

[Tools] (工具) 索引標籤Tools tab

[工具]**** 索引標籤會顯示 [工具]__ 和 [額外項目]__ 的清單。The Tools tab displays a list of tools and extras. 使用此索引標籤來安裝 Android SDK 工具、平台工具及建置工具。Use this tab to install the Android SDK tools, platform tools, and build tools. 此外,您還能安裝 Android Emulator、低階除錯程式 (LLDB)、NDK、HAXM 加速,以及 Google Play 程式庫。Also, you can install the Android Emulator, the low-level debugger (LLDB), the NDK, HAXM acceleration, and Google Play libraries.

例如,若要下載 Google Android Emulator 套件,請按一下 [Android Emulator]**** 旁的核取記號,然後按一下 [套用變更]**** 按鈕:For example, to download the Google Android Emulator package, click the check mark next to Android Emulator and click the Apply Changes button:

從 [工具] 索引標籤安裝 Android EmulatorInstalling the Android Emulator from the Tools tab

可能會顯示具有以下訊息的對話方塊:下列套件需要您先接受其授權條款,然後才能安裝A dialog may be shown with the message, The following package requires that you accept its license terms before installing:

[接受授權] 畫面

如果您接受條款及條件,請按一下 [接受]****。Click Accept if you accept the Terms and Conditions. 在視窗底部,進度列會指出下載及安裝的進度。At the bottom of the window, a progress bar indicates download and installation progress. 安裝完成之後,[工具]**** 索引標籤將會顯示已安裝所選取的工具和額外項目。After the installation completes, the Tools tab will show that the selected tools and extras were installed.

[Platforms] (平台) 索引標籤Platforms tab

「 平台」 選項卡顯示每個平台的平臺 SDK 版本以及其他資源(如系統映像)的清單:The Platforms tab displays a list of platform SDK versions along with other resources (like system images) for each platform:

平台窗格的螢幕截圖Screenshot of the Platforms pane

此畫面列出了 Android 版本 (例如 Android 8.0)、代號 (Oreo)、API 層級 (例如 26),以及該平台的元件大小 (例如 1 GB)。This screen lists the Android version (such as Android 8.0), the code name (Oreo), the API level (such as 26), and the sizes of the components for that platform (such as 1 GB). 您可使用 [Platforms] (平台)**** 索引標籤來為您想要設為目標的 Android API 層級安裝元件。You use the Platforms tab to install components for the Android API level that you want to target. 如需 Android 版本與 API 層級的詳細資訊,請參閱 Understanding Android API Levels(了解 Android API 層級)。For more information about Android versions and API levels, see Understanding Android API Levels.

當平台的所有元件均安裝完成時,平台名稱旁就會出現一個選取記號。When all components of a platform are installed, a checkmark appears next to the platform name. 如果尚未安裝平台的所有元件,則會填滿該平台的方塊。If not all components of a platform are installed, the box for that platform is filled. 您可以通過按一下平台**+** 左側的 框來展開平臺以查看其元件(以及安裝了哪些元件)。You can expand a platform to see its components (and which components are installed) by clicking the + box to the left of the platform. 按一- 下 以取消展開平臺的元件清單。Click - to unexpand the component listing for a platform.

若要將其他平台新增至 SDK,請按一下平台旁邊的方塊,直到核取符號出現以安裝其所有元件為止,然後按一下 [套用變更]****:To add another platform to the SDK, click the box next to the platform until the checkmark appears to install all of its components, then click Apply Changes:

將 Android 7.1 Nougat 元件加入 Android SDK 的範例Example of adding Android 7.1 Nougat components to the Android SDK

若只要安裝特定元件,請按一下平台旁邊的方塊一次。To install only specific components, click the box next to the platform once. 您接著可以選取任何所需的個別元件:You can then select any individual components that you need:

新增 Android 7.1 元件的範例Example of adding some Android 7.1 components

請注意,要安裝的元件數目會出現在 [套用變更]**** 按鈕旁邊。Notice that the number of components to install appears next to the Apply Changes button. 按一下 [Apply Changes] (套用變更)**** 按鈕後,您會看到先前顯示的 [License Acceptance] (授權接受)**** 畫面。After you click the Apply Changes button, you will see the License Acceptance screen as shown earlier. 如果您接受條款及條件,請按一下 [接受]****。Click Accept if you accept the Terms and Conditions. 如果有多個要安裝的元件,您可能會多次看到這個對話方塊。You may see this dialog more than one time when there are multiple components to install. 在視窗底部,進度列將會指出下載及安裝的進度。At the bottom of the window, a progress bar will indicate download and installation progress. 當下載及安裝程序完成時 (視需要下載的元件數目而定,這可能需要花費幾分鐘),系統會以核取記號標示已新增的元件,並將其列為 [已安裝]****。When the download and installation process completes (this can take many minutes, depending on how many components need to be downloaded), the added components are marked with a checkmark and listed as Installed.

存放庫選取Repository selection

根據預設,Android SDK 管理員會從 Microsoft 管理的存放庫下載平台元件與工具。By default, the Android SDK Manager downloads platform components and tools from a Microsoft-managed repository. 若您需要存取 Microsoft 存放庫中尚無法使用的實驗性 alpha/beta 平台與元件,您可以切換 SDK 管理員為使用 Google 的存放庫。If you need access to experimental alpha/beta platforms and tools that are not yet available in the Microsoft repository, you can switch the SDK Manager to use Google's repository. 若要進行此切換,請按一下右下角的齒輪圖示,並選取 [Repository] > [Google (Unsupported)] ([存放庫] > [Google (不支援)])****:To make this switch, click the gear icon in the lower right-hand corner and select Repository > Google (Unsupported):

選擇谷歌的存儲庫Selecting Google's repository

選取 Google 存放庫時,先前無法使用的其他套件可能會出現在 [Platforms] (平台)**** 索引標籤中。When the Google repository is selected, additional packages may appear in the Platforms tab that were not available previously. (在上面的螢幕截圖中 ,Android SDK 平臺 28是透過切換到 Google 儲存庫添加的。請記住,使用 Google 儲存庫不受支援,因此不建議用於日常開發。(In the above screenshot, Android SDK Platform 28 was added by switching to the Google repository.) Keep in mind that use of the Google repository is unsupported and is therefore not recommended for everyday development.

若要切換回支援的平台與工具存放庫,請按一下 Microsoft (Recommended) (Microsoft (建議))****。To switch back to the supported repository of platforms and tools, click Microsoft (Recommended). 這會將套件與工具的清單還原至預設選取項目。This restores the list of packages and tools to the default selection.

總結Summary

本指南說明如何在 Visual Studio 和 Visual Studio for Mac 中安裝和使用 Xamarin Android SDK 管理員工具。This guide explained how to install and use the Xamarin Android SDK Manager tool in Visual Studio and Visual Studio for Mac.