ऐसेट डिलीवरी (नेटिव) को इंटिग्रेट करना

अपने C और C++ ऐप्लिकेशन के ऐसेट पैक ऐक्सेस करने के लिए, इस गाइड में दिया गया तरीका अपनाएं कोड.

सैंपल इंटिग्रेशन कोड GitHub पर उपलब्ध है.

नेटिव के लिए बनाएं

अपने प्रोजेक्ट के Android में, 'Play ऐसेट डिलीवरी' की सुविधा बनाने के लिए, यह तरीका अपनाएं ऐप्लिकेशन बंडल. इन चरणों को पूरा करने के लिए, आपको Android Studio का इस्तेमाल करने की ज़रूरत नहीं है.

  1. अपने प्रोजेक्ट में 'Android Gradle प्लग इन' का वर्शन अपडेट करें build.gradle फ़ाइल को 4.0.0 या उसके बाद के वर्शन पर अपलोड करें.

  2. अपने प्रोजेक्ट की टॉप-लेवल की डायरेक्ट्री में, ऐसेट के लिए एक डायरेक्ट्री बनाएं पैक. डायरेक्ट्री के इस नाम का इस्तेमाल, ऐसेट पैक के नाम के तौर पर किया जाता है. ऐसेट पैक के नाम यह किसी अक्षर से शुरू होना चाहिए और इसमें सिर्फ़ अक्षर, अंक, और अंडरस्कोर.

  3. ऐसेट पैक की डायरेक्ट्री में, build.gradle फ़ाइल बनाएं और . पक्का करें कि आपने ऐसेट पैक का नाम और सिर्फ़ एक नाम डाला हो डिलीवरी टाइप:

    // In the asset pack’s build.gradle file:
    plugins {
        id 'com.android.asset-pack'
    }
    
    assetPack {
        packName = "asset-pack-name" // Directory name for the asset pack
        dynamicDelivery {
            deliveryType = "[ install-time | fast-follow | on-demand ]"
        }
    }
    
  4. प्रोजेक्ट के ऐप्लिकेशन की build.gradle फ़ाइल में, हर ऐसेट पैक का नाम जोड़ें जोड़ें, जैसा कि नीचे दिखाया गया है:

    // In the app build.gradle file:
    android {
        ...
        assetPacks = [":asset-pack-name", ":asset-pack2-name"]
    }
    
  5. प्रोजेक्ट की settings.gradle फ़ाइल में, सभी ऐसेट पैक को प्रोजेक्ट देखें, जैसा कि नीचे दिखाया गया है:

    // In the settings.gradle file:
    include ':app'
    include ':asset-pack-name'
    include ':asset-pack2-name'
    
  6. ऐसेट पैक की डायरेक्ट्री में, यह सबडायरेक्ट्री बनाएं: src/main/assets.

  7. एसेट को src/main/assets डायरेक्ट्री में रखें. आप बना सकते हैं सबडायरेक्ट्री में भी शामिल होंगी. आपके ऐप्लिकेशन की डायरेक्ट्री का स्ट्रक्चर अब ऐसा दिखेगा:

    • build.gradle
    • settings.gradle
    • app/
    • asset-pack-name/build.gradle
    • asset-pack-name/src/main/assets/your-asset-directories
  8. Gradle की मदद से Android ऐप्लिकेशन बंडल बनाएं. जनरेट किए गए ऐप्लिकेशन बंडल में, रूट-लेवल की डायरेक्ट्री में अब फ़ॉलो किया जा रहा है:

    • asset-pack-name/manifest/AndroidManifest.xml: ऐसेट पैक का आइडेंटिफ़ायर और डिलीवरी मोड कॉन्फ़िगर करती है
    • asset-pack-name/assets/your-asset-directories: वह डायरेक्ट्री जिसमें ऐसेट पैक के हिस्से के तौर पर डिलीवर की गई सभी ऐसेट शामिल हैं

    Gradle, हर ऐसेट पैक के लिए मेनिफ़ेस्ट जनरेट करता है और assets/ को जनरेट करता है डायरेक्ट्री मिलेगी.

  9. (ज़रूरी नहीं) अलग-अलग टेक्सचर के साथ काम करने के लिए, अपने ऐप्लिकेशन बंडल को कॉन्फ़िगर करें कंप्रेशन फ़ॉर्मैट में बनाए जा सकते हैं.

'Play ऐसेट डिलीवरी लाइब्रेरी' के साथ इंटिग्रेट करना

इस एपीआई को इस हिसाब से लागू किया जाता है कि डिलीवरी किस तरह की है आपको जिस ऐसेट पैक को ऐक्सेस करना है. यह तरीका यहां दिया गया है फ़्लोचार्ट.

नेटिव कोड के लिए ऐसेट पैक फ़्लो का डायग्राम

पहला डायग्राम. ऐसेट पैक ऐक्सेस करने का फ़्लो डायग्राम

Play Core नेटिव SDK टूल, C हेडर उपलब्ध कराता है एसेट पैक का अनुरोध करने, डाउनलोड मैनेज करने, औरplay/asset_pack.h ऐसेट ऐक्सेस करता है.

Play Core नेटिव SDK टूल के लिए, अपना डेवलपमेंट एनवायरमेंट सेट अप करें

Download Play Core Native SDK

Before downloading, you must agree to the following terms and conditions.

Terms and Conditions

Last modified: September 24, 2020
  1. By using the Play Core Software Development Kit, you agree to these terms in addition to the Google APIs Terms of Service ("API ToS"). If these terms are ever in conflict, these terms will take precedence over the API ToS. Please read these terms and the API ToS carefully.
  2. For purposes of these terms, "APIs" means Google's APIs, other developer services, and associated software, including any Redistributable Code.
  3. “Redistributable Code” means Google-provided object code or header files that call the APIs.
  4. Subject to these terms and the terms of the API ToS, you may copy and distribute Redistributable Code solely for inclusion as part of your API Client. Google and its licensors own all right, title and interest, including any and all intellectual property and other proprietary rights, in and to Redistributable Code. You will not modify, translate, or create derivative works of Redistributable Code.
  5. Google may make changes to these terms at any time with notice and the opportunity to decline further use of the Play Core Software Development Kit. Google will post notice of modifications to the terms at https://developer.android.com/guide/playcore/license. Changes will not be retroactive.
Download Play Core Native SDK

play-core-native-sdk-1.14.0.zip

  1. इनमें से कोई एक काम करें:

    • Android Studio का 4.0 या इसके बाद वाला वर्शन इंस्टॉल करें. SDK टूल का इस्तेमाल करना Android SDK प्लैटफ़ॉर्म के वर्शन 10.0 (एपीआई लेवल 29) को इंस्टॉल करने के लिए, मैनेजर के यूज़र इंटरफ़ेस (यूआई) की इमेज.
    • Android SDK टूल के कमांड-लाइन टूल इंस्टॉल करें और इसे इंस्टॉल करने के लिए sdkmanager का इस्तेमाल करें Android SDK प्लैटफ़ॉर्म का वर्शन 10.0 (एपीआई लेवल 29).
  2. इनका इस्तेमाल करके, अपने ऐप्लिकेशन को बेहतर बनाने के लिए Android Studio को तैयार करें नया वर्शन इंस्टॉल करने के लिए, SDK Manager CMake और Android नेटिव डेवलपमेंट किट (NDK). अगर आपको ज़्यादा जानकारी चाहिए, तो नेटिव प्रोजेक्ट बनाना या इंपोर्ट करना, तो एनडीके (NDK) के साथ शुरुआत करना.

  3. ZIP फ़ाइल डाउनलोड करें और उसे अपने प्रोजेक्ट के साथ एक्सट्रैक्ट करें.

    लिंक डाउनलोड करें साइज़ SHA-256 चेकसम
    36 एमआईबी 782a8522d937848c83a715c9a258b95a3ff2879a7cd71855d137b41c00786a5e
  4. नीचे बताए गए तरीके से अपने ऐप्लिकेशन की build.gradle फ़ाइल अपडेट करें:

    ग्रूवी

        // App build.gradle
    
        plugins {
          id 'com.android.application'
        }
    
        // Define a path to the extracted Play Core SDK files.
        // If using a relative path, wrap it with file() since CMake requires absolute paths.
        def playcoreDir = file('../path/to/playcore-native-sdk')
    
        android {
            defaultConfig {
                ...
                externalNativeBuild {
                    cmake {
                        // Define the PLAYCORE_LOCATION directive.
                        arguments "-DANDROID_STL=c++_static",
                                  "-DPLAYCORE_LOCATION=$playcoreDir"
                    }
                }
                ndk {
                    // Skip deprecated ABIs. Only required when using NDK 16 or earlier.
                    abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
                }
            }
            buildTypes {
                release {
                    // Include Play Core Library proguard config files to strip unused code while retaining the Java symbols needed for JNI.
                    proguardFile '$playcoreDir/proguard/common.pgcfg'
                    proguardFile '$playcoreDir/proguard/gms_task.pgcfg'
                    proguardFile '$playcoreDir/proguard/per-feature-proguard-files'
                    ...
                }
                debug {
                    ...
                }
            }
            externalNativeBuild {
                cmake {
                    path 'src/main/CMakeLists.txt'
                }
            }
        }
    
        dependencies {
            // Import these feature-specific AARs for each Google Play Core library.
            implementation 'com.google.android.play:app-update:2.1.0'
            implementation 'com.google.android.play:asset-delivery:2.2.2'
            implementation 'com.google.android.play:integrity:1.4.0'
            implementation 'com.google.android.play:review:2.0.1'
    
            // Import these common dependencies.
            implementation 'com.google.android.gms:play-services-tasks:18.0.2'
            implementation files("$playcoreDir/playcore-native-metadata.jar")
            ...
        }
        

    Kotlin

    // App build.gradle
    
    plugins {
        id("com.android.application")
    }
    
    // Define a path to the extracted Play Core SDK files.
    // If using a relative path, wrap it with file() since CMake requires absolute paths.
    val playcoreDir = file("../path/to/playcore-native-sdk")
    
    android {
        defaultConfig {
            ...
            externalNativeBuild {
                cmake {
                    // Define the PLAYCORE_LOCATION directive.
                    arguments += listOf("-DANDROID_STL=c++_static", "-DPLAYCORE_LOCATION=$playcoreDir")
                }
            }
            ndk {
                // Skip deprecated ABIs. Only required when using NDK 16 or earlier.
                abiFilters.clear()
                abiFilters += listOf("armeabi-v7a", "arm64-v8a", "x86", "x86_64")
            }
        }
        buildTypes {
            release {
                // Include Play Core Library proguard config files to strip unused code while retaining the Java symbols needed for JNI.
                proguardFile("$playcoreDir/proguard/common.pgcfg")
                proguardFile("$playcoreDir/proguard/gms_task.pgcfg")
                proguardFile("$playcoreDir/proguard/per-feature-proguard-files")
                ...
            }
            debug {
                ...
            }
        }
        externalNativeBuild {
            cmake {
                path = "src/main/CMakeLists.txt"
            }
        }
    }
    
    dependencies {
        // Import these feature-specific AARs for each Google Play Core library.
        implementation("com.google.android.play:app-update:2.1.0")
        implementation("com.google.android.play:asset-delivery:2.2.2")
        implementation("com.google.android.play:integrity:1.4.0")
        implementation("com.google.android.play:review:2.0.1")
    
        // Import these common dependencies.
        implementation("com.google.android.gms:play-services-tasks:18.0.2")
        implementation(files("$playcoreDir/playcore-native-metadata.jar"))
        ...
    }
    
  5. अपने ऐप्लिकेशन की CMakeLists.txt फ़ाइलों को अपडेट करने का तरीका नीचे दिया गया है:

    cmake_minimum_required(VERSION 3.6)
    
    ...
    
    # Add a static library called “playcore” built with the c++_static STL.
    include(${PLAYCORE_LOCATION}/playcore.cmake)
    add_playcore_static_library()
    
    // In this example “main” is your native code library, i.e. libmain.so.
    add_library(main SHARED
            ...)
    
    target_include_directories(main PRIVATE
            ${PLAYCORE_LOCATION}/include
            ...)
    
    target_link_libraries(main
            android
            playcore
            ...)
    

डेटा का संग्रह

Play Core नेटिव SDK टूल, वर्शन से जुड़ा डेटा इकट्ठा कर सकता है, ताकि Google इन कामों को कर सके बेहतर बनाने के लिए, इनमें शामिल हैं:

  • ऐप्लिकेशन के पैकेज का नाम
  • ऐप्लिकेशन का पैकेज वर्शन
  • मुख्य नेटिव SDK टूल का वर्शन चलाएं

यह डेटा, अपना ऐप्लिकेशन पैकेज अपलोड करने पर इकट्ठा किया जाएगा Play Console में जाएं. इस डेटा कलेक्शन प्रोसेस से ऑप्ट-आउट करने के लिए, बिल्ड.gradle फ़ाइल में $playcoreDir/playcore-native-metadata.jar इंपोर्ट करते हैं.

ध्यान दें, यह डेटा कलेक्शन, Play Core नेटिव SDK टूल और Google, इकट्ठा किए गए डेटा का अलग-अलग तरीके से इस्तेमाल करता है. यह Google के डेटा से अलग होता है ऐप्लिकेशन अपलोड करते समय, Gradle में बताई गई लाइब्रेरी डिपेंडेंसी का कलेक्शन पैकेज को Play Console से जोड़ना होगा.

ऐप्लिकेशन इंस्टॉल करते समय डिलीवरी पाएं

install-time के तौर पर कॉन्फ़िगर किए गए ऐसेट पैक, ऐप्लिकेशन में तुरंत उपलब्ध हो जाते हैं लॉन्च. ऐक्सेस करने के लिए, NDK AAssetManager API का इस्तेमाल करना इस मोड में दिखाई जाने वाली ऐसेट:

#include <android/asset_manager.h>
#include <android_native_app_glue.h>
...
AAssetManager* assetManager = app->activity->assetManager;
AAsset* asset = AAssetManager_open(assetManager, "asset-name", AASSET_MODE_BUFFER);
size_t assetLength = AAsset_getLength(asset);
char* buffer = (char*) malloc(assetLength + 1);
AAsset_read(asset, buffer, assetLength);

तेज़ी से फ़ॉलो करें और मांग पर डिलीवरी पाएं

नीचे दिए गए सेक्शन में, एपीआई को शुरू करने और जानकारी पाने का तरीका बताया गया है ऐसेट पैक डाउनलोड करने से पहले, और एपीआई को कॉल करने का तरीका पैक डाउनलोड करने, और डाउनलोड किए गए पैक ऐक्सेस करने का तरीका भी शामिल है. ये सेक्शन इन पर लागू होते हैं: fast-follow और on-demand ऐसेट पैक.

ऐप्लिकेशन लॉन्च किया गया

AssetPackManager_init() को हमेशा कॉल करें किसी अन्य को कॉल करने से पहले ऐसेट पैक एपीआई को शुरू करें फ़ंक्शन का इस्तेमाल करना होगा. देखें कि क्या कोई ऐसेट पैक की गड़बड़ी के कोड शामिल करने के बारे में ज़्यादा जानें.

#include "play/asset_pack.h"
...
AssetPackErrorCode AssetPackManager_init(JavaVM* jvm, jobject android_context);

साथ ही, onPause() और onResume() में इन फ़ंक्शन को कॉल करना न भूलें में से ANativeActivityCallbacks:

एसेट पैक के बारे में डाउनलोड की जानकारी पाना

ऐसेट को फ़ेच करने से पहले, ऐप्लिकेशन को डाउनलोड का साइज़ ज़ाहिर करना होगा पैक. इसका इस्तेमाल करें AssetPackManager_requestInfo() फ़ंक्शन का इस्तेमाल करके डाउनलोड के साइज़ के लिए एसिंक्रोनस अनुरोध शुरू करें और पैक पहले से डाउनलोड हो रहा है या नहीं. इसके बाद, इसका इस्तेमाल करें AssetPackManager_getDownloadState() डाउनलोड की स्थिति जानने के लिए (उदाहरण के लिए, इस फ़ंक्शन को हर फ़्रेम में एक बार कॉल करें में शामिल हैं). अगर कोई अनुरोध पूरा नहीं होता है, तो ऐसेट पैक की गड़बड़ी के कोड शामिल करने के बारे में ज़्यादा जानें.

AssetPackErrorCode AssetPackManager_requestInfo();      // Call once
AssetPackErrorCode AssetPackManager_getDownloadState(); // Call once per frame in your game loop

AssetPackManager_getDownloadState() फ़ंक्शन, ओपेक टाइप दिखाता है AssetPackDownloadState का इस्तेमाल एक आउटपुट पॉइंटर के रूप में करता है. नीचे दिए गए फ़ंक्शन को कॉल करने के लिए इस पॉइंटर का इस्तेमाल करें:

AssetPackDownloadState* state;
AssetPackErrorCode error_code = AssetPackManager_getDownloadState(asset-pack-name, &state);
AssetPackDownloadStatus status = AssetPackDownloadState_getStatus(state);
uint64_t downloadedBytes = AssetPackDownloadState_getBytesDownloaded(state);
uint64_t totalBytes = AssetPackDownloadState_getTotalBytesToDownload(state));
AssetPackDownloadState_destroy(state);

इंस्टॉल करें

इस्तेमाल की जाने वाली चीज़ें AssetPackManager_requestDownload() पहली बार किसी ऐसेट पैक को डाउनलोड करने या किसी ऐसेट के लिए अनुरोध करने के लिए पैक अपडेट पूरा करने के लिए:

AssetPackErrorCode AssetPackManager_requestDownload();  // Call once
AssetPackErrorCode AssetPackManager_getDownloadState(); // Call once per frame in your game loop

AssetPackManager_getDownloadState() फ़ंक्शन, ओपेक टाइप दिखाता है AssetPackDownloadState. इस प्रकार का उपयोग करने के बारे में जानकारी के लिए, देखें डाउनलोड की जानकारी पाएं.

बड़े डाउनलोड

अगर डाउनलोड 200 एमबी से बड़ा है और उपयोगकर्ता वाई-फ़ाई से कनेक्ट नहीं है, तो डाउनलोड तब तक शुरू नहीं होता, जब तक उपयोगकर्ता साफ़ तौर पर, डाउनलोड करने के लिए, मोबाइल डेटा कनेक्शन का इस्तेमाल करें. इसी तरह, यदि डाउनलोड बड़ा है और उपयोगकर्ता का वाई-फ़ाई बंद हो जाता है, डाउनलोड रुक जाता है और उसे मोबाइल डेटा कनेक्शन का इस्तेमाल करके आगे बढ़ें. रोके गए पैक की स्थिति WAITING_FOR_WIFI. उपयोगकर्ता से सहमति लेने के लिए कहने के लिए, यूज़र इंटरफ़ेस (यूआई) फ़्लो को ट्रिगर करने के लिए, इसका इस्तेमाल करें निम्न:

उपयोगकर्ता की पुष्टि करना ज़रूरी है

अगर किसी पैक में REQUIRES_USER_CONFIRMATION की स्थिति है, तो डाउनलोड की स्थिति तब तक आगे बढ़ें, जब तक उपयोगकर्ता AssetPackManager_showConfirmationDialog(). यह स्थिति तब पैदा हो सकती है, जब Play ने ऐप्लिकेशन की पहचान नहीं की. ध्यान दें कि कॉलिंग इस मामले में AssetPackManager_showConfirmationDialog() की वजह से ऐप्लिकेशन अपडेट किया गया. अपडेट के बाद, ऐसेट के लिए फिर से अनुरोध करें.

एसेट पैक ऐक्सेस करें

डाउनलोड करने के अनुरोध के बाद, फ़ाइल सिस्टम कॉल का इस्तेमाल करके ऐसेट पैक को ऐक्सेस किया जा सकता है COMPLETED पर पहुंच जाता है. हर ऐसेट पैक को एक अलग डायरेक्ट्री में स्टोर किया जाता है ऐप्लिकेशन के स्टोरेज में. इस्तेमाल की जाने वाली चीज़ें AssetPackManager_getAssetPackLocation() पाने के लिए AssetPackLocation का इस्तेमाल किया जा सकता है. इस्तेमाल की जाने वाली चीज़ें AssetPackLocation_getStorageMethod() तो आपको उस जगह पर सेव करने का तरीका तय करना होगा:

  • ASSET_PACK_STORAGE_APK: ऐसेट पैक को APK के तौर पर इंस्टॉल किया गया है. यहां जाएं: इन ऐसेट को ऐक्सेस करने के लिए, इंस्टॉल के समय डिलीवरी का विकल्प चुनें.
  • ASSET_PACK_STORAGE_FILES: इस्तेमाल करें AssetPackLocation_getAssetsPath() उस डायरेक्ट्री का फ़ाइल पाथ पाने के लिए जिसमें ऐसेट है या अगर एसेट डाउनलोड नहीं किए गए हैं. इस फ़ाइल में डाउनलोड की गई फ़ाइलों में बदलाव न करें पाथ.
AssetPackLocation* location;

AssetPackErrorCode error_code = AssetPackManager_getAssetPackLocation(asset-pack-name, &location);

if (error_code == ASSET_PACK_NO_ERROR) {
    AssetPackStorageMethod storage_method = AssetPackLocation_getStorageMethod(location);
    const char* assets_path = AssetPackLocation_getAssetsPath(location);
    AssetPackLocation_destroy(location);
}

ऐसेट मिल जाने के बाद, उन्हें ऐक्सेस करने के लिए fopen या ifstream जैसे फ़ंक्शन का इस्तेमाल करें फ़ाइलें.

Play Core API के अन्य तरीके

नीचे एपीआई के कुछ अन्य तरीकों के बारे में बताया गया है, जिनका इस्तेमाल आपको अपने ऐप्लिकेशन में करना चाहिए.

अनुरोध रद्द करें

इस्तेमाल की जाने वाली चीज़ें AssetPackManager_cancelDownload() का इस्तेमाल करें. ध्यान दें कि इस अनुरोध को पूरा करने के लिए, कार्रवाई.

हटाने का अनुरोध करें

इस्तेमाल की जाने वाली चीज़ें AssetPackManager_requestRemoval() शेड्यूल करने के लिए इसका इस्तेमाल किया जा सकता है.

अगले चरण

अपने देश/इलाके में और स्टोर पर जाकर, Play ऐसेट डिलीवरी की जांच करना Google Play से डाउनलोड करें.