This is the third day of my participation in the August More text Challenge. For details, see: August More Text Challenge

AAB

The Android App Bundle is a new official release format for Android that allows you to develop and publish apps in a more efficient way. With the Android App Bundle, you can make it easier to deliver a great experience with smaller apps, increasing installation success and reducing uninstalls. The conversion process is easy and convenient. You don’t have to refactor your code to start taking advantage of smaller applications. By switching to this format, you can benefit from the experience of modular application development and the delivery of customizable functionality.

Play Asset Delivery

Play Asset Delivery (PAD) brings the benefits of app Bundle to games. It allows games over 150 MB to replace the older version of the extension file (OBB) by publishing a single artifact to Play that contains all the resources the game needs. PAD offers flexible distribution modes, automatic updates, compression, and incremental fixes, and is free to use. With the PAD, all resource packs are hosted and provided on Google Play, so you don’t need to use a content delivery network (CDN) to provide game resources to players.

Play Asset Delivery uses resource packs, which consist of resources such as textures, shaders, and sounds, but do not contain executable code. Dynamic Delivery allows you to customize how and when to download individual resource packs to devices based on three distribution modes: install-time distribution, fast-track distribution, and on-demand distribution.

Distribution pattern

Install-time The resource bundle is distributed when the user installs the application. These resource bundles are provided in the form of a split APK (part of an APK set). They are also called “up front” resource packs; You can use these resource bundles immediately upon application startup. These resource packs increase the size of the app listed on the Google Play store. Users cannot modify or delete these resource bundles.

Fast-follow resource packs are automatically downloaded as soon as the user installs the application; Users can start the fast-follow download without having to open the app. Such downloads do not prevent users from accessing the application. These resource packs increase the size of the app listed on the Google Play store.

On-demand resource packs are downloaded while the application is running.

The Google Play store offers resource packs configured for fast-follow and on-demand in the form of archived files rather than split APKs. These resource bundles are then expanded in the application’s internal storage space. You can use the Play Core API to query the location of resource bundles provided in this way. The application cannot assume the existence or location of these files, as they may be deleted by the user or moved between game sessions by the Play Core SDK. Although these files can be written by the application, you should treat them as read-only files because the resource pack patch depends on the integrity of these files.

Resource update

When an app is updated, install-time Asset Pack is updated as part of the underlying app update (the developer does not have to do anything).

For application updates to the fast-follow and on-demand Asset Pack, follow these steps:

  • 1. The system downloads the applied patches (including all assets) to a secure location on the device.
  • 2. Update application binaries; This includes all install-time Asset packs.
  • All Asset Pack downloads become invalid.
  • 4. Copy and apply patches for assets to assets stored in the in-application storage space.

In most cases, when the user opens the game, the overall update is complete and the user can immediately start playing the updated version. In rare cases, when the user opens the application, the application binaries may have been updated, but the patch application process for the assets has not yet been completed, so the user cannot access those assets. To deal with such situations, you need to provide appropriate “updating” interface elements for these assets, or build logic to handle invalid resources that are not yet accessible. Since the application binaries are not updated until all Asset Pack types have been downloaded, the application of the patch takes place locally offline and should be done quickly.

Texture compression format positioning

Texture compression is a lossy image compression that allows the GPU to render directly from the compressed texture using dedicated hardware, thereby reducing the amount of texture memory and memory bandwidth required. With texture compression format positioning, you can add textures compressed using multiple texture compression formats in the Android App Bundle and rely on Google Play to automatically provide resources for each device using the best texture compression format.

Application version Update

After a new version of an app is uploaded to Google Play, users can open the previous version of the app if they haven’t updated the app on their device. In this case, the application can choose to force or recommend Updates by calling the In-App Updates API if necessary. With this API, you can trigger updates from within the app, rather than having users trigger updates from the Google Play store.

Maximum download size

The Asset Pack is ideal for large games because of its high size ceiling:

  • The maximum download size of each fast-follow and on-demand Asset Pack is 512 MB.
  • The total download size of all install-time Asset Packs is limited to 1 GB.
  • The total download size of all Asset packs in an Android App Bundle is capped at 2 GB.
  • 4. Up to 50 resource bundles can be used in an Android App Bundle.

If you are using texture compression format positioning, these download restrictions apply to each unique texture format.

Building with Unity

The Unity AssetBundle file contains serialized assets that can be loaded by the Unity engine while the application is running. These files are platform-specific (for example, built for Android) and can be used in conjunction with asset packages. Most commonly, an AssetBundle file is packaged as an asset package that uses the same name as AssetBundle.

  • 1. Get the Play Asset Delivery Unity plugin (or package).

Note: Download and use of the Google Play Core libraries are subject to the Play Core Software Development Kit terms of Service. By downloading and using the Google Play Core Library, you agree to the Play Core Software Development Kit Terms of Service.

The Unity plug-in is available for a selection of Play core apis, including Play Asset Delivery. Download the latest version from Google Play Plugins for Unity. This is a single package that contains the Play Core plug-in as well as other Play plug-ins such as Play In-app Billing and Play Instant.

  • 2. Create AssetBundles in Unity.

Note: You can create and manage AssetBundles in your Unity project using the Unity AssetBundle Browser.

Configure AssetBundles using UI

  • 1. Configure each AssetBundle in the AssetBundle:

    • Choose Google > Android App Bundle > Asset Delivery Settings.
    • To select the folder that contains the AssetBundle file directly, click Add Folder.

  • 2. Change Asset Delivery to Install Time, Fast Follow, or On Demand for each bundle. Resolve any errors or dependencies and close the window.

  • 3. Select Google > Build Android App Bundle to Build the application package.
  • 4. (Optional) Configure your application package to support different texture compression formats.

Common texture compression formats:

  • DDS or S3TC: Sometimes called DXTC or DXTn. OpenGL supports three forms of this format.
  • ETC1: Most devices support this format. This format does not support transparency, but the game can use the second texture file for the Alpha channel component.
  • ETC2: This format is supported by all devices that support GLES3.
  • PVRTC: A popular format for iOS games, supported on some Android devices.
  • ASTC: A new format designed to replace the previous one. It is more flexible than previous formats because it supports a variety of block sizes. Using this format can help you optimize the size of your game.

Supported formats and percentage of devices that support them:

Configure the asset package using the API

You can configure asset delivery through editor scripts that can be run as part of an automated build system.

Use the AssetPackConfig class to define the assets to be included in the Android App Bundle build, as well as the schema. These asset bundles do not need to contain assetBundles.

public void ConfigureAssetPacks { // Creates an AssetPackConfig with a single asset pack, named // examplePackName, containing all the files in path/to/exampleFolder. var assetPackConfig = new AssetPackConfig(); assetPackConfig.AddAssetsFolder("examplePackName", "path/to/exampleFolder", AssetPackDeliveryMode.OnDemand); // Configures the build system to use the newly created assetPackConfig when // calling Google > Build and Run or Google  > Build Android App Bundle. AssetPackConfigSerializer.SaveConfig(assetPackConfig); // Alternatively, use BundleTool.BuildBundle to build an App Bundle from script. BuildBundle(new buildPlayerOptions(), assetPackConfig); }Copy the code

You can also use the static BuildBundle method Bundletool in the class to generate an Android App Bundle with asset packages, given BuildPlayerOptions and AssetPackConfig.

Play Asset Delivery Unity API integration

The Play Asset Delivery Unity API provides the ability to request Asset packages, download management, and access resources. Be sure to add the Unity plugin to your project first.

The functions you use in the API depend on how you create the asset package.

  • If you are using the UI to configure AssetBundles, select the asset bundle configured by the plug-in.
  • If you use an API configuration asset package, select the API configuration asset package.

You can implement the API depending on the delivery type of the asset package you want to access. These steps are shown in the following flowchart.

Retrieve AssetBundles

Import the Play Asset Delivery API and call the RetrieveAssetBundleAsync() method to retrieve the AssetBundle.

using Google.Play.AssetDelivery;

// Loads the AssetBundle from disk, downloading the asset pack containing it if necessary.
PlayAssetBundleRequest bundleRequest = PlayAssetDelivery.RetrieveAssetBundleAsync(asset-bundle-name);
Copy the code

Delivery on installation

The asset package is configured for install-time to be available immediately upon application startup. You can load a scenario from AssetBundle using the following command:

AssetBundle assetBundle = bundleRequest.AssetBundle;

// You may choose to load scenes from the AssetBundle. For example:
string[] scenePaths = assetBundle.GetAllScenePaths();
SceneManager.LoadScene(scenePaths[path-index]);
Copy the code

Fast follow up and on-demand delivery

These sections apply to the fast-follow and on-demand asset packages.

Check the status

Each asset package is stored in a separate folder within the application’s internal storage. Use the isDownloaded() method to determine whether the asset package has been downloaded.

Monitor the download

Query PlayAssetBundleRequest to monitor the state of the request object:

// Download progress of request, Between 0.0f and 1.0 F. The value will always be // 1.0 for assets delivered as install-time. // NOTE: A value of 1.0 will only signify the download is complete. It will still need to be loaded. Float progress = bundleRequest.DownloadProgress; // Returns true if: // * it had either completed the download, installing, and loading of the AssetBundle, // * OR if it has encountered an error. bool done = bundleRequest.IsDone; // Returns status of retrieval request. AssetDeliveryStatus status = bundleRequest.Status; switch(status) { case AssetDeliveryStatus.Pending: // Asset pack download is pending - N/A for install-time assets. case AssetDeliveryStatus.Retrieving: // Asset pack is being downloaded and transferred to app storage. // N/A for install-time assets. case AssetDeliveryStatus.Available: // Asset pack is downloaded on disk but NOT loaded into memory. // For PlayAssetPackRequest(), this indicates that the request is complete. case AssetDeliveryStatus.Loading: // Asset pack is being loaded. case AssetDeliveryStatus.Loaded: // Asset pack has finished loading, assets can now be loaded. // For PlayAssetBundleRequest(), this indicates that the request is complete. case AssetDeliveryStatus.Failed: // Asset pack retrieval has failed. case AssetDeliveryStatus.WaitingForWifi: // Asset pack retrieval paused until either the device connects via Wi-Fi, // or the user accepts the PlayAssetDelivery.ShowCellularDataConfirmation dialog. default: break; }Copy the code

A large number of downloads

Asset packages larger than 150MB can be downloaded automatically, but only over Wi-Fi. If the user is not connected to wi-fi, PlayAssetBundleRequest state is set to AssetDeliveryStatus. Download WaitingForWifi and suspended. In this case, you either wait for the device to connect to Wi-Fi and continue with the download, or you prompt the user for approval to download the package over a cellular connection.

if(bundleRequest.Status == AssetDeliveryStatus.WaitingForWifi) { var userConfirmationOperation = PlayAssetDelivery.ShowCellularDataConfirmation(); yield return userConfirmationOperation; switch(userConfirmationOperation.GetResult()) { case ConfirmationDialogResult.Unknown: // userConfirmationOperation finished with an error. Something went // wrong when displaying the prompt to the user, and they weren't // able to interact with the dialog. In this case, we recommend // developers wait for Wi-Fi before attempting to download again. // You can get more info by calling GetError() on the operation. case ConfirmationDialogResult.Accepted: // User accepted the confirmation dialog - download will start // automatically (no action needed). case ConfirmationDialogResult.Declined: // User canceled or declined the dialog. Await Wi-Fi connection, or // re-prompt the user. default: break; }}Copy the code

Cancel request (on demand only)

To cancel the request before AssetBundles are loaded into memory, call the PlayAssetBundleRequest method on the AttemptCancel() object:

// Will only attempt if the status is Pending, Retrieving, or Available - otherwise
// it will be a no-op.
bundleRequest.AttemptCancel();

// Check to see if the request was successful by checking if the error code is Canceled.
if(bundleRequest.Error == AssetDeliveryErrorCode.Canceled) {
    // Request was successfully canceled.
}
Copy the code

Asynchronous request asset package

In most cases, you should use Coroutines to asynchronously request asset packages and monitor progress, as follows:

private IEnumerator LoadAssetBundleCoroutine(string assetBundleName) { PlayAssetBundleRequest bundleRequest = PlayAssetDelivery.RetrieveAssetBundleAsync(assetBundleName); while (! bundleRequest.IsDone) { if(bundleRequest.Status == AssetDeliveryStatus.WaitingForWifi) { var userConfirmationOperation =  PlayAssetDelivery.ShowCellularDataConfirmation(); // Wait for confirmation dialog action. yield return userConfirmationOperation; if((userConfirmationOperation.Error ! = AssetDeliveryErrorCode.NoError) || (userConfirmationOperation.GetResult() ! = ConfirmationDialogResult.Accepted)) { // The user did not accept the confirmation - handle as needed. } // Wait for Wi-Fi connection OR confirmation dialog acceptance before moving on. yield return new WaitUntil(() => bundleRequest.Status ! = AssetDeliveryStatus.WaitingForWifi); } // Use bundleRequest.DownloadProgress to track download progress. // Use bundleRequest.Status to track the status of request. yield return null; } if (bundleRequest.Error ! = AssetDeliveryErrorCode.NoError) { // There was an error retrieving the bundle. For error codes NetworkError // and InsufficientStorage, you may prompt the user to check their // connection settings or check their storage space, respectively, then // try again. yield return null; } // Request was successful. Retrieve AssetBundle from request.AssetBundle. AssetBundle assetBundle = bundleRequest.AssetBundle;Copy the code

Other Play Core API methods

Here are some other API methods you might want to use in your application.

Check the download size

Check the size of the AssetBundle by making an asynchronous call to Google Play and setting the callback method when the operation is complete:

public IEnumerator GetDownloadSize() {
   PlayAsyncOperation<long> getSizeOperation =
   PlayAssetDelivery.GetDownloadSize(assetPackName);

   yield return getSizeOperation;
   if(operation.Error != AssetDeliveryErrorCode.NoError) {
       // Error while retrieving download size.
    } else {
        // Download size is given in bytes.
        long downloadSize = operation.GetResult();
    }
}
Copy the code

Remove AssetBundles

You can remove the current not loaded into memory for quick attention and on-demand assetBundles. Make the following asynchronous call and set the callback method when completed:

PlayAsyncOperation<string> removeOperation = PlayAssetDelivery.RemoveAssetPack(assetBundleName); removeOperation.Completed += (operation) => { if(operation.Error ! = AssetDeliveryErrorCode.NoError) { // Error while attempting to remove AssetBundles. } else { // Files were deleted OR files did not exist to begin with. } };Copy the code

test

In the Unity Editor, select Google > Build and Run.

behavior

The install-time package is installed during the application installation process.

The fast-follow package behaves the same as the on-demand package. That is, they are not automatically acquired when the game is side-loaded. The developer needs to manually request it at the start of the game; This does not require any code changes in your application.

limit

Here are the limitations of local testing:

  • Packs are retrieved from external storage, not Play, so you can’t test how your code behaves in the event of a network error.
  • Local tests do not include waiting Wi-Fi scenarios.
  • Updates are not supported. Before installing a new build, manually uninstall the previous version.

Use internal application shares for testing

As you approach a release candidate, test your game with as realistic a configuration as possible to ensure that your game will perform well for your users in a production environment. Build your application package.

To share test asset delivery using an internal application:

  • Build your application package.
  • Follow the Play Manager instructions to learn how to share your application internally.
  • On the test device, click the internal application share link for the version of the application you just uploaded.
  • Install the app from the Google Play Store page you see after clicking the link.

Get the job done.

Related to recommend

Android AAB package error

AAB+PAD for Android