כדי לגשת לחבילות הנכסים של האפליקציה מקוד C ו-C++, פועלים לפי השלבים במדריך הזה.
קוד שילוב לדוגמה זמין ב-GitHub.
פיתוח למודעות מותאמות
כדי להוסיף את Play Asset Delivery ל-Android App Bundle של הפרויקט, פועלים לפי השלבים הבאים. לא צריך להשתמש ב-Android Studio כדי לבצע את השלבים האלה.
מעדכנים את גרסת הפלאגין של Android Gradle בקובץ
build.gradleשל הפרויקט לגרסה4.0.0ואילך.בספרייה ברמה העליונה של הפרויקט, יוצרים ספרייה לחבילת הנכסים. שם הספרייה הזו משמש כשם של חבילת הנכסים. השמות של חבילות הנכסים חייבים להתחיל באות ויכולים להכיל רק אותיות, מספרים וקווים תחתונים.
בספרייה של חבילת הנכסים, יוצרים קובץ
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 ]" } }
בקובץ
build.gradleשל האפליקציה בפרויקט, מוסיפים את השם של כל חבילת נכסים בפרויקט, כמו שמוצג בהמשך:// In the app build.gradle file: android { ... assetPacks = [":asset-pack-name", ":asset-pack2-name"] }
בקובץ
settings.gradleשל הפרויקט, כוללים את כל חבילות הנכסים בפרויקט, כמו שמוצג בהמשך:// In the settings.gradle file: include ':app' include ':asset-pack-name' include ':asset-pack2-name'
בספרייה של חבילת הנכסים, יוצרים את ספריית המשנה הבאה:
src/main/assets.ממקמים את הנכסים בספרייה
src/main/assets. אפשר גם ליצור כאן תיקיות משנה. מבנה הספרייה של האפליקציה אמור להיראות כך:build.gradlesettings.gradleapp/asset-pack-name/build.gradleasset-pack-name/src/main/assets/your-asset-directories
יוצרים את קובץ ה-Android App Bundle באמצעות Gradle. ב-App Bundle שנוצר, ספריית הרמה הבסיסית כוללת עכשיו את הפריטים הבאים:
asset-pack-name/manifest/AndroidManifest.xml: מגדיר את המזהה ואת מצב ההעברה של חבילת הנכסיםasset-pack-name/assets/your-asset-directories: הספרייה שמכילה את כל הנכסים שנמסרו כחלק מחבילת הנכסים
Gradle יוצר את המניפסט לכל חבילת נכסים ומוציא את הספרייה
assets/.(אופציונלי) מגדירים את ה-App Bundle כך שיתמוך בפורמטים שונים של דחיסת טקסטורה.
שילוב עם ספריית Play Asset Delivery
מטמיעים את ה-API הזה בהתאם לסוג המסירה של חבילת הנכסים שאליה רוצים לגשת. השלבים האלה מוצגים בתרשים הזרימה הבא.
איור 1. תרשים זרימה לגישה לחבילות נכסים
Play Core Native SDK מספק את קובץ הכותרת play/asset_pack.h בשפת C כדי לבקש חבילות נכסים, לנהל הורדות ולגשת לנכסים.
הגדרת סביבת הפיתוח ל-Play Core Native SDK
הורדה Play Core Native SDK
לפני ההורדה, עליכם לאשר את התנאים וההגבלות הבאים.
תנאים והגבלות
Last modified: September 24, 2020- 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.
- For purposes of these terms, "APIs" means Google's APIs, other developer services, and associated software, including any Redistributable Code.
- “Redistributable Code” means Google-provided object code or header files that call the APIs.
- 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.
- 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.
בצע אחת מהפעולות הבאות:
- מתקינים את Android Studio בגרסה 4.0 ואילך. משתמשים בממשק המשתמש של SDK Manager כדי להתקין את גרסה 10.0 של Android SDK Platform (רמת API 29).
- מתקינים את כלי שורת הפקודה של Android SDK ומשתמשים ב-
sdkmanagerכדי להתקין את Android SDK Platform גרסה 10.0 (רמת API 29).
כדי להכין את Android Studio לפיתוח נייטיב, משתמשים ב-SDK Manager ומתקינים את הגרסאות העדכניות של CMake ו-Android Native Development Kit (NDK). מידע נוסף על יצירה או ייבוא של פרויקטים מקוריים זמין במאמר תחילת העבודה עם NDK.
מורידים את קובץ ה-ZIP ומחלצים אותו לצד הפרויקט.
קישור להורדה גודל סיכום ביקורת (checksum) של SHA-256 54.8 MiB 008b8fedc6179a6dc6ccc21af75591afc7036f78f3d5559d844f1b923934fef0 מעדכנים את הקובץ
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.3.0' implementation 'com.google.android.play:integrity:1.6.0' implementation 'com.google.android.play:review:2.0.2' // 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.3.0") implementation("com.google.android.play:integrity:1.6.0") implementation("com.google.android.play:review:2.0.2") // Import these common dependencies. implementation("com.google.android.gms:play-services-tasks:18.0.2") implementation(files("$playcoreDir/playcore-native-metadata.jar")) ... }
מעדכנים את קובצי ה-
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 Native SDK יאסוף נתונים שקשורים לגרסה כדי לאפשר ל-Google לשפר את המוצר, כולל:
- שם החבילה של האפליקציה
- גרסת החבילה של האפליקציה
- גרסת Play Core Native SDK
הנתונים האלה ייאספו כשתעלו את חבילת ה-APK ל-Play Console. כדי לבטל את ההסכמה לתהליך איסוף הנתונים הזה, צריך להסיר את הייבוא של $playcoreDir/playcore-native-metadata.jar בקובץ build.gradle.
חשוב לדעת: איסוף הנתונים הזה שקשור לשימוש שלכם ב-Play Core Native SDK והשימוש של Google בנתונים שנאספו נפרדים ובלתי תלויים באיסוף של Google של יחסי תלות של ספריות שהוצהרו ב-Gradle כשאתם מעלים את חבילת ה-APK ל-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 ועל פי דרישה
בקטעים הבאים מוסבר איך להפעיל את ה-API, איך לקבל מידע על חבילות נכסים לפני שמורידים אותן, איך להפעיל את ה-API כדי להתחיל את ההורדה ואיך לגשת לחבילות שהורדו. הקטעים האלה רלוונטיים לחבילות נכסים מסוג fast-follow ו-on-demand.
הפעלת האפליקציה
תמיד צריך להפעיל את AssetPackManager_init() כדי לאתחל את ה-API של חבילת הנכסים לפני שמפעילים פונקציה אחרת. בודקים אם יש קודי שגיאה של חבילת הנכסים.
#include "play/asset_pack.h" ... AssetPackErrorCode AssetPackManager_init(JavaVM* jvm, jobject android_context);
חשוב גם להפעיל את הפונקציות הבאות ב-onPause() וב-onResume() של ANativeActivityCallbacks:
קבלת מידע על הורדה של חבילות נכסים
האפליקציות נדרשות לחשוף את גודל ההורדה לפני שהן מאחזרות את חבילת הנכסים. משתמשים בפונקציה AssetPackManager_requestInfo() כדי להתחיל בקשה לא סנכרונית לגודל ההורדה ולבדוק אם החבילה כבר בתהליך הורדה. לאחר מכן, משתמשים בפונקציה AssetPackManager_getDownloadState() כדי לבדוק את מצב ההורדה (לדוגמה, קוראים לפונקציה הזו פעם אחת לכל פריים ב-game loop). אם בקשה נכשלת, בודקים את קודי השגיאה של חבילת הנכסים.
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.
במאמר קבלת מידע על הורדות מוסבר איך משתמשים בסוג הזה.
הורדות גדולות
אם גודל ההורדה הוא יותר מ-200MB והמשתמש לא מחובר ל-Wi-Fi, ההורדה לא תתחיל עד שהמשתמש יאשר במפורש להמשיך בהורדה באמצעות חיבור לחבילת גלישה. באופן דומה, אם ההורדה גדולה והמשתמש מאבד את החיבור ל-Wi-Fi, ההורדה מושהית ונדרשת הסכמה מפורשת כדי להמשיך באמצעות חיבור לחבילת גלישה. חבילה מושהית נמצאת במצב 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()כדי לקבל נתיב קובץ לספרייה שמכילה את הנכסים, או null אם הנכסים לא הורדו. אל תשנו את הקבצים שהורדתם בנתיב הקובץ הזה.
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
בהמשך מפורטות כמה שיטות API נוספות שאולי תרצו להשתמש בהן באפליקציה.
ביטול הבקשה
כדי לבטל בקשה פעילה של חבילת נכסים, משתמשים ב-AssetPackManager_cancelDownload(). חשוב לדעת שהבקשה הזו היא פעולה שמתבצעת כמיטב היכולת.
בקשת הסרה
משתמשים ב-AssetPackManager_requestRemoval() כדי לתזמן את ההסרה של חבילת נכסים.
השלבים הבאים
בודקים את Play Asset Delivery באופן מקומי ומ-Google Play.