一般來說,您應使用 Google Play 服務提供的執行階段環境,因為這類環境會動態載入,因此比標準環境更節省空間,也能讓應用程式保持較小的大小。Google Play 服務也會自動使用 LiteRT 執行階段的最新穩定版本,讓您享有額外功能,並隨著時間推移提升效能。如果您在未包含 Google Play 服務的裝置上提供應用程式,或是需要密切管理 ML 執行階段環境,則應使用標準 LiteRT 執行階段。這個選項會將額外的程式碼併入應用程式,讓您更能控管應用程式中的機器學習執行階段,但會增加應用程式的下載大小。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-06-16 (世界標準時間)。"],[],[],null,["# LiteRT for Android\n\nLiteRT lets you run TensorFlow, PyTorch, and JAX models in your Android apps.\nThe LiteRT system provides prebuilt and customizable execution environments for\nrunning models on Android quickly and efficiently, including options for\nhardware acceleration.\n\nFor example Android applications that use LiteRT, see the [LiteRT\nsamples](https://github.com/google-ai-edge/litert-samples/tree/main/examples)\nrepository.\n\nMachine learning models\n-----------------------\n\nLiteRT uses TensorFlow, PyTorch, and JAX models that are converted into a\nsmaller, portable, more efficient machine learning model format. You can use\nprebuilt models with LiteRT on Android, or build your own models and convert\nthem to LiteRT format.\n\nThis page discusses using already-built machine learning models and does not\ncover building, training, testing, or converting models. Learn more about\npicking, modifying, building, and converting machine learning models for LiteRT\nin the [Models](../models/trained) page.\n\nRun models on Android\n---------------------\n\nA LiteRT model running inside an Android app takes in data, processes the data,\nand generates a prediction based on the model's logic. A LiteRT model requires a\nspecial runtime environment in order to execute, and the data that is passed\ninto the model must be in a specific data format, called a\n[*tensor*](https://www.tensorflow.org/guide/tensor). When a model processes the\ndata, known as running an *inference*, it generates prediction results as new\ntensors, and passes them to the Android app so it can take action, such as\nshowing the result to a user or executing additional business logic.\n\n**Figure 1.** Functional execution flow for LiteRT models in Android apps.\n\nAt the functional design level, your Android app needs the following elements to\nrun a LiteRT model:\n\n- **LiteRT runtime environment** for executing the model\n- **Model input handler** to transform data into tensors\n- **Model output handler** to receive output result tensors and interpret them as prediction results\n\nThe following sections describe how the LiteRT libraries and tools provide these\nfunctional elements.\n\nBuild apps with LiteRT\n----------------------\n\nThis section describes the recommended, most common path for implementing LiteRT\nin your Android App. You should pay most attention to the [runtime\nenvironment](#runtime) and [development libraries](#apis) sections. If you have\ndeveloped a custom model, make sure to review the [Advanced development\npaths](#adv_development) section.\n\n### Runtime environment options\n\nThere are several ways you can enable a runtime environment for executing models\nin your Android app. These are the preferred options:\n\n- **LiteRT in [Google Play services runtime environment](./play_services)\n (recommended)**\n- Stand-alone LiteRT runtime environment\n\nIn general, you should use the runtime environment provided by Google Play\nservices because it is more space-efficient than the standard environment since\nit loads dynamically, keeping your app size smaller. Google Play services also\nautomatically uses the most recent, stable release of the LiteRT runtime, giving\nyou additional features and improved performance over time. If you offer your\napp on devices that do not include Google Play services or you need to closely\nmanage your ML runtime environment, then you should use the standard LiteRT\nruntime. This option bundles additional code into your app, allowing you to have\nmore control over the ML runtime in your app at the cost of increasing your\napp's download size.\n\nYou access these runtime environments in your Android app by adding LiteRT\ndevelopment libraries to your app development environment. For information about\nhow to use the standard runtime environments in your app, see the next section.\n| **Note:** Some advanced use cases may require customization of model runtime environment, which are described in the [Advanced runtime\n| environments](#adv_runtime) section.\n\n### Libraries\n\nYou can access the Interpreter API using the [Google Play\nservices](./play_services#add_tensorflow_lite_to_your_app). You can use the\nLiteRT [core](./development#lite_lib) and [support](./development#support_lib)\nlibraries in your Android app. For programming details about using LiteRT\nlibraries and runtime environments, see [Development tools for\nAndroid](./development).\n\n### Obtain models\n\nRunning a model in an Android app requires a LiteRT-format model. You can use\nprebuilt models or build one and convert it to the Lite format. For more\ninformation on obtaining models for your Android app, see the LiteRT\n[Models](../models/trained) page.\n\n### Handle input data\n\nAny data you pass into a ML model must be a tensor with a specific data\nstructure, often called the *shape* of the tensor. To process data with a model,\nyour app code must transform data from its native format, such as image, text,\nor audio data, into a tensor in the required shape for your model.\n| **Note:** Many LiteRT models come with embedded [metadata](./metadata/overview) that describes the required input data.\n\n### Run inferences\n\nProcessing data through a model to generate a prediction result is known as\nrunning an *inference* . Running an inference in an Android app requires a LiteRT\n[runtime environment](#runtime), a [model](#models) and [input\ndata](#input_data).\n\nThe speed at which a model can generate an inference on a particular device\ndepends on the size of the data processed, the complexity of the model, and the\navailable computing resources such as memory and CPU, or specialized processors\ncalled *accelerators* . Machine learning models can run faster on these\nspecialized processors such as graphics processing units (GPUs) and tensor\nprocessing units (TPUs), using LiteRT hardware drivers called\n*delegates* . For more information about delegates and hardware acceleration of\nmodel processing, see the [Hardware acceleration\noverview](../performance/delegates).\n\n### Handle output results\n\nModels generate prediction results as tensors, which must be handled by your\nAndroid app by taking action or displaying a result to the user. Model output\nresults can be as simple as a number corresponding to a single result (0 = dog,\n1 = cat, 2 = bird) for an image classification, to much more complex results,\nsuch as multiple bounding boxes for several classified objects in an image, with\nprediction confidence ratings between 0 and 1.\n| **Note:** Many LiteRT models come with embedded [metadata](./metadata/overview) that describes the output results of a model and how to interpret it.\n\nAdvanced development paths\n--------------------------\n\nWhen using more sophisticated and customized LiteRT models, you may need to use\nmore advanced development approaches than what is described above. The following\nsections describe advanced techniques for executing models and developing them\nfor LiteRT in Android apps.\n\n### Advanced runtime environments\n\nIn addition to the standard runtime and Google Play services runtime\nenvironments for LiteRT, there are additional runtime environments you can use\nwith your Android app. The most likely use for these environments is if you have\na machine learning model that uses ML operations that are not supported by the\nstandard runtime environment for LiteRT.\n\n- [Flex runtime](../models/ops_select) for LiteRT\n- Custom-built LiteRT runtime\n\nThe LiteRT [Flex runtime](../models/ops_select) lets you include specific\noperators required for your model. As an advanced option for running your model,\nyou can build LiteRT for Android to include operators and other functionality\nrequired for running your TensorFlow machine learning model. For more\ninformation, see [Build LiteRT for Android](../build/android).\n\n### C and C++ APIs\n\nLiteRT also provides an API for running models using C and C++. If your app uses\nthe [Android NDK](https://developer.android.com/ndk), you should consider using\nthis API. You may also want to consider using this API if you want to be able to\nshare code between multiple platforms. For more information about this\ndevelopment option, see the [Development\ntools](./development#tools_for_building_with_c_and_c) page.\n\n### Server-based model execution\n\nIn general, you should run models in your app on an Android device to take\nadvantage of lower latency and improved data privacy for your users. However,\nthere are cases where running a model on a cloud server, off device, is a better\nsolution. For example, if you have a large model which does not easily compress\ndown to a size that fits on your users' Android devices, or can be executed with\nreasonable performance on those devices. This approach may also be your\npreferred solution if consistent performance of the model across a wide range of\ndevices is top priority.\n\nGoogle Cloud offers a full suite of services for running AI models. For more\ninformation, see Google Cloud's [AI and machine learning\nproducts](https://cloud.google.com/products/ai) page.\n\n### Custom model development and optimization\n\nMore advanced development paths are likely to include developing custom machine\nlearning models and optimizing those models for use on Android devices. If you\nplan to build custom models, make sure you consider applying [quantization\ntechniques](../models/post_training_quantization) to models to reduce memory and\nprocessing costs. For more information on how to build high-performance models\nfor use with LiteRT, see [Performance best practices](../models/best_practices)\nin the Models section.\n\nSupported Android Versions\n--------------------------\n\n| LiteRT Version | Status | Min SDK Level | Min NDK Level (if used) | Release Date |\n|----------------|----------|-----------------------------|-------------------------|--------------|\n| **`v1.2.0`** ⭐ | ✅ Active | `31` (Android 12 Snow Cone) | `r26a` | 2025-03-13 |\n\n**Important:** *Keep your dependencies up to date to ensure compatibility with the latest features and security updates.*"]]