縮減 ML Kit Android 應用程式('s APK) 的大小
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在將使用 ML Kit 裝置端模型的應用程式部署至正式版之前,建議您按照本頁的建議縮減應用程式的下載大小。
將應用程式建構為 Android 應用程式套件
請將應用程式建構及部署為 Android App Bundle,以便 Google Play 自動為特定螢幕密度、CPU 架構和語言產生 APK。使用者只需下載與裝置設定相符的 APK,最重要的是,使用者只會下載與裝置架構相符的原生程式碼程式庫。
進階:將選用的機器學習功能移至動態功能模組
如果您在應用程式中使用 ML Kit 的功能並非主要用途,建議您重構應用程式,將該功能及其 ML Kit 依附元件移至動態功能模組。
為了讓 ML Kit 功能在隨選功能模組中運作,請在基本 APK 的 build.gradle
檔案中加入 ML Kit Play 商店動態功能支援程式庫依附元件。
dependencies {
// ...
implementation 'com.google.mlkit:playstore-dynamic-feature-support:16.0.0-beta2'
}
這樣一來,您就能避免使用者不必要地下載應用程式的 ML 模型 (可能會很大)。
進階:排除未使用的機器學習套件二進位檔
ML Kit 的設計可同時支援 32 位元和 64 位元架構。如果應用程式只支援 32 位元模式 (例如,因為您使用的是只提供 32 位元二進位檔的程式庫),您可以從建構中排除未使用的 ML Kit 程式庫:
android {
defaultConfig {
ndk {
// Don't package arm64-v8a or x86_64
abiFilters 'armeabi-v7a', 'x86'
}
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間:2025-08-29 (世界標準時間)。"],[[["\u003cp\u003eReduce your app's download size by building it as an Android App Bundle, enabling Google Play to deliver optimized APKs to users based on their device configurations.\u003c/p\u003e\n"],["\u003cp\u003eFor non-core ML features, leverage dynamic feature modules to deliver them on demand, minimizing the initial download size by excluding optional ML models.\u003c/p\u003e\n"],["\u003cp\u003eIf your app only supports 32-bit mode, exclude unused 64-bit ML Kit libraries to further reduce the app's size.\u003c/p\u003e\n"],["\u003cp\u003eTo enable ML Kit features in on-demand modules, include the \u003ccode\u003eplaystore-dynamic-feature-support\u003c/code\u003e library in your base APK's dependencies.\u003c/p\u003e\n"]]],[],null,["Before you deploy to production an app that uses an ML Kit on-device model,\nconsider following the advice on this page to reduce the download size of your\napp.\n\nBuild your app as an Android App Bundle\n\nBuild and deploy your app as an [Android App Bundle](//developer.android.com/guide/app-bundle/) so that Google\nPlay can automatically generate APKs for specific screen densities, CPU\narchitectures, and languages. Users will only have to download the APKs that\nmatch their device configuration, and most importantly, users only download the\nnative code libraries that match their device architecture.\n\nAdvanced: Move optional ML features to dynamic feature modules\n\nIf you use ML Kit in a feature of your app that isn't its primary purpose,\nconsider refactoring your app to move that feature and its ML Kit\ndependencies to a [dynamic feature module](//developer.android.com/studio/projects/dynamic-delivery#dynamic_feature_modules).\n\nIn order for ML Kit features to work in an on-demand feature module, in your base apk's `build.gradle` file,\ninclude the ML Kit playstore dynamic feature support library dependency. \n\n```carbon\ndependencies {\n // ...\n implementation 'com.google.mlkit:playstore-dynamic-feature-support:16.0.0-beta2'\n}\n```\n\nBy doing so, you prevent users from unnecessarily downloading your app's ML models, which can be\nlarge.\n\nAdvanced: Exclude unused ML Kit binaries\n\nML Kit is built with support for both 32-bit and 64-bit architectures. If\nyour app only supports 32-bit mode---for example, because you use a library\nthat only provides 32-bit binaries---you can exclude the unused ML Kit\nlibraries from your build: \n\n```carbon\nandroid {\n defaultConfig {\n ndk {\n // Don't package arm64-v8a or x86_64\n abiFilters 'armeabi-v7a', 'x86'\n }\n }\n}\n```"]]