AMAPI की मदद से कस्टम ऐप्लिकेशन मैनेज करना

Android Management API पर आधारित EMM के तौर पर, आपके पास डिवाइसों पर कस्टम ऐप्लिकेशन को दूर से मैनेज करने का विकल्प होता है. इसमें इन ऐप्लिकेशन को इंस्टॉल और अनइंस्टॉल करना, दोनों शामिल हैं. यह सुविधा, AMAPI SDK का इस्तेमाल करके, स्थानीय तौर पर एक्सटेंशन ऐप्लिकेशन डेवलप करके हासिल की जाती है.

ज़रूरी शर्तें

1. सुविधा का इस्तेमाल करने के लिए, ऐप्लिकेशन तैयार करना

1.1. अपने एक्सटेंशन ऐप्लिकेशन में AMAPI SDK टूल के साथ इंटिग्रेट करें

कस्टम ऐप्लिकेशन मैनेजमेंट की प्रोसेस के लिए, आपको अपने एक्सटेंशन ऐप्लिकेशन में AMAPI SDK टूल को इंटिग्रेट करना होगा. इस लाइब्रेरी और इसे अपने ऐप्लिकेशन में जोड़ने के तरीके के बारे में ज़्यादा जानने के लिए, AMAPI SDK टूल को इंटिग्रेट करने से जुड़ी गाइड पढ़ें.

1.2. FileProvider के साथ काम करने के लिए, अपने ऐप्लिकेशन के मेनिफ़ेस्ट को अपडेट करें

  • AMAPI SDK इंटिग्रेशन गाइड में दिखाए गए तरीके से, Android डिवाइस की नीति (एडीपी) लागू करने वाले ऐप्लिकेशन के लिए, अपने AndroidManifest.xml में <queries> एलिमेंट जोड़ें.
  • अपने ऐप्लिकेशन के AndroidManifest.xml में <application> टैग के अंदर, यहां दिया गया <provider> स्निपेट लागू करें. इस स्निपेट का इस्तेमाल, कस्टम ऐप्लिकेशन का APK शेयर करते समय फ़ाइलें सेव करने के लिए किया जाता है. इससे AMAPI का इस्तेमाल करके कस्टम ऐप्लिकेशन इंस्टॉल किए जा सकते हैं.

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.customapp">
  <queries>
    <package android:name="com.google.android.apps.work.clouddpc" />
  </queries>

  <application>

    <!--This is used to store files when sharing the custom app apk.-->
    <provider
        android:name="com.google.android.managementapi.customapp.provider.CustomAppProvider"
        android:authorities="${applicationId}.AmapiCustomAppProvider"
        android:exported="false"
        android:grantUriPermissions="true">
      <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/file_provider_paths" />
    </provider>
  </application>
</manifest>
  • अपने ऐप्लिकेशन की res/xml/ डायरेक्ट्री में एक नई एक्सएमएल फ़ाइल बनाएं. इसमें कस्टम APK के लिए स्टोरेज पाथ शामिल होना चाहिए.

file_provider_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <cache-path
      name="android_managementapi_custom_apks"
      path="com.google.android.managementapi/customapp/apks/" />
</paths>

2. AMAPI SDK टूल की कस्टम ऐप्लिकेशन सुविधा के साथ इंटिग्रेट करना

2.1. इंस्टॉल करने के लिए कस्टम APK फ़ाइल तैयार करना

डप्लॉय करने से पहले, ऐप्लिकेशन की APK फ़ाइल को इंस्टॉल करने के लिए तैयार किया जाना चाहिए. यहां दिया गया कोड स्निपेट, इस प्रोसेस के बारे में बताता है:

Kotlin

import android.net.Uri
import androidx.core.net.Uri
import java.io.File
...
import com.google.android.managementapi.commands.LocalCommandClient
import com.google.android.managementapi.commands.LocalCommandClient.InstallCustomAppCommandHelper
import com.google.android.managementapi.commands.LocalCommandClientFactory

...

fun prepareApkFile(): Uri? {
    // Get the storage location of custom APK files from AM API
    val client: LocalCommandClient = LocalCommandClientFactory.create(context)
    val installCustomAppCommandHelper = client.installCustomAppCommandHelper

    val customApksStorageDir: File = installCustomAppCommandHelper.customApksStorageDirectory ?: return null

    // Once you get hold of the custom APKs storage directory, you must store your custom APK
    // in that location before issuing the install command.
    val customApkFile: File = fetchMyAppToDir(customApksStorageDir) ?: return null
    val customApkFileUri: Uri = customApkFile.toUri()

    return customApkFileUri
}

Java

import android.net.Uri;
import androidx.core.net.Uri;
import java.io.File;
...
import com.google.android.managementapi.commands.LocalCommandClient;
import com.google.android.managementapi.commands.LocalCommandClient.InstallCustomAppCommandHelper;
import com.google.android.managementapi.commands.LocalCommandClientFactory;

...

Uri prepareApkFile() {
  // Get the storage location of custom APK files from AM API
  LocalCommandClient client = LocalCommandClientFactory.create();
  InstallCustomAppCommandHelper installCustomAppCommandHelper = client.getInstallCustomAppCommandHelper();
  File customApksStorageDir = installCustomAppCommandHelper.getCustomApksStorageDirectory();

  // Once you get hold of the custom APKs storage directory, you must store your custom APK
  // in that location before issuing the install command.
  File customApkFile = fetchMyAppToDir(customApksStorageDir);
  Uri customApkFileUri = Uri.fromFile(customApkFile);
  ...
}

2.2. कस्टम ऐप्लिकेशन इंस्टॉल करने का अनुरोध करना

नीचे दिए गए स्निपेट में, कस्टम ऐप्लिकेशन इंस्टॉल करने का अनुरोध करने का तरीका बताया गया है:

Kotlin

import android.content.Context
import android.net.Uri
import android.util.Log
import com.google.android.managementapi.commands.LocalCommandClientFactory
import com.google.android.managementapi.commands.model.Command
import com.google.android.managementapi.commands.model.IssueCommandRequest
import com.google.android.managementapi.commands.model.IssueCommandRequest.InstallCustomApp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.guava.await
import kotlinx.coroutines.withContext
import java.lang.Exception

private const val TAG = "MyClass"

...

    // Requires a file URI of the APK file.
    fun issueInstallCustomAppCommand(packageName: String, fileUri: Uri) {
        coroutineScope.launch {
            try {
                withContext(coroutineScope.coroutineContext) {
                    val result: Command = LocalCommandClientFactory.create(context)
                        .issueCommand(createInstallCustomAppRequest(packageName, fileUri)).await()
                    // Process the returned command result here.
                    Log.i(TAG, "Successfully issued command: $result")
                }
            } catch (t: Exception) {
                Log.e(TAG, "Failed to issue command", t)
                // Handle the exception (e.g., show an error message)
            } finally {
                // Make sure to clean up the apk file after the command is executed.
                cleanUpApkFile(fileUri)
            }
        }
    }

    private fun createInstallCustomAppRequest(packageName: String, fileUri: Uri): IssueCommandRequest {
        return IssueCommandRequest.builder()
            .setInstallCustomApp(
                InstallCustomApp.builder()
                    .setPackageName(packageName)
                    .setPackageUri(fileUri.toString())
                    .build()
            )
            .build()
    }
}

Java

import android.util.Log;
...
import com.google.android.managementapi.commands.LocalCommandClientFactory;
import com.google.android.managementapi.commands.model.Command;
import com.google.android.managementapi.commands.model.GetCommandRequest;
import com.google.android.managementapi.commands.model.IssueCommandRequest;
import com.google.android.managementapi.commands.model.IssueCommandRequest.ClearAppsData;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;

...

  // Requires a file URI of the APK file.
  void issueInstallCustomAppCommand(String packageName, Uri fileUri) {
    Futures.addCallback(
        LocalCommandClientFactory.create(getContext())
            .issueCommand(createInstallCustomAppRequest(packageName, fileUri)),
        new FutureCallback() {
          @Override
          public void onSuccess(Command result) {
            // Process the returned command result here.
            Log.i(TAG, "Successfully issued command");
          }

          @Override
          public void onFailure(Throwable t) {
            Log.e(TAG, "Failed to issue command", t);
          }
        },
        MoreExecutors.directExecutor());
  }

  IssueCommandRequest createInstallCustomAppRequest(String packageName, Uri fileUri) {
    return IssueCommandRequest.builder()
        .setInstallCustomApp(
            InstallCustomApp.builder()
                .setPackageName(packageName)
                .setPackageUri(fileUri.toString())
                .build()
        )
        .build();
  }

2.3. इंस्टॉल किए गए ऐप्लिकेशन की जानकारी पाने का अनुरोध करना

Kotlin

import android.content.Context
import com.google.android.managementapi.device.DeviceClientFactory
import com.google.android.managementapi.device.model.GetDeviceRequest
import kotlinx.coroutines.guava.await

  suspend fun getInstalledApps(context: Context) =
    DeviceClientFactory.create(context)
      .getDevice(GetDeviceRequest.getDefaultInstance())
      .await()
      .getApplicationReports()

Java

import android.content.Context;
import com.google.android.managementapi.device.DeviceClientFactory;
import com.google.android.managementapi.device.model.GetDeviceRequest;
import com.google.android.managementapi.device.model.Device;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.List;
import java.util.concurrent.Executor;

public ListenableFuture<List> getInstalledApps() {
        ListenableFuture deviceFuture =
            DeviceClientFactory.create(context)
                .getDevice(GetDeviceRequest.getDefaultInstance());

        return Futures.transform(
            deviceFuture,
            Device::getApplicationReports,
            executor // Use the provided executor
        );
    }

3. डिवाइस पर, ऐप्लिकेशन मैनेज करने की कस्टम नीतियां लागू करना

  1. उन कस्टम ऐप्लिकेशन के साथ policy सेट अप करें जिन्हें आपको मैनेज करना है.

      {
        "statusReportingSettings": {
          "applicationReportsEnabled": true
        },
        "applications": [
        {
          "signingKeyCerts": [
            {
            "signingKeyCertFingerprintSha256": <sha256 signing key certificate hash value>
            }
          ],
          "packageName": "<emm_extensibility_app>",
          "installType": "AVAILABLE",
          "lockTaskAllowed": true,
          "defaultPermissionPolicy": "GRANT",
          "extensionConfig": {
              "notificationReceiver": "com.example.customapp.NotificationReceiverService"
          }
        },
        {
          "signingKeyCerts": [
            {
            "signingKeyCertFingerprintSha256": <sha256 signing key certificate hash value>
            },
          ],
          "packageName": "<custom_app>",
          "installType": "CUSTOM",
          "lockTaskAllowed": true,
          "defaultPermissionPolicy": "GRANT",
          "customAppConfig": {
        "userUninstallSettings": "DISALLOW_UNINSTALL_BY_USER"
          }
        }
        ]
      }
      ```
    
  2. डिवाइस के लिए रजिस्ट्रेशन टोकन बनाएं. इसके लिए, enterprises.enrollmentTokens.create को कॉल करें. साथ ही, allowPersonalUsage को PERSONAL_USAGE_DISALLOWED पर सेट करें.

  3. रजिस्ट्रेशन टोकन की मदद से, डिवाइस को पूरी तरह से मैनेज की गई सेवा मोड में प्रोविज़न करें.

  4. Managed Play से एक्सटेंशन की सुविधा देने वाला ऐप्लिकेशन इंस्टॉल करें.

  5. आपका एक्सटेंसिबिलिटी ऐप्लिकेशन:

    • कस्टम ऐप्लिकेशन की APK फ़ाइल डाउनलोड कर सकता है
    • कस्टम ऐप्लिकेशन इंस्टॉल करने का अनुरोध कर सकता है (पहले दिखाए गए कोड स्निपेट देखें)
    • जवाब मिलना चाहिए

एपीआई

सर्वर-क्लाइंट एपीआई

यहां दिए गए नए फ़ील्ड और enum देखें: