Google Play পরিষেবার ওভারভিউ নিবন্ধে যেমন বর্ণনা করা হয়েছে, Google Play পরিষেবা দ্বারা পরিচালিত SDK, Google-সার্টিফায়েড Android ডিভাইসে অন-ডিভাইস পরিষেবা দ্বারা সমর্থিত। সব ডিভাইসের স্টোরেজ ও মেমরি বাঁচানোর জন্য, কিছু পরিষেবা মডিউল হিসেবে প্রদান করা হয় যা আপনার অ্যাপের প্রাসঙ্গিক কার্যকারিতার প্রয়োজন হলে চাহিদা অনুযায়ী ইনস্টল করা হয়। যেমন, Google Play পরিষেবায় মডেল ব্যবহার করার সময় ML Kit এই বিকল্প প্রদান করে।
বেশিরভাগ ক্ষেত্রে, আপনার অ্যাপ এমন কোনও API ব্যবহার করলে যার জন্য প্রয়োজনীয় মডিউল দরকার, সেক্ষেত্রে Google Play পরিষেবা SDK অটোমেটিক তা ডাউনলোড ও ইনস্টল করে। তবে, আপনি হয়ত প্রসেসের উপর আরও বেশি নিয়ন্ত্রণ রাখতে চাইবেন, যেমন, আপনি যদি আগে থেকে মডিউল ইনস্টল করে ব্যবহারকারীর অভিজ্ঞতা উন্নত করতে চান।
ModuleInstallClient API আপনাকে এগুলি করার ক্ষমতা দেয়:
- ডিভাইসে মডিউলগুলি আগে থেকেই ইনস্টল করা আছে কিনা তা চেক করুন।
- মডিউল ইনস্টল করার অনুরোধ।
- ইনস্টলেশন প্রোগ্রেস মনিটর করুন।
- ইনস্টলেশন প্রসেস চলাকালীন হওয়া সমস্যার সমাধান করা।
এই নির্দেশিকা আপনাকে দেখায় যে কীভাবে আপনার অ্যাপে মডিউল ম্যানেজ করতে ModuleInstallClient ব্যবহার করবেন। মনে রাখবেন যে নিম্নলিখিত কোড স্নিপেটগুলি উদাহরণ হিসাবে
TensorFlow Lite SDK
(play-services-tflite-java) ব্যবহার করে, তবে এই ধাপগুলি OptionalModuleApi-এর সাথে ইন্টিগ্রেট করা যেকোনও লাইব্রেরির জন্য প্রযোজ্য।
শুরু করার আগে
আপনার অ্যাপ প্রস্তুত করতে, নিম্নলিখিত বিভাগে উল্লেখ করা ধাপগুলি সম্পূর্ণ করুন।
অ্যাপের পূর্বশর্ত
আপনার অ্যাপের বিল্ড ফাইল নিম্নলিখিত ভ্যালু ব্যবহার করছে কিনা তা নিশ্চিত করুন:
23বা তার বেশিminSdkVersion
আপনার অ্যাপ কনফিগার করুন
আপনার টপ-লেভেল
settings.gradleফাইলে, Google-এর Maven রিপোজিটরি এবং Maven সেন্ট্রাল রিপোজিটরি-কেdependencyResolutionManagementব্লকের মধ্যে অন্তর্ভুক্ত করুন:dependencyResolutionManagement { repositories { google() mavenCentral() } }আপনার মডিউলের Gradle বিল্ড ফাইলে (সাধারণত
app/build.gradle),play-services-baseএবংplay-services-tflite-java-এর জন্য Google Play পরিষেবার ডিপেন্ডেন্সি যোগ করুন:dependencies { implementation 'com.google.android.gms:play-services-base:18.11.0' implementation 'com.google.android.gms:play-services-tflite-java:16.5.0' }
মডিউল উপলভ্য আছে কিনা চেক করুন
কোনও মডিউল ইনস্টল করার চেষ্টা করার আগে, আপনি চেক করে দেখতে পারেন যে এটি ডিভাইসে আগে থেকেই ইনস্টল করা আছে কিনা। এটি আপনাকে অপ্রয়োজনীয় ইনস্টলেশন অনুরোধ এড়াতে সাহায্য করে।
ModuleInstallClient-এর একটি ইনস্ট্যান্স পান:Kotlin
val moduleInstallClient = ModuleInstall.getClient(context)
Java
ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(context);
মডিউলের
OptionalModuleApiব্যবহার করে এর উপলভ্যতা চেক করুন। আপনি যে Google Play পরিষেবা SDK ব্যবহার করছেন, এটি সেই SDK-এর মাধ্যমে প্রদান করা হয়।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… });
বিলম্বিত ইনস্টলের অনুরোধ করা
আপনার যদি অবিলম্বে মডিউলটির প্রয়োজন না হয়, তাহলে আপনি একটি ডিফার্ড ইনস্টলের অনুরোধ করতে পারেন। এর ফলে Google Play পরিষেবা ব্যাকগ্রাউন্ডে মডিউল ইনস্টল করতে পারে, সম্ভাব্যভাবে ডিভাইসটি যখন ওয়াই-ফাইয়ের সাথে কানেক্ট করা থাকে এবং কোনও কাজ করে না।
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 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 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. } // The install request has been sent successfully. This does not mean // the installation is completed. To monitor the install status, set an // InstallStatusListener to the ModuleInstallRequest. } .addOnFailureListener { // Handle failure… }
Java
moduleInstallClient.installModules(moduleInstallRequest) .addOnSuccessListener( response -> { if (response.areModulesAlreadyInstalled()) { // Modules are already installed when the request is sent. } // The install request has been sent successfully. This does not // mean the installation is completed. To monitor the install // status, set an InstallStatusListener to the // ModuleInstallRequest. }) .addOnFailureListener( e -> { // Handle failure... });
FakeModuleInstallClient-এর মাধ্যমে আপনার অ্যাপ পরীক্ষা করুন
Google Play পরিষেবা SDK, আপনাকে মডিউল ইনস্টল API-এর ফলাফল সিমুলেট করার অনুমতি দিতে FakeModuleInstallClient প্রদান করে।
ডিপেন্ডেন্সি ইনজেকশন ব্যবহার করে টেস্টে এটি করা হয়।
এটি আপনাকে বিভিন্ন পরিস্থিতিতে
আপনার অ্যাপের আচরণ পরীক্ষা করতে সাহায্য করে, এর জন্য আপনাকে কোনও আসল ডিভাইসে এটি ডিপ্লয় করতে হবে না।
অ্যাপের পূর্বশর্ত
Hilt ডিপেন্ডেন্সি ইনজেকশন ফ্রেমওয়ার্ক ব্যবহার করার জন্য আপনার অ্যাপ কনফিগার করুন।
পরীক্ষায় ModuleInstallClient-এর জায়গায় FakeModuleInstallClient ব্যবহার করুন
আপনার পরীক্ষায় FakeModuleInstallClient ব্যবহার করতে, আপনাকে নকল ইমপ্লিমেন্টেশনের মাধ্যমে
ModuleInstallClient বাইন্ডিং পরিবর্তন করতে হবে।
ডিপেন্ডেন্সি যোগ করুন:
আপনার মডিউলের Gradle বিল্ড ফাইলে (সাধারণত
app/build.gradle), আপনার পরীক্ষার জন্যplay-services-base-testing-এর Google Play পরিষেবা সংক্রান্ত নির্ভরতা যোগ করুন।dependencies { // other dependencies... testImplementation 'com.google.android.gms:play-services-base-testing:16.2.0' }ModuleInstallClientপ্রদান করতে Hilt মডিউল তৈরি করুন: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; ... }
বিভিন্ন পরিস্থিতি সিমুলেট করা
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... }