ตามที่อธิบายไว้ในบทความภาพรวมของบริการ Google Play SDK ที่ขับเคลื่อนโดยบริการ Google Play จะได้รับการสนับสนุนจากบริการในอุปกรณ์ บนอุปกรณ์แอนดรอยด์ที่ได้รับการรับรองจาก Google เพื่อประหยัดพื้นที่เก็บข้อมูลและหน่วยความจําทั่วทั้งกลุ่มอุปกรณ์ บริการบางอย่างจะได้รับการติดตั้งตามคําขอเมื่อเห็นได้ชัดว่าอุปกรณ์ที่ต้องการต้องมีฟังก์ชันการทํางานที่เกี่ยวข้อง เช่น ML Kit มอบตัวเลือกนี้ เมื่อใช้โมเดลในบริการของ Google Play
กรณีทั่วไปคือการที่บริการ (หรือ "โมดูล") จะดาวน์โหลดและติดตั้งควบคู่ไปกับแอปที่ต้องใช้ โดยขึ้นอยู่กับการอ้างอิง AndroidManifest.xml
ของ SDK เพื่อการควบคุมที่มากขึ้น API การติดตั้งโมดูลทําให้สามารถตรวจสอบความพร้อมใช้งานของโมดูลอย่างชัดเจน การติดตั้งโมดูลคําขอ ตรวจสอบสถานะคําขอ และจัดการกับข้อผิดพลาด
ทําตามขั้นตอนด้านล่างเพื่อให้ API พร้อมใช้งานใน ModuleInstallClient
โปรดทราบว่าข้อมูลโค้ดด้านล่างใช้ TensorFlow Lite SDK (play-services-tflite-java
) เป็นไลบรารีตัวอย่าง แต่ขั้นตอนเหล่านี้ใช้ได้กับไลบรารีที่ผสานรวมกับ OptionalModuleApi
เราจะอัปเดตคู่มือนี้ด้วยข้อมูลเพิ่มเติมเมื่อ SDK เพิ่มการสนับสนุน
ข้อควรทราบก่อนที่จะเริ่มต้น
ในการเตรียมแอป ให้ทําตามขั้นตอนในหัวข้อต่อไปนี้
ข้อกําหนดเบื้องต้นของแอป
ตรวจสอบว่าไฟล์บิวด์ของแอปใช้ค่าต่อไปนี้
minSdkVersion
ตั้งแต่19
ขึ้นไป
กําหนดค่าแอปของคุณ
ในไฟล์
settings.gradle
ระดับบนสุด ให้ใส่ที่เก็บ Maven ของ Google และที่เก็บส่วนกลางของ Maven ภายในบล็อกdependencyResolutionManagement
dependencyResolutionManagement { repositories { google() mavenCentral() } }
ในไฟล์บิวด์ Gradle ของโมดูล (มักจะเป็น
app/build.gradle
) ให้เพิ่มทรัพยากร Dependency ของบริการ Google Play สําหรับplay-services-base
และplay-services-tflite-java
dependencies { implementation 'com.google.android.gms:play-services-base:18.2.0' implementation 'com.google.android.gms:play-services-tflite-java:16.1.0' }
ตรวจสอบความพร้อมใช้งานของโมดูล
รับอินสแตนซ์ของ
ModuleInstallClient
:Kotlin
val moduleInstallClient = ModuleInstall.getClient(context)
Java
ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(context);
ตรวจสอบความพร้อมใช้งานของโมดูลที่ไม่บังคับโดยใช้
OptionalModuleApi
ดังนี้Kotlin
val optionalModuleApi = TfLite.getClient(context) moduleInstallClient .areModulesAvailable(optionalModuleApi) .addOnSuccessListener { if (it.areModulesAvailable()) { // Modules are present on the device... } else { // Modules are not present on the device... } } .addOnFailureListener { // Handle failure... }
Java
OptionalModuleApi optionalModuleApi = TfLite.getClient(context); moduleInstallClient .areModulesAvailable(optionalModuleApi) .addOnSuccessListener( response -> { if (response.areModulesAvailable()) { // Modules are present on the device... } else { // Modules are not present on the device... } }) .addOnFailureListener( e -> { // Handle failure… });
ส่งคําขอชะลอการติดตั้ง
รับอินสแตนซ์ของ
ModuleInstallClient
:Kotlin
val moduleInstallClient = ModuleInstall.getClient(context)
Java
ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(context);
ส่งคําขอที่เลื่อนเวลา:
Kotlin
val optionalModuleApi = TfLite.getClient(context) moduleInstallClient.deferredInstall(optionalModuleApi)
Java
OptionalModuleApi optionalModuleApi = TfLite.getClient(context); moduleInstallClient.deferredInstall(optionalModuleApi);
ส่งคําขอติดตั้งโมดูลแบบเร่งด่วน
รับอินสแตนซ์ของ
ModuleInstallClient
:Kotlin
val moduleInstallClient = ModuleInstall.getClient(context)
Java
ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(context);
(ไม่บังคับ) สร้าง
InstallStatusListener
เพื่อจัดการการอัปเดตสถานะการติดตั้งหากคุณต้องการตรวจสอบความคืบหน้าในการดาวน์โหลดใน UI ที่กําหนดเอง (เช่น แถบความคืบหน้า) คุณสามารถสร้าง
InstallStatusListener
เพื่อรับการอัปเดตสถานะการติดตั้งKotlin
inner class ModuleInstallProgressListener : InstallStatusListener { override fun onInstallStatusUpdated(update: ModuleInstallStatusUpdate) { // Progress info is only set when modules are in the progress of downloading. update.progressInfo?.let { val progress = (it.bytesDownloaded * 100 / it.totalBytesToDownload).toInt() // Set the progress for the progress bar. progressBar.setProgress(progress) } if (isTerminateState(update.installState)) { moduleInstallClient.unregisterListener(this) } } fun isTerminateState(@InstallState state: Int): Boolean { return state == STATE_CANCELED || state == STATE_COMPLETED || state == STATE_FAILED } } val listener = ModuleInstallProgressListener()
Java
static final class ModuleInstallProgressListener implements InstallStatusListener { @Override public void onInstallStatusUpdated(ModuleInstallStatusUpdate update) { ProgressInfo progressInfo = update.getProgressInfo(); // Progress info is only set when modules are in the progress of downloading. if (progressInfo != null) { int progress = (int) (progressInfo.getBytesDownloaded() * 100 / progressInfo.getTotalBytesToDownload()); // Set the progress for the progress bar. progressBar.setProgress(progress); } // Handle failure status maybe… // Unregister listener when there are no more install status updates. if (isTerminateState(update.getInstallState())) { moduleInstallClient.unregisterListener(this); } } public boolean isTerminateState(@InstallState int state) { return state == STATE_CANCELED || state == STATE_COMPLETED || state == STATE_FAILED; } } InstallStatusListener listener = new ModuleInstallProgressListener();
กําหนดค่า
ModuleInstallRequest
และเพิ่มOptionalModuleApi
ในคําขอ ดังนี้Kotlin
val optionalModuleApi = TfLite.getClient(context) val moduleInstallRequest = ModuleInstallRequest.newBuilder() .addApi(optionalModuleApi) // Add more APIs if you would like to request multiple optional modules. // .addApi(...) // Set the listener if you need to monitor the download progress. // .setListener(listener) .build()
Java
OptionalModuleApi optionalModuleApi = TfLite.getClient(context); ModuleInstallRequest moduleInstallRequest = ModuleInstallRequest.newBuilder() .addApi(optionalModuleApi) // Add more API if you would like to request multiple optional modules //.addApi(...) // Set the listener if you need to monitor the download progress //.setListener(listener) .build();
ส่งคําขอติดตั้ง
Kotlin
moduleInstallClient .installModules(moduleInstallRequest) .addOnSuccessListener { if (it.areModulesAlreadyInstalled()) { // Modules are already installed when the request is sent. } } .addOnFailureListener { // Handle failure… }
Java
moduleInstallClient.installModules(moduleInstallRequest) .addOnSuccessListener( response -> { if (response.areModulesAlreadyInstalled()) { // Modules are already installed when the request is sent. } }) .addOnFailureListener( e -> { // Handle failure... });
การทดสอบในท้องถิ่นกับ FakeModuleInstallClient
SDK บริการ Google Play มี FakeModuleInstallClient
ที่ให้คุณจําลองผลลัพธ์ของ API การติดตั้งโมดูลในการทดสอบโดยใช้การแทรกทรัพยากร Dependency
ข้อกําหนดเบื้องต้นของแอป
กําหนดค่าแอปให้ใช้ เฟรมเวิร์กการแทรกการอ้างอิง Dependency
แทนที่ ModuleInstallClient
ด้วย FakeModuleInstallClient
ในการทดสอบ
เพิ่มการอ้างอิง:
ในไฟล์บิวด์ Gradle ของโมดูล (มักจะเป็น
app/build.gradle
) ให้เพิ่มทรัพยากร Dependency ของบริการ Google Play สําหรับplay-services-base-testing
ในการทดสอบdependencies { // other dependencies... testImplementation 'com.google.android.gms:play-services-base-testing:16.0.0' }
สร้างโมดูล Hilt เพื่อระบุ
ModuleInstallClient
ดังนี้Kotlin
@Module @InstallIn(ActivityComponent::class) object ModuleInstallModule { @Provides fun provideModuleInstallClient( @ActivityContext context: Context ): ModuleInstallClient = ModuleInstall.getClient(context) }
Java
@Module @InstallIn(ActivityComponent.class) public class ModuleInstallModule { @Provides public static ModuleInstallClient provideModuleInstallClient( @ActivityContext Context context) { return ModuleInstall.getClient(context); } }
แทรก
ModuleInstallClient
ในกิจกรรม:Kotlin
@AndroidEntryPoint class MyActivity: AppCompatActivity() { @Inject lateinit var moduleInstallClient: ModuleInstallClient ... }
Java
@AndroidEntryPoint public class MyActivity extends AppCompatActivity { @Inject ModuleInstallClient moduleInstallClient; ... }
แทนที่การเชื่อมโยงในการทดสอบ:
Kotlin
@UninstallModules(ModuleInstallModule::class) @HiltAndroidTest class MyActivityTest { ... private val context:Context = ApplicationProvider.getApplicationContext() private val fakeModuleInstallClient = FakeModuleInstallClient(context) @BindValue @JvmField val moduleInstallClient: ModuleInstallClient = fakeModuleInstallClient ... }
Java
@UninstallModules(ModuleInstallModule.class) @HiltAndroidTest class MyActivityTest { ... private static final Context context = ApplicationProvider.getApplicationContext(); private final FakeModuleInstallClient fakeModuleInstallClient = new FakeModuleInstallClient(context); @BindValue ModuleInstallClient moduleInstallClient = fakeModuleInstallClient; ... }
จําลองความพร้อมใช้งานของโมดูล
Kotlin
@Test fun checkAvailability_available() { // Reset any previously installed modules. fakeModuleInstallClient.reset() val availableModule = TfLite.getClient(context) fakeModuleInstallClient.setInstalledModules(api) // Verify the case where modules are already available... } @Test fun checkAvailability_unavailable() { // Reset any previously installed modules. fakeModuleInstallClient.reset() // Do not set any installed modules in the test. // Verify the case where modules unavailable on device... } @Test fun checkAvailability_failed() { // Reset any previously installed modules. fakeModuleInstallClient.reset() fakeModuleInstallClient.setModulesAvailabilityTask(Tasks.forException(RuntimeException())) // Verify the case where an RuntimeException happened when trying to get module's availability... }
Java
@Test public void checkAvailability_available() { // Reset any previously installed modules. fakeModuleInstallClient.reset(); OptionalModuleApi optionalModuleApi = TfLite.getClient(context); fakeModuleInstallClient.setInstalledModules(api); // Verify the case where modules are already available... } @Test public void checkAvailability_unavailable() { // Reset any previously installed modules. fakeModuleInstallClient.reset(); // Do not set any installed modules in the test. // Verify the case where modules unavailable on device... } @Test public void checkAvailability_failed() { fakeModuleInstallClient.setModulesAvailabilityTask(Tasks.forException(new RuntimeException())); // Verify the case where an RuntimeException happened when trying to get module's availability... }
จําลองผลลัพธ์สําหรับคําขอการติดตั้งที่เลื่อนเวลา
Kotlin
@Test fun deferredInstall_success() { fakeModuleInstallClient.setDeferredInstallTask(Tasks.forResult(null)) // Verify the case where the deferred install request has been sent successfully... } @Test fun deferredInstall_failed() { fakeModuleInstallClient.setDeferredInstallTask(Tasks.forException(RuntimeException())) // Verify the case where an RuntimeException happened when trying to send the deferred install request... }
Java
@Test public void deferredInstall_success() { fakeModuleInstallClient.setDeferredInstallTask(Tasks.forResult(null)); // Verify the case where the deferred install request has been sent successfully... } @Test public void deferredInstall_failed() { fakeModuleInstallClient.setDeferredInstallTask(Tasks.forException(new RuntimeException())); // Verify the case where an RuntimeException happened when trying to send the deferred install request... }
จําลองผลลัพธ์สําหรับคําขอการติดตั้งเร่งด่วน
Kotlin
@Test fun installModules_alreadyExist() { // Reset any previously installed modules. fakeModuleInstallClient.reset(); OptionalModuleApi optionalModuleApi = TfLite.getClient(context); fakeModuleInstallClient.setInstalledModules(api); // Verify the case where the modules already exist when sending the install request... } @Test fun installModules_withoutListener() { // Reset any previously installed modules. fakeModuleInstallClient.reset(); // Verify the case where the urgent install request has been sent successfully... } @Test fun installModules_withListener() { // Reset any previously installed modules. fakeModuleInstallClient.reset(); // Generates a ModuleInstallResponse and set it as the result for installModules(). val moduleInstallResponse = FakeModuleInstallUtil.generateModuleInstallResponse() fakeModuleInstallClient.setInstallModulesTask(Tasks.forResult(moduleInstallResponse)) // Verify the case where the urgent install request has been sent successfully... // Generates some fake ModuleInstallStatusUpdate and send it to listener. val update = FakeModuleInstallUtil.createModuleInstallStatusUpdate( moduleInstallResponse.sessionId, STATE_COMPLETED) fakeModuleInstallClient.sendInstallUpdates(listOf(update)) // Verify the corresponding updates are handled correctly... } @Test fun installModules_failed() { fakeModuleInstallClient.setInstallModulesTask(Tasks.forException(RuntimeException())) // Verify the case where an RuntimeException happened when trying to send the urgent install request... }
Java
@Test public void installModules_alreadyExist() { // Reset any previously installed modules. fakeModuleInstallClient.reset(); OptionalModuleApi optionalModuleApi = TfLite.getClient(context); fakeModuleInstallClient.setInstalledModules(api); // Verify the case where the modules already exist when sending the install request... } @Test public void installModules_withoutListener() { // Reset any previously installed modules. fakeModuleInstallClient.reset(); // Verify the case where the urgent install request has been sent successfully... } @Test public void installModules_withListener() { // Reset any previously installed modules. fakeModuleInstallClient.reset(); // Generates a ModuleInstallResponse and set it as the result for installModules(). ModuleInstallResponse moduleInstallResponse = FakeModuleInstallUtil.generateModuleInstallResponse(); fakeModuleInstallClient.setInstallModulesTask(Tasks.forResult(moduleInstallResponse)); // Verify the case where the urgent install request has been sent successfully... // Generates some fake ModuleInstallStatusUpdate and send it to listener. ModuleInstallStatusUpdate update = FakeModuleInstallUtil.createModuleInstallStatusUpdate( moduleInstallResponse.getSessionId(), STATE_COMPLETED); fakeModuleInstallClient.sendInstallUpdates(ImmutableList.of(update)); // Verify the corresponding updates are handled correctly... } @Test public void installModules_failed() { fakeModuleInstallClient.setInstallModulesTask(Tasks.forException(new RuntimeException())); // Verify the case where an RuntimeException happened when trying to send the urgent install request... }