設定專案
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
設定使用 Android Game Development Extension 的專案。
Android 遊戲開發擴充功能會叫用 MSBuild,將 C/C++ 原始碼建構為共用程式庫 (.so
) 和靜態程式庫 (.a
)。在建構程序中,自訂 MSBuild 工作會叫用 Gradle 來編譯 Java 和 Kotlin 原始碼、封裝資產,並產生 APK 檔案供部署。設定專案時,您必須確認 MSBuild 包含為 Android 平台建構所需的資訊。
使用 MSBuild 建構 C/C++
一般 Android 專案是以 Gradle 建構,其中專案內的原生程式碼是以執行 CMake 或 ndk-build 的 Gradle 通道建構的。使用 Visual Studio 的 Android Game Development Extension 時,建構程序會按相反的順序執行。現在 MSBuild 是建構程序的起點。所有 C/C++ 原始碼都是先由 MSBuild 為您的系統上所安裝的新 Android 平台建構的,做為擴充功能的一部分 (例如「Android-x86_64」)。MSBuild 接著會叫用 Gradle,將含有 C/C++ 邏輯的共用資料庫檔案封裝至 APK。
請先在 MSBuild 內的 CMake 或 ndk-build 中複製專案現有的建構邏輯。將目標平台設為以下項目:
- Android-x86
- Android-x86_64
- Android-armeabi-v7a
- Android-arm64-v8a
這些平台都是由 Android Game Development Extension 提供。
設定編譯和連結選項
AGDE 會在建構應用程式的 C/C++ 部分時,使用您選取的 NDK 來判斷預設的編譯和連結選項。
如果您需要自訂這些編譯或連結選項,可以使用「專案屬性」進行設定。您可以在 C/C++ (用於編譯)、Librarian (用於靜態程式庫封存) 和 Linker (用於動態程式庫連結) 群組中找到最常見的選項。如果您需要傳遞任何其他自訂選項,可以將這些選項新增至「Command Line」部分。舉例來說,如果您使用的是 r28 以下版本的 NDK,建議您設定連結器標記,讓應用程式支援 16 KB 分頁大小。
雖然茶壺範例專案包括 Android 平台,但您必須手動將 Android 平台新增至現有專案。如要新增平台,請在 Visual Studio 執行下列步驟:
- 依序選取「Build」>「Configuration Manager」。
- 在「Active solution platform」下方,選取「<New>」。
為新平台輸入下列其中一個值:
- Android-armeabi-v7a
- Android-arm64-v8a
- Android-x86
- Android-x86_64
在「Copy settings from」方塊中選取另一個現有 Android 平台。如果還沒有 Android 平台,則選取「<Empty>」。確認已啟用「Create new project platforms」。
新增 Android APK 項目
依序選取「Add」>「New Item」>「Visual C++」>「Android」>「Android APK」,然後按一下「Add」。請在下列對話方塊中設定 Android 應用程式。
- 「Application Name」:人類可讀的 Android 應用程式名稱。
- 「Application ID」:Android 應用程式的專屬 ID。
- 「Solution Explorer Location」:包含新增 Android 封裝支援檔案的虛擬資料夾位置。根據預設,這類檔案還位於名稱相同的資料夾的專案中。如要自訂位置,請勾選「Put support files in a custom location」核取方塊,並指定自訂位置。虛擬資料夾仍位於 Solution Explorer 的目前專案中。
讓 MSBuild 叫用 Gradle 建構 APK
除非 MSBuild 知道 Gradle 專案的位置,否則其無法叫用 Gradle。使用「Gradle Build Directory」屬性設定這個位置,如圖 1 所示。

圖 1.「Gradle Build Directory」屬性
此外,請設定「Application Module」、「Application Variant」和「APK Name」屬性 (如上圖所示),以便 MSBuild 瞭解您要建構的內容。
- Application Module:Gradle 子專案的名稱。這是您在
settings.gradle
檔案中設定的主要專案。對於使用 Android Studio 直接建立的專案,通常將其稱為 app
。
- 「Application Variant」:要建構的 Android 變化版本。這個值應根據 MSBuild 設定進行設定。舉例來說,偵錯版本應有一個設為偵錯變化版本的值。如果專案的 MSBuild 設定名稱與 Gradle 變化版本名稱相符,則請使用預設值
$(Configuration)
。
- 「APK Name」:產生的 APK 檔案名稱,用於在開發電腦上偵錯和剖析。這個名稱會傳遞至 Gradle,且您的 Gradle 建構指令碼應遵循這點 (請參閱下一節的
MSBUILD_ANDROID_OUTPUT_APK_NAME
屬性)。
修改 Gradle 建構指令碼
在建構期間,MSBuild 會將下列資訊當做專案屬性傳遞至 Gradle 指令碼。變更專案現有的建構指令碼 (通常稱為 build.gradle
) 以讀取這些屬性。
MSBUILD_MIN_SDK_VERSION
:建構 APK 的最低 SDK 版本,以字串表示。在圖 2 所示的專案屬性頁面上,於「Minimum Android SDK Version」方塊中設定這個值。

圖 2.「Minimum Android SDK Version」屬性
Gradle 建構指令碼應將 minSdkVersion
或 minSdk
設為這個字串值,並視需要進行 toInteger()
類型轉換。
Groovy
android {
// ...
defaultConfig {
applicationId "com.yourcompany.yourapp"
minSdkVersion MSBUILD_MIN_SDK_VERSION
// Or: minSdk MSBUILD_MIN_SDK_VERSION.toInteger()
// ...
}
// ...
}
Kotlin
android {
// ...
defaultConfig {
applicationId = "com.yourcompany.yourapp"
minSdkVersion(MSBUILD_MIN_SDK_VERSION)
// Or: minSdk = MSBUILD_MIN_SDK_VERSION.toInteger()
// ...
}
// ...
}
MSBUILD_ANDROID_OUTPUT_APK_NAME
:Gradle 建構的 APK 預期名稱。Android Game Development Extension 功能會尋找與這個名稱相符的 APK,然後將其部署至已連結的裝置 (用於偵錯和剖析)。在圖 3 所示的專案屬性頁面的「APK Name」方塊中設定這個值。

圖 3.「APK Name」屬性
Gradle 建構指令碼必須遵守這項屬性。例如,以下範例會將所有變化版本的輸出 APK 名稱設為 MSBuild 選擇的名稱。
Groovy
android {
// ...
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = MSBUILD_ANDROID_OUTPUT_APK_NAME
}
}
// ...
}
Kotlin
android {
// ...
applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = MSBUILD_ANDROID_OUTPUT_APK_NAME
}
}
// ...
}
MSBUILD_JNI_LIBS_SRC_DIR
:包含 MSBuild 建構的共用資料庫 (.so
檔案) 的目錄。在下方所示的專案屬性頁面的「Output Directory」方塊中設定這個值。根據預設,這個值是 Visual Studio 專案的輸出目錄屬性,如圖 4 所示。

圖 4. 「Output Directory」屬性
Gradle 應該將共用資料庫檔案封裝在 APK 中的資料夾中,以便 Android 應用程式在執行階段載入這些檔案。
Groovy
android {
// ...
sourceSets {
main {
jniLibs.srcDirs += [MSBUILD_JNI_LIBS_SRC_DIR]
}
}
// ...
}
Kotlin
android {
// ...
sourceSets.getByName("main") {
jniLibs.srcDir(MSBUILD_JNI_LIBS_SRC_DIR)
}
// ...
}
此外,由於任何 C/C++ 程式碼現在是由 MSBuild 所建構,請移除 Gradle 建構指令碼中的 externalNativeBuild
區段。這些區段用於叫用 CMake 或 ndk-build 來編譯 C/C++ 程式碼,但現在已不再需要。
MSBUILD_NDK_VERSION
:用於建構專案的 NDK 版本。在圖 5 所示的專案屬性頁面中,於「Android NDK Version」方塊中設定這個值。

圖 5.「Android NDK Version」屬性
Gradle 建構指令碼應將 ndkVersion
設為這個值,如下所示:
Groovy
android {
// ...
ndkVersion MSBUILD_NDK_VERSION
// ...
}
Kotlin
android {
// ...
ndkVersion = MSBUILD_NDK_VERSION
// ...
}
詳情請參閱 Android Studio 主題「安裝及設定 NDK 和 CMake」。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Configure a project to use the Android Game Development Extension.\n\nThe Android Game Development Extension invokes MSBuild to build C/C++ source code into shared\nlibraries (`.so`) and static libraries (`.a`). As part of the build process, a\ncustom MSBuild task invokes Gradle to compile Java and Kotlin source code,\npackage assets, and generate an APK file for deployment. When you configure your\nproject, you must ensure that MSBuild has the information it needs to build for\nthe Android platform.\n\nBuild C/C++ with MSBuild\n------------------------\n\nA typical Android project is built with Gradle, where the native code inside the\nproject is built by a Gradle pass that runs either [CMake](/ndk/guides/cmake) or\n[ndk-build](/ndk/guides/ndk-build). With the Android Game Development Extension for Visual\nStudio, the build process is inverted. Now MSBuild is the starting point of the\nbuild process. All C/C++ source code is built first by MSBuild for the new\nAndroid platforms installed on your system as part of the extension (for\nexample, \"Android-x86_64\"). MSBuild then invokes Gradle to package the shared\nlibrary files that contain your C/C++ logic into an APK.\n\nYou should first replicate your project's existing build logic in CMake or\nndk-build in MSBuild. Set the target platforms to the following:\n\n- Android-x86\n- Android-x86_64\n- Android-armeabi-v7a\n- Android-arm64-v8a\n\nThese platforms are all provided by the Android Game Development Extension.\n\n### Set your compile and link options\n\nAGDE uses the NDK you select to determine the default compile and link options\nwhen building the C/C++ part of your app.\n\nIf you need to customize these compile or link options, you can set them using\nProject Properties. You can find the most common options in the C/C++\n(for compilation), Librarian (for static library archiving) and Linker (for\ndynamic library linking) groups. If you need to pass any other custom\noptions, you can add them to the Command Line section. For example,\nif you are using an NDK older than r28, you might want to set the linker flag\nto make your app [support 16 KB page sizes](/guide/practices/page-sizes#compile-16-kb-alignment).\n\n### Add an Android Platform\n\nWhile the teapot sample project includes Android platforms, you must manually\nadd an Android platform to an existing project. To add a new platform, do the\nfollowing in Visual Studio:\n\n1. Select **Build \\\u003e Configuration Manager**.\n2. Under **Active solution platform** , select **\\\u003cNew\\\u003e**.\n3. Type one of the following for the new platform:\n\n - **Android-armeabi-v7a**\n - **Android-arm64-v8a**\n - **Android-x86**\n - **Android-x86_64**\n4. In the **Copy settings from** box, select another existing Android\n platform, or **\\\u003cEmpty\\\u003e** if you do not have any Android platforms yet.\n Make sure you enabled **Create new project platforms**.\n\n### Add an Android APK item\n\nSelect **Add \\\u003e New Item \\\u003e Visual C++ \\\u003e Android \\\u003e Android APK** and click\n**Add**. Configure the Android application on the following dialog.\n\n- **Application Name**: The human-readable name of your Android application.\n- **Application ID** : The [unique identifier](/studio/build/configure-app-module#set_the_application_id) for your Android application.\n- **Solution Explorer Location** : Location of the virtual folder that contains the added Android packaging support files. By default, these files are also located in the project in a folder with the same name. You can customize the location by selecting the **Put support files in a custom location** checkbox and specifying a custom location. The virtual folder will still be under the current project in the Solution Explorer.\n\nMake MSBuild invoke Gradle to build an APK\n------------------------------------------\n\nMSBuild cannot invoke Gradle unless it knows the location of the Gradle project.\nSet this location using the **Gradle Build Directory** property, as\nshown in figure 1.\n\n\u003cbr /\u003e\n\n\n**Figure 1** . **Gradle Build Directory** property\n\nIn addition, set the **Application Module** , **Application Variant** , and **APK\nName** properties (as shown in the previous image) in order for MSBuild to know\nwhat to build.\n\n- **Application Module** : The name of the Gradle subproject. This is the main project set in the `settings.gradle` file. It is usually called `app` for projects directly created using Android Studio.\n- **Application Variant** : The Android variant to build. This value should be set according to the MSBuild configurations. For example, a debug build should have a value set to the debug variant. If your project's MSBuild configuration name matches the Gradle variant names, then just use the default value of `$(Configuration)`.\n- **APK Name** : The name of the generated APK file used for debugging and profiling on your development computer. This name is passed to Gradle and your Gradle build script should respect this (see the property `MSBUILD_ANDROID_OUTPUT_APK_NAME` in the following section).\n\n### Modify your Gradle build scripts\n\nDuring the build, MSBuild passes the following information as project properties\nto the Gradle script. Change your project's existing build scripts (typically\nnamed `build.gradle`) to read these properties.\n\n- `MSBUILD_MIN_SDK_VERSION`: The minimum SDK version for building the APK, as a\n string. Set this value in the **Minimum Android SDK Version** box on the\n project property page shown in figure 2.\n\n \u003cbr /\u003e\n\n\n **Figure 2** . **Minimum Android SDK Version** property\n\n The Gradle build script should set `minSdkVersion` or `minSdk` to this\n string value, with a `toInteger()` type conversion when necessary. \n\n ### Groovy\n\n ```groovy\n android {\n // ...\n\n defaultConfig {\n applicationId \"com.yourcompany.yourapp\"\n minSdkVersion MSBUILD_MIN_SDK_VERSION\n // Or: minSdk MSBUILD_MIN_SDK_VERSION.toInteger()\n // ...\n }\n\n // ...\n }\n ```\n\n ### Kotlin\n\n ```kotlin\n android {\n // ...\n\n defaultConfig {\n applicationId = \"com.yourcompany.yourapp\"\n minSdkVersion(MSBUILD_MIN_SDK_VERSION)\n // Or: minSdk = MSBUILD_MIN_SDK_VERSION.toInteger()\n // ...\n }\n\n // ...\n }\n ```\n- `MSBUILD_ANDROID_OUTPUT_APK_NAME`: The expected name of the APK that Gradle\n builds. The Android Game Development Extension will look for an APK matching this name and\n then deploy it to connected devices (for debugging and profiling). Set this\n value in the **APK Name** box on the project property page shown in figure 3.\n\n \u003cbr /\u003e\n\n\n **Figure 3** . **APK Name** property\n\n The Gradle build script must respect this property. For example, the\n following example sets the output APK name for all variants to the name\n chosen by MSBuild. \n\n ### Groovy\n\n ```groovy\n android {\n // ...\n\n applicationVariants.all { variant -\u003e\n variant.outputs.all {\n outputFileName = MSBUILD_ANDROID_OUTPUT_APK_NAME\n }\n }\n\n // ...\n }\n ```\n\n ### Kotlin\n\n ```kotlin\n android {\n // ...\n\n applicationVariants.all { variant -\u003e\n variant.outputs.all {\n outputFileName = MSBUILD_ANDROID_OUTPUT_APK_NAME\n }\n }\n\n // ...\n }\n ```\n- `MSBUILD_JNI_LIBS_SRC_DIR`: The directory containing the shared libraries\n (`.so` files) built by MSBuild. Set this value in the **Output Directory**\n box on the project property page shown below. By default, this value is the\n output directory property for the Visual Studio project, as shown in figure 4.\n\n \u003cbr /\u003e\n\n\n **Figure 4** . **Output Directory** property\n\n Gradle should package the shared library files in this folder inside the APK\n in order for the Android application to load them at runtime. \n\n ### Groovy\n\n ```groovy\n android {\n // ...\n\n sourceSets {\n main {\n jniLibs.srcDirs += [MSBUILD_JNI_LIBS_SRC_DIR]\n }\n }\n\n // ...\n }\n ```\n\n ### Kotlin\n\n ```kotlin\n android {\n // ...\n\n sourceSets.getByName(\"main\") {\n jniLibs.srcDir(MSBUILD_JNI_LIBS_SRC_DIR)\n }\n\n // ...\n }\n ```\n\n In addition, since any C/C++ code is now built by MSBuild, remove the\n `externalNativeBuild` sections in your Gradle build scripts. These sections\n were used to invoke CMake or ndk-build to compile your C/C++ code, but are\n no longer needed.\n- `MSBUILD_NDK_VERSION`: The version of the NDK to use to build your\n project. Set this value in the **Android NDK Version** box on the\n project property page shown in figure 5.\n\n \u003cbr /\u003e\n\n\n **Figure 5** . **Android NDK Version** property\n\n The Gradle build script should set `ndkVersion` to this value, as shown: \n\n ### Groovy\n\n ```groovy\n android {\n // ...\n\n ndkVersion MSBUILD_NDK_VERSION\n\n // ...\n }\n ```\n\n ### Kotlin\n\n ```kotlin\n android {\n // ...\n\n ndkVersion = MSBUILD_NDK_VERSION\n\n // ...\n }\n ```\n\n For more information, see the Android Studio topic\n [Install and configure the NDK and CMake](/studio/projects/install-ndk)."]]