ลดขนาด APK ของแอป ML Kit สำหรับ Android
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ก่อนที่จะทําให้แอปที่ใช้โมเดล ML Kit ในอุปกรณ์ใช้งานได้ในเวอร์ชันที่ใช้งานจริง ให้ลองทําตามคําแนะนําในหน้านี้เพื่อลดขนาดการดาวน์โหลดของแอป
สร้างแอปเป็น Android App Bundle
สร้างและเผยแพร่แอปเป็น Android App Bundle เพื่อให้ Google Play สร้าง APK ตามความหนาแน่นของหน้าจอ สถาปัตยกรรม CPU และภาษาที่เฉพาะเจาะจงโดยอัตโนมัติ ผู้ใช้จะต้องดาวน์โหลดเฉพาะ APK ที่ตรงกับการกำหนดค่าอุปกรณ์ และที่สำคัญที่สุดคือผู้ใช้จะดาวน์โหลดเฉพาะไลบรารีโค้ดเนทีฟที่ตรงกับสถาปัตยกรรมของอุปกรณ์เท่านั้น
ขั้นสูง: ย้ายฟีเจอร์ ML ที่ไม่บังคับไปยังโมดูลฟีเจอร์แบบไดนามิก
หากคุณใช้ ML Kit ในฟีเจอร์ของแอปที่ไม่ใช่วัตถุประสงค์หลัก ให้ลองปรับโครงสร้างแอปเพื่อย้ายฟีเจอร์นั้นและข้อกําหนดของ ML Kit ไปยังโมดูลฟีเจอร์แบบไดนามิก
หากต้องการให้ฟีเจอร์ ML Kit ทำงานในโมดูลฟีเจอร์แบบออนดีมานด์ ให้รวมไลบรารีการสนับสนุนฟีเจอร์แบบไดนามิกของ ML Kit ใน Playstore ไว้ในไฟล์ build.gradle
ของ apk หลัก
dependencies {
// ...
implementation 'com.google.mlkit:playstore-dynamic-feature-support:16.0.0-beta2'
}
ซึ่งจะช่วยป้องกันไม่ให้ผู้ใช้ดาวน์โหลดโมเดล ML ของแอปโดยไม่จำเป็น ซึ่งอาจมีขนาดใหญ่
ขั้นสูง: ยกเว้นไบนารี ML Kit ที่ไม่ได้ใช้
ML Kit สร้างขึ้นเพื่อรองรับทั้งสถาปัตยกรรม 32 บิตและ 64 บิต หากแอปรองรับเฉพาะโหมด 32 บิต เช่น เนื่องจากคุณใช้ไลบรารีที่มีเฉพาะไบนารี 32 บิต คุณสามารถยกเว้นไลบรารี ML Kit ที่ไม่ได้ใช้ออกจากบิลด์ได้ ดังนี้
android {
defaultConfig {
ndk {
// Don't package arm64-v8a or x86_64
abiFilters 'armeabi-v7a', 'x86'
}
}
}
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-29 UTC
[null,null,["อัปเดตล่าสุด 2025-08-29 UTC"],[[["\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```"]]