這個屬性對 NDK 建構沒有任何影響。NDK 的 API 可用性受 minSdkVersion 規範。這是因為 C++ 符號在程式庫載入時就被即時解析,而不是延遲到在第一次呼叫時才解析 (在 Java 中亦是如此)。使用 minSdkVersion 中未提供的任何符號會導致程式庫在沒有新版 API 的 OS 版本中載入失敗,無論這些 API 是否被呼叫都是如此。
如果是新的應用程式,請選擇最新版本。如果是現有應用程式,請將應用程式更新為最新版本。
targetSdkVersion
與 Java 類似,應用程式的 targetSdkVersion 可以變更原生程式碼的執行階段行為。系統中的行為變更僅會在適用情況下影響符合以下條件的應用程式:targetSdkVersion 版本大於或等於採用對應變更的 OS 版本。
雖然應用程式開發人員通常會知道應用程式的 targetSdkVersion,但對於不知道使用者會選擇哪個 targetSdkVersion 的程式庫開發人員而言,這個 API 相當實用。
在執行階段期間,您可以透過呼叫 android_get_application_target_sdk_version() 取得應用程式使用的 targetSdkVersion。這個 API 適用於 API 級別 24 及更高級別。這個函式的簽名如下:
/** * Returns the `targetSdkVersion` of the caller, or `__ANDROID_API_FUTURE__` if * there is no known target SDK version (for code not running in the context of * an app). * * The returned values correspond to the named constants in `<android/api-level.h>`, * and is equivalent to the AndroidManifest.xml `targetSdkVersion`. * * See also android_get_device_api_level(). * * Available since API level 24. */intandroid_get_application_target_sdk_version()__INTRODUCED_IN(24);
其他行為變更可能取決於裝置 API 級別。您可以透過呼叫 android_get_device_api_level() 取得執行應用程式的裝置 API 級別。這個函式的簽名如下:
/** * Returns the API level of the device we're actually running on, or -1 on failure. * The returned values correspond to the named constants in `<android/api-level.h>`, * and is equivalent to the Java `Build.VERSION.SDK_INT` API. * * See also android_get_application_target_sdk_version(). */intandroid_get_device_api_level();
取得裝置 API 級別後,NDK 程式碼就能動態瞭解平台中的行為變化。由於次要 API 級別與行為變更無關,且我們目前沒有在次要 API 級別中新增 NDK 功能的計畫,因此無法直接透過 NDK 呼叫取得完整的裝置 API 級別。
您可以利用 dlopen()/dlsym() 或弱式 API 參照,呼叫比 minSdkVersion 指定的更新版 API。
maxSdkVersion
這個屬性對 NDK 建構沒有任何影響。
minSdkVersion
build.gradle 檔案中設定的 minSdkVersion 會決定建構時可用的 API (請參閱 compileSdkVersion,瞭解這不同於 Java 建構的原因),也會影響可與您的程式碼相容的最低 OS 版本。
NDK 會使用 minSdkVersion 來判斷編譯程式碼時可以使用哪些功能。例如,這個屬性會決定 libc 中使用的 FORTIFY 功能,而且如果您的二進位檔與舊版 Android 不相容,這個屬性也可能會為這些二進位檔啟用改善效能或大小功能 (例如 GNU 雜湊或 RELR)。即使您並未使用任何新的 API,這個屬性仍會決定程式碼支援的最低 OS 版本。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-20 (世界標準時間)。"],[],[],null,["# Android SDK version properties\n\nAndroid applications can set a number of SDK version properties in their\n`build.gradle` file. The [Android `build.gradle`](/studio/build#module-level) documentation explains what\nthose properties mean for the application in general. This document explains how\nthose properties affect NDK builds.\n\ncompileSdkVersion\n-----------------\n\nThis property has no effect on NDK builds. API availability for the NDK is\ninstead governed by `minSdkVersion`. This is because C++ symbols are eagerly\nresolved at library load time rather than lazily resolved when first called (as\nthey are in Java). Using any symbols that are not available in the\n`minSdkVersion` will cause the library to fail to load on OS versions that do\nnot have the newer API, regardless of whether or not those APIs will be called.\n\nFor a new app, choose the newest version available. For an existing app, update\nthis to the latest version when convenient.\n\ntargetSdkVersion\n----------------\n\nSimilar to Java, the `targetSdkVersion` of your app can change the runtime\nbehavior of native code. Behavior changes in the system are, when feasible, only\napplied to apps with a `targetSdkVersion` greater than or equal to the OS\nversion that introduced the change.\n\nFor a new app, choose the newest version available. For an existing app, update\nthis to the latest version when convenient (after updating `compileSdkVersion`).\n\nWhile application developers generally know their app's `targetSdkVersion`, this\nAPI is useful for library developers that cannot know which `targetSdkVersion`\ntheir users will choose.\n\nAt runtime, you can get the `targetSdkVersion` used by an application by calling\n`android_get_application_target_sdk_version()`. This API is available in API\nlevel 24 and later. This function has the following signature: \n\n /**\n * Returns the `targetSdkVersion` of the caller, or `__ANDROID_API_FUTURE__` if\n * there is no known target SDK version (for code not running in the context of\n * an app).\n *\n * The returned values correspond to the named constants in `\u003candroid/api-level.h\u003e`,\n * and is equivalent to the AndroidManifest.xml `targetSdkVersion`.\n *\n * See also android_get_device_api_level().\n *\n * Available since API level 24.\n */\n int android_get_application_target_sdk_version() __INTRODUCED_IN(24);\n\nOther behavior changes might depend on the device API level. You can get the API\nlevel of the device your application is running on by calling\n`android_get_device_api_level()`. This function has the following signature: \n\n /**\n * Returns the API level of the device we're actually running on, or -1 on failure.\n * The returned values correspond to the named constants in `\u003candroid/api-level.h\u003e`,\n * and is equivalent to the Java `Build.VERSION.SDK_INT` API.\n *\n * See also android_get_application_target_sdk_version().\n */\n int android_get_device_api_level();\n\nGetting device API levels allows NDK code to be dynamically aware of behavior\nchanges in the platform. Since minor API levels aren't tied to behavior changes,\nand we have no current plans to add new NDK functionality in minor API levels,\nthere is no way to get the full device API level directly from an NDK call.\n\nYou can make use of either `dlopen()`/`dlsym()` or weak API references to call\nnewer APIs than what your `minSdkVersion` specifies.\n\nmaxSdkVersion\n-------------\n\nThis property has no effect on NDK builds.\n\nminSdkVersion\n-------------\n\nThe `minSdkVersion` set in your `build.gradle` file determines which APIs are\navailable at build time (see [compileSdkVersion](#compilesdkversion) to understand why this differs\nfrom Java builds), and determines the minimum version of the OS that your code\nwill be compatible with.\n\nThe `minSdkVersion` is used by the NDK to determine what features may be used\nwhen compiling your code. For example, this property determines which [FORTIFY](https://android-developers.googleblog.com/2017/04/fortify-in-android.html)\nfeatures are used in libc, and may also enable performance or size improvements\n(such as [GNU hashes](https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#gnu-hashes-availible-in-api-level-23) or [RELR](https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#relative-relocations-relr)) for your binaries that are not compatible with\nolder versions of Android. Even if you do not use any new APIs, this property\nstill governs the minimum supported OS version of your code.\n| **Warning:** Your app *might* work on older devices even if your native libraries are built with a newer `minSdkVersion`. **Do not rely on this behavior.** It is not guaranteed to work, and may not on other NDK versions, OS versions, or individual devices.\n\nFor a new app, see the user distribution data in Android Studio's New Project\nWizard or on [apilevels.com](https://apilevels.com). Choose your balance between\npotential market share and maintenance costs. The lower your `minSdkVersion`,\nthe more time you'll spend working around old bugs and adding fallback behaviors\nfor features that weren't implemented yet.\n\nFor an existing app, raise your `minSdkVersion` whenever old API levels are no\nlonger worth the maintenance costs, or lower it if your users demand it and it's\nworth the new maintenance costs. The Play console has metrics specific to your\napp's user distribution.\n| **Note:** The NDK has its own `minSdkVersion` defined in `\u003cNDK\u003e/meta/platforms.json`. This is the lowest API level supported by the NDK. Do not set your app's `minSdkVersion` lower than this. Play may allow your app to be installed on older devices, but your NDK code may not work.\n\nThe `minSdkVersion` of your application is made available to the preprocessor\nvia the `__ANDROID_MIN_SDK_VERSION__` macro (the legacy `__ANDROID_API__` is\nidentical, but prefer the former because its meaning is clearer). This macro is\ndefined automatically by Clang, so no header needs to be included to use it. For\nNDK builds, this macro is always defined."]]