Call WasmEdge functions from an NDK native app

In this section, we will demonstrate how to build an Android native application using C and the Android SDK. The native application uses the WasmEdge C SDK to embed the WasmEdge Runtime, and call WASM functions through WasmEdge.

Prerequisite

Android

Currently, WasmEdge only supports the arm64-v8a architecture on Android devices. You need an arm64-v8a Android simulator or a physical device with developer options turned on. WasmEdge requires Android 6.0 and above.

Android development CLI

In Ubuntu Linux, you can use the apt-get command to install Android debugging and testing tool adb. Using the adb shell command on the Ubuntu dev machine, you can open a CLI shell to execute commands on the connected Android device.

  1. sudo apt-get install adb

Android NDK

To compile programs with the wasmedge-tensorflow c api, you need to install the Android NDK. In this example, we use the latest LTS version (r23b).

Review of source code

The test.c uses the wasmedge-tensorflow c api to run a WebAssembly function. The WebAssembly file birds_v1.wasm is compiled from Rust source code and explained here.

  1. #include <wasmedge/wasmedge.h>
  2. #include <wasmedge/wasmedge-image.h>
  3. #include <wasmedge/wasmedge-tensorflowlite.h>
  4. #include <stdio.h>
  5. int main(int argc, char *argv[]) {
  6. /*
  7. * argv[0]: ./a.out
  8. * argv[1]: WASM file
  9. * argv[2]: tflite model file
  10. * argv[3]: image file
  11. * Usage: ./a.out birds_v1.wasm lite-model_aiy_vision_classifier_birds_V1_3.tflite bird.jpg
  12. */
  13. /* Create the VM context. */
  14. WasmEdge_ConfigureContext *ConfCxt = WasmEdge_ConfigureCreate();
  15. WasmEdge_ConfigureAddHostRegistration(ConfCxt, WasmEdge_HostRegistration_Wasi);
  16. WasmEdge_VMContext *VMCxt = WasmEdge_VMCreate(ConfCxt, NULL);
  17. WasmEdge_ConfigureDelete(ConfCxt);
  18. /* Create the image and TFLite import objects. */
  19. WasmEdge_ImportObjectContext *ImageImpObj = WasmEdge_Image_ImportObjectCreate();
  20. WasmEdge_ImportObjectContext *TFLiteImpObj = WasmEdge_TensorflowLite_ImportObjectCreate();
  21. WasmEdge_ImportObjectContext *TFDummyImpObj = WasmEdge_Tensorflow_ImportObjectCreateDummy();
  22. /* Register into VM. */
  23. WasmEdge_VMRegisterModuleFromImport(VMCxt, ImageImpObj);
  24. WasmEdge_VMRegisterModuleFromImport(VMCxt, TFLiteImpObj);
  25. WasmEdge_VMRegisterModuleFromImport(VMCxt, TFDummyImpObj);
  26. /* Init WASI. */
  27. const char *Preopens[] = {".:."};
  28. const char *Args[] = {argv[1], argv[2], argv[3]};
  29. WasmEdge_ImportObjectContext *WASIImpObj = WasmEdge_VMGetImportModuleContext(VMCxt, WasmEdge_HostRegistration_Wasi);
  30. WasmEdge_ImportObjectInitWASI(WASIImpObj, Args, 3, NULL, 0, Preopens, 1);
  31. /* Run WASM file. */
  32. WasmEdge_String FuncName = WasmEdge_StringCreateByCString("_start");
  33. WasmEdge_Result Res = WasmEdge_VMRunWasmFromFile(VMCxt, argv[1], FuncName, NULL, 0, NULL, 0);
  34. WasmEdge_StringDelete(FuncName);
  35. /* Check the result. */
  36. if (!WasmEdge_ResultOK(Res)) {
  37. printf("Run WASM failed: %s\n", WasmEdge_ResultGetMessage(Res));
  38. return -1;
  39. }
  40. WasmEdge_ImportObjectDelete(ImageImpObj);
  41. WasmEdge_ImportObjectDelete(TFLiteImpObj);
  42. WasmEdge_ImportObjectDelete(TFDummyImpObj);
  43. WasmEdge_VMDelete(VMCxt);
  44. return 0;
  45. }

Build

Install dependencies

Use the following commands to download WasmEdge for Android on your Ubuntu dev machine.

  1. wget https://github.com/WasmEdge/WasmEdge/releases/download/0.10.0/WasmEdge-0.10.0-android_aarch64.tar.gz
  2. wget https://github.com/second-state/WasmEdge-image/releases/download/0.10.0/WasmEdge-image-0.10.0-android_aarch64.tar.gz
  3. wget https://github.com/second-state/WasmEdge-tensorflow/releases/download/0.10.0/WasmEdge-tensorflowlite-0.10.0-android_aarch64.tar.gz
  4. wget https://github.com/second-state/WasmEdge-tensorflow-deps/releases/download/0.10.0/WasmEdge-tensorflow-deps-TFLite-0.10.0-android_aarch64.tar.gz
  5. tar -zxf WasmEdge-0.10.0-android_aarch64.tar.gz
  6. tar -zxf WasmEdge-image-0.10.0-android_aarch64.tar.gz -C WasmEdge-0.10.0-Android/
  7. tar -zxf WasmEdge-tensorflowlite-0.10.0-android_aarch64.tar.gz -C WasmEdge-0.10.0-Android/
  8. tar -zxf WasmEdge-tensorflow-deps-TFLite-0.10.0-android_aarch64.tar.gz -C WasmEdge-0.10.0-Android/lib/

Compile

The following command compiles the C program to a.out on your Ubunu dev machine.

  1. (/path/to/ndk)/toolchains/llvm/prebuilt/(HostPlatform)/bin/aarch64-linux-(AndroidApiVersion)-clang test.c -I./WasmEdge-0.10.0-Android/include -L./WasmEdge-0.10.0-Android/lib -lwasmedge-image_c -lwasmedge-tensorflowlite_c -ltensorflowlite_c -lwasmedge_c

Run

Push files onto Android

Install the compiled program, Tensorflow Lite model file, test image file, as well as WasmEdge shared library files for Android, onto the Android device using adb from your Ubuntu dev machine.

  1. adb push a.out /data/local/tmp
  2. adb push birds_v1.wasm /data/local/tmp
  3. adb push lite-model_aiy_vision_classifier_birds_V1_3.tflite /data/local/tmp
  4. adb push bird.jpg /data/local/tmp
  5. adb push ./WasmEdge-0.10.0-Android/lib /data/local/tmp

Run the example

Now you can run the compiled C program on the Android device via a remote shell command. Run adb shell from your Ubuntu dev machine.

  1. $ adb shell
  2. sirius:/ $ cd /data/local/tmp
  3. sirius:/data/local/tmp $ export LD_LIBRARY_PATH=/data/local/tmp/lib:$LD_LIBRARY_PATH
  4. sirius:/data/local/tmp $ ./a.out birds_v1.wasm lite-model_aiy_vision_classifier_birds_V1_3.tflite bird.jpg
  5. INFO: Initialized TensorFlow Lite runtime.
  6. 166 : 0.84705883