翻篇收录说明
翻篇网页快照

Play for On-device AI (beta) | Other Play guides | Android Developers

https://developer.android.com/google/play/on-device-ai

最近抓取:2026-12-03 04:22:22(北京时间)

最近内容变化:2026-11-29 20:21:07(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Play for On-device AI enables developers to efficiently distribute custom machine learning models using Android App Bundles and Google Play, offering various delivery modes and device targeting capabilities to optimize deployment on Android devices.

正文摘录

Android Developers Google Play Other Play guides Play for On-device AI (beta) Introduction Play for On-device AI brings the benefits of Android App Bundles and Google Play delivery to custom ML model distribution so you can improve model performance with less device ecosystem complexity at no additional cost. It lets you publish a single artifact to Play containing your code, assets, and ML models and to choose from a number of delivery modes and targeting options. Benefits Upload a single publishing artifact to Google Play and delegate hosting, delivery, updates, and targeting to Play at no additional cost. Deliver your ML models at install-time, fast-follow, or on-demand. Install-time delivery can guarantee that a very large model is present when your app is opened. Your model will be installed as an APK. Fast-follow delivery occurs automatically in the background after your app has been installed. Users may open your app before your model has been fully downloaded. Your model will be downloaded to your app's internal storage space. On-demand delivery lets you request the model at runtime, which is useful if the model is only required for certain user-flows. Your model will be downloaded to your app's internal storage space. Deliver variants of your ML models that are targeted to specific devices based on device model, system properties, or RAM. Keep app updates small and optimized with Play's automatic patching, which means only the differences in files need to be downloaded. Considerations By using Play for On-device AI you agree to the terms in the Google Play Developer Distribution Agreement and the Play Core Software Development Kit Terms of Service. Models downloaded by Play for On-device AI should only be used by your apps. Models shouldn't be offered to other apps. Individual AI packs can be up to 1.5GB, based on their compressed download sizes. The maximum cumulative app size of any version of your app generated from your app bundle is 4GB. Apps over 1GB in size must set min SDK Level to 21 or higher. How to use Play for On-device AI Play for On-device AI uses AI packs. You package custom models that are ready for distribution in AI packs in your app bundle. You can choose whether the AI pack should be delivered at install-time, fast-follow, or on-demand. By packaging AI packs with your app bundle, you can use all of Play's existing testing and release tools, such as test tracks and staged rollouts to manage your app's distribution with your custom models. AI packs are updated together with the app binary. If your new app release doesn't make changes to an AI pack, then Play's automatic patching process will ensure the user doesn't have to re-download it. Play will just download what's changed when it updates the app. AI packs only contain models. Java, Kotlin, and native libraries are not allowed. If you need to ship libraries or code to run your ML model, move it into the base module or a feature module. You can configure your feature module so that it has the same download and targeting settings as the AI pack. Use LiteRT and MediaPipe with AI packs You can use LiteRT and MediaPipe with AI packs. Package your model in an AI pack and then access it using the instructions for install-time packs or for fast-follow and on-demand packs. Further reading: Getting started with LiteRT The sample app shows how you can package a LiteRT model in an AI pack and load it at runtime. There are many pretrained LiteRT models that you can use in AI packs to get started. Getting started with MediaPipe For fast-follow and on-demand packs, you can use AssetCache.java to load your assets (eg. .binarypb files) by their file paths. For install-time packs, you can use AndroidAssetUtil.java. Get started with AI packs At a high level, here's how you can start using Play for On-device AI: Package your models into AI packs into your Android App Bundle and specify how the AI packs should be delivered. [Optional] If you want to deliver different models to different devices, you can configure device targeting for your AI packs. For example, you could deliver AI pack A to a specific device model, AI pack B to devices with at least 6GB of RAM, and all other devices could receive no model. [Optional] If you're using on-demand or fast-follow delivery, integrate the Play AI Delivery Library into your app to download your AI packs as needed. Test and release your app bundle to Google Play. Check Android Gradle Plugin version To use AI packs, ensure that your Android Gradle Plugin (AGP) version is at least 8.8. This version is packaged with Android Studio Ladybug 2. Extract your model into an AI pack Android Studio is not required for the following steps. In the top-level directory of your project, create a directory for the AI pack. This directory name is used as the AI pack name. AI pack names must start with a letter and can only contain letters, numbers, and underscores. In the AI pack directory, create a build.gradle file and add the following code. Make sure to specify the name of the AI pack and only one delivery type: // In the AI pack's build.gradle file: plugins { id 'com.android.ai-pack' } aiPack { packName = "ai-pack-name" // Directory name for the AI pack dynamicDelivery { deliveryType = "[ install-time | fast-follow | on-demand ]" } } In the project's app build.gradle file, add the name of every AI pack in your project as shown below: // In the app build.gradle file: android { ... assetPacks = [":ai-pack-name", ":ai-pack2-name"] } In the project's settings.gradle file, include all AI packs in your project as shown below: // In the settings.gradle file: include ':app' include ':ai-pack-name' include ':ai-pack2-name' Inside your AI pack, create a src/main/assets/ directory. Place your models in the src/main/assets directory. You can create subdirectories in here as well. The directory structure for your app should now look like the following: build.gradle settings.gradle app/ ai-pack-name/build.gradle ai-pack-name/src/main/assets/your-model-directories Add code to load and run your models. How you do this will depend on the delivery mode of your AI packs. See instructions for install-time and fast-follow/on-demand below. [Optional] Configure device targeting to deliver different models to different devices. Build the Android App Bundle with Gradle. In the generated app bundle, the root-level directory now includes the following: ai-pack-name/manifest/AndroidManifest.xml: Configures the AI pack's identifier and delivery mode ai-pack-name/assets/your-model-directories: Directory that contains all assets delivered as part of the AI pack Gradle generates the manifest for each AI pack and outputs the assets/ directory for you. Configure install-time delivery AI packs configured as install-time are immediately available at app launch. Use the Java AssetManager API to access AI packs served in this mode: import android.content.res.AssetManager; ... Context context = createPackageContext("com.example.app", 0); AssetManager assetManager = context.getAssets(); InputStream is = assetManager.open("model-name"); Configure fast-follow and on-demand delivery To download AI packs with fast-follow or on-demand delivery, use the Play AI Delivery Library. Declare dependency on the Play AI Delivery Library In your app's build.gradle file, declare a dependency on the Play AI Delivery Library: dependencies { ... implementation "com.google.android.play:ai-delivery:0.1.1-alpha01" } Check status Each AI pack is stored in a separate folder in the app's internal storage. Use the getPackLocation() method to determine the root folder of an AI pack. This method returns the following values: Return value Status A valid AiPackLocation object AI pack root folder is ready for immediate access at assetsPath() null Unknown AI pack or AI packs are not available Get download information about AI packs Use the getPackStates() method to determine the size of the download and whether the pack is already downloading. Task<AiPackStates> getPackStates(List<String> packNames) getPackStates() is an asynchronous method that returns a Task<AiPackStates>. The packStates() method of an AiPackStates object returns a Map<String, AiPackState>. This map contains the state of each requested AI pack, keyed by its name: Map<String, AiPackState> AiPackStates#packStates() The final request is shown by the following: final String aiPackName = "myAiPackName"; aiPackManager .getPackStates(Collections.singletonList(aiPackName)) .addOnCompleteListener(new OnCompleteListener<AiPackStates>() { @Override public void onComplete(Task<AiPackStates> task) { AiPackStates aiPackStates; try { aiPackStates = task.getResult(); AiPackState aiPackState = aiPackStates.packStates().get(aiPackName); } catch (RuntimeExecutionException e) { Log.d("MainActivity", e.getMessage()); return; }); The following AiPackState methods provide the size of the AI pack, the downloaded amount so far (if requested), and the amount already transferred to the app: totalBytesToDownload() bytesDownloaded() transferProgressPercentage() To get the status of an AI pack, use the status() method, which returns the status as an integer that corresponds to a constant field in the AiPackStatus class. An AI pack that's not installed yet has the status AiPackStatus.NOT_INSTALLED. If a request fails, use the errorCode() method, whose return value corresponds to a constant field in the AiPackErrorCode class. Install Use the fetch() method to download an AI pack for the first time or call for the update of an AI pack to complete: Task<AiPackStates> fetch(List<String> packNames) This method returns an AiPackStates object containing a list of packs and their initial download states and sizes. If an AI pack requested using fetch() is already downloading, the download status is returned and no additional download is started. Monitor download states You should implement an AiPackStateUpdateListener to track the installation progress of AI pa