English | [中文](../../cn/faq/use_cpp_sdk_on_android.md) # FastDeploy to deploy on Android Platform This document will take PicoDet as an example and explain how to encapsulate FastDeploy model to Android through JNI. You need to know at least the basics of C++, Java, JNI and Android. If you mainly focus on how to call FastDeploy API in Java layer, you can skip this document. ## Content - [FastDeploy to deploy on Android Platform](#fastdeploy-to-deploy-on-android-platform) - [Content](#content) - [Create a new Java class and Define the native API](#create-a-new-java-class-and-define-the-native-api) - [Generate JNI function definition with Android Studio](#generate-jni-function-definition-with-android-studio) - [Implement JNI function in the C++ layer](#implement-jni-function-in-the-c-layer) - [Write CMakeLists.txt and configure build.gradle](#write-cmakeliststxt-and-configure-buildgradle) - [More examples of FastDeploy Android](#more-examples-of-fastdeploy-android) ## Create a new Java class and Define the native API
```java public class PicoDet { protected long mNativeModelContext = 0; // Context from native. protected boolean mInitialized = false; // ... // Bind predictor from native context. private static native long bindNative(String modelFile, String paramsFile, String configFile, int cpuNumThread, boolean enableLiteFp16, int litePowerMode, String liteOptimizedModelDir, boolean enableRecordTimeOfRuntime, String labelFile); // Call prediction from native context. private static native long predictNative(long nativeModelContext, Bitmap ARGB8888Bitmap, boolean saved, String savedImagePath, float scoreThreshold, boolean rendering); // Release buffers allocated in native context. private static native boolean releaseNative(long nativeModelContext); // Initializes at the beginning. static { FastDeployInitializer.init(); } } ``` These interfaces, marked as native, are required to be implemented by JNI and should be available to call for Class PicoDet in the Java layer. For the complete PicoDet Java code, please refer to [PicoDet.java](../../../java/android/fastdeploy/src/main/java/com/baidu/paddle/fastdeploy/vision/detection/PicoDet.java). The functions are described seperately: - `bindNative`: Initialize the model resource in the C++ layer. It returns a cursor (of type long) to the model if it is successfully initialized, otherwise it returns a 0 cursor. - `predictNative`: Run the prediction code in th C++ layer with the initialized model cursor. If executed successfully, it returns a cursor to the result, otherwise it returns a 0 cursor. Please note that the cursor needs to be released after the current prediction, please refer to the definition of the `predict` funtion in [PicoDet.java](../../../java/android/fastdeploy/src/main/java/com/baidu/paddle/fastdeploy/vision/detection/PicoDet.java) for details. - `releaseNative`: Release model resources in the C++ layer according to the input model cursor. ## Generate JNI function definition with Android Studio Hover over the native function defined in Java and Android Studio will prompt if you want to create a JNI function definition. Here, we create the definition in a pre-created c++ file `picodet_jni.cc`. - Create a JNI function definition with Android Studio:  - Create the definition in picodet_jni.cc:  - The JNI function definition created:  You can create JNI function definitions corresponding to other native functions referring to this process. ## Implement JNI function in the C++ layer Here is an example of the PicoDet JNI layer implementation. For the complete C++ code, please refer to [android/app/src/main/cpp](../../../examples/vision/detection/paddledetection/android/app/src/main/cpp/). ```C++ // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include