缩减机器学习套件 Android 应用的 APK 的大小
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
将使用机器学习套件设备端模型的应用部署到生产环境之前,建议您按照本页面中给出的建议,缩小应用的下载体量。
构建应用时采用 Android App Bundle 格式
您可以在构建和部署应用时采用 Android App Bundle 格式,这样 Google Play 能自动生成适用于特定屏幕像素密度、CPU 架构和语言的 APK。这样,用户只需要下载与其设备配置相匹配的 APK,最重要的是,用户只需下载与其设备架构相匹配的原生代码库。
高级:将可选的机器学习功能移至动态功能模块
如果您的应用中使用机器学习套件的功能不是该应用的主要功能,建议您重构应用,将这部分功能及其机器学习套件依赖项移至动态功能模块。
为了让 ML Kit 功能在按需功能模块中正常运行,请在基本 apk 的 build.gradle
文件中添加 ML Kit Play 商店动态功能支持库依赖项。
dependencies {
// ...
implementation 'com.google.mlkit:playstore-dynamic-feature-support:16.0.0-beta2'
}
这样做可以防止用户下载不必要的机器学习模型,而这个模型的体量可能会很大。
高级:移除未使用的机器学习套件二进制文件
机器学习套件同时支持 32 位和 64 位架构。如果您的应用只支持 32 位模式(例如,由于您使用的库只提供 32 位二进制文件),您可以从您的 build 中排除用不到的机器学习套件库:
android {
defaultConfig {
ndk {
// Don't package arm64-v8a or x86_64
abiFilters 'armeabi-v7a', 'x86'
}
}
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):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```"]]