moduleInstallClient के साथ एपीआई की उपलब्धता पक्का करना

जैसा कि Google Play services की खास जानकारी लेख में बताया गया है, Google Play services के साथ काम करने वाले SDK टूल, Google से प्रमाणित Android डिवाइसों पर, डिवाइस पर उपलब्ध सेवाओं के साथ काम करते हैं. सभी डिवाइसों में स्टोरेज और मेमोरी बचाने के लिए, कुछ सेवाएं मांग पर इंस्टॉल की जाती हैं. ऐसा तब होता है, जब यह पता चलता है कि किसी खास डिवाइस के लिए ज़रूरी फ़ंक्शन की ज़रूरत है. उदाहरण के लिए, Google Play services में मॉडल इस्तेमाल करते समय, एमएल किट में यह विकल्प मिलता है.

आम तौर पर, किसी सेवा (या “मॉड्यूल”) का डाउनलोड किया जाना और उस ऐप्लिकेशन के साथ-साथ इंस्टॉल होना सबसे ज़्यादा होता है जिसके लिए यह ज़रूरी है. यह SDK टूल के AndroidManifest.xml में डिपेंडेंसी के हिसाब से तय होता है. ज़्यादा कंट्रोल के लिए, मॉड्यूल इंस्टॉल एपीआई से साफ़ तौर पर मॉड्यूल की उपलब्धता की जांच की जा सकती है, मॉड्यूल इंस्टॉल करने का अनुरोध किया जा सकता है, अनुरोध की स्थिति पर नज़र रखी जा सकती है, और गड़बड़ियों को ठीक किया जा सकता है.

यह पक्का करने के लिए कि ModuleInstallClient के साथ एपीआई की उपलब्धता है, नीचे दिया गया तरीका अपनाएं. ध्यान रखें कि नीचे दिए गए कोड स्निपेट, उदाहरण के तौर पर लाइब्रेरी के तौर पर TensorFlow Lite SDK (play-services-tflite-java) का इस्तेमाल करते हैं. हालांकि, OptionalModuleApi के साथ इंटिग्रेट की गई किसी भी लाइब्रेरी के लिए यह तरीका अपनाया जा सकता है. जैसे-जैसे ज़्यादा SDK टूल की सुविधा बढ़ती जाएगी वैसे-वैसे इस गाइड में और जानकारी अपडेट की जाएगी.

शुरू करने से पहले

अपना ऐप्लिकेशन तैयार करने के लिए, नीचे दिए गए सेक्शन में दिए गए चरणों को पूरा करें.

ऐप्लिकेशन से जुड़ी ज़रूरी शर्तें

पक्का करें कि आपके ऐप्लिकेशन की बिल्ड फ़ाइल इन वैल्यू का इस्तेमाल करती हो:

  • 19 या इससे ज़्यादा का minSdkVersion

अपने ऐप्लिकेशन को कॉन्फ़िगर करें

  1. अपनी टॉप-लेवल settings.gradle फ़ाइल में, Google की Maven रिपॉज़िटरी और dependencyResolutionManagement ब्लॉक में Maven सेंट्रल रिपॉज़िटरी शामिल करें:

    dependencyResolutionManagement {
        repositories {
            google()
            mavenCentral()
        }
    }
    
  2. अपने मॉड्यूल की Gradle बिल्ड फ़ाइल (आम तौर पर app/build.gradle) में, play-services-base और play-services-tflite-java के लिए, Google Play services डिपेंडेंसी जोड़ें:

    dependencies {
      implementation 'com.google.android.gms:play-services-base:18.3.0'
      implementation 'com.google.android.gms:play-services-tflite-java:16.2.0-beta02'
    }
    

मॉड्यूल की उपलब्धता देखें

  1. ModuleInstallClient का एक इंस्टेंस पाएं:

    Kotlin

    val moduleInstallClient = ModuleInstall.getClient(context)
    

    Java

    ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(context);
    
  2. वैकल्पिक मॉड्यूल की उपलब्धता देखने के लिए, 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…
            });
    

स्थगित इंस्टॉल करने का अनुरोध भेजना

  1. ModuleInstallClient का एक इंस्टेंस पाएं:

    Kotlin

    val moduleInstallClient = ModuleInstall.getClient(context)
    

    Java

    ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(context);
    
  2. स्थगित अनुरोध भेजें:

    Kotlin

    val optionalModuleApi = TfLite.getClient(context)
    moduleInstallClient.deferredInstall(optionalModuleApi)
    

    Java

    OptionalModuleApi optionalModuleApi = TfLite.getClient(context);
    moduleInstallClient.deferredInstall(optionalModuleApi);
    

मॉड्यूल इंस्टॉल करने के लिए ज़रूरी अनुरोध भेजें

  1. ModuleInstallClient का एक इंस्टेंस पाएं:

    Kotlin

    val moduleInstallClient = ModuleInstall.getClient(context)
    

    Java

    ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(context);
    
  2. (ज़रूरी नहीं) इंस्टॉल की स्थिति से जुड़े अपडेट मैनेज करने के लिए, InstallStatusListener बनाएं.

    अगर आपको पसंद के मुताबिक बनाए गए यूज़र इंटरफ़ेस (जैसे, प्रोग्रेस बार) में डाउनलोड की प्रोग्रेस पर नज़र रखनी है, तो इंस्टॉल की स्थिति से जुड़े अपडेट पाने के लिए 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();
    
  3. 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();
    
  4. इंस्टॉल करने का अनुरोध भेजें:

    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 की मदद से स्थानीय तौर पर टेस्ट किया जा रहा है

Google Play services SDK टूल में FakeModuleInstallClient मौजूद होता है, ताकि आप डिपेंडेंसी इंजेक्शन का इस्तेमाल करके, मॉड्यूल इंस्टॉल एपीआई के नतीजों को सिम्युलेट कर सकें.

ऐप्लिकेशन से जुड़ी ज़रूरी शर्तें

हिल्ट डिपेंडेंसी इंजेक्शन फ़्रेमवर्क का इस्तेमाल करने के लिए, अपने ऐप्लिकेशन को कॉन्फ़िगर करें.

टेस्ट में, ModuleInstallClient को FakeModuleInstallClient से बदलें

  1. डिपेंडेंसी जोड़ें:

    अपने मॉड्यूल की Gradle बिल्ड फ़ाइल (आम तौर पर app/build.gradle) में, अपने टेस्ट में play-services-base-testing के लिए Google Play services डिपेंडेंसी जोड़ें.

      dependencies {
        // other dependencies...
    
        testImplementation 'com.google.android.gms:play-services-base-testing:16.0.0'
      }
    
  2. 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);
      }
    }
    
  3. गतिविधि में ModuleInstallClient इंजेक्ट करें:

    Kotlin

    @AndroidEntryPoint
    class MyActivity: AppCompatActivity() {
      @Inject lateinit var moduleInstallClient: ModuleInstallClient
    
      ...
    }
    

    Java

    @AndroidEntryPoint
    public class MyActivity extends AppCompatActivity {
      @Inject ModuleInstallClient moduleInstallClient;
    
      ...
    }
    
  4. टेस्ट में बाइंडिंग को बदलें:

    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...
}