Build tensorflow-lite-select-tf-ops.aar and tensorflow-lite.aar files with Colab.

George Soloupis
5 min readOct 2, 2022

Written by George Soloupis ML GDE.

This is a guide of how you can shrink the final size of your android application’s .apk by building tensorflow-lite-select-tf-ops.aar and tensorflow-lite.aar files with Colab without the need of Docker or your personal PC’s environment.

When you are building applications which will require the deployment of machine learning models then TensorFlow Lite library is an excellent choice which comes with a large set of tools, a detailed guide and a lot of examples that can help you in your first steps. TensorFlow Lite builtin operator library though only supports a limited number of TensorFlow operators. To allow conversion, users can enable the usage of certain TensorFlow ops in their TensorFlow Lite model. However, running TensorFlow Lite models with TensorFlow ops requires pulling in the core TensorFlow runtime, which increases the TensorFlow Lite interpreter binary size for more than 100MB. For Android, you can avoid this by selectively building only required TensorFlow ops. If you find the documented procedure overwhelming and you do not want to use Docker or your personal PC’s runtime environment then follow along to see how you can build tensorflow-lite-select-tf-ops.aar and tensorflow-lite.aar files and save a great amount of the final .apk size of your project.

Colaboratory, or “Colab” for short, is a product from Google Research. Colab allows anybody to write and execute arbitrary python code through the browser, and is especially well suited to machine learning, data analysis and education. Anyone with a Google’s account having access to their personal Drive can open, modify and execute Python code.

Let’s follow the procedure to create the two .aar files that we need and which they will be bundled in the end inside the android project:

1. Check the Python’s version and the path. By September of 2022 you will need Python 3.7, TensorFlow’s branch r2.8 and Bazel version 4.2.1 .

2. Check that you are at the root path and then create folder and install the jdk.

3. Install android SDK.

4. Set environment paths.

5. Move at cmdline’s folder where sdkmanager file is and download additional android tools and libraries. You may need to agree to the license.

6. Install android NDK and make the /android folder executable.

7. Move again at the root folder, clone TensorFlow respository, rename it, move at the tensorflow folder and checkout branch r2.8.

8. Install Bazel with the help of Baselisk. Based on the specific TensorFlow branch you will download the required Bazel version. For this branch version 4.2.1 will be installed.

9. Go up one folder and run configure.py file. You will get prompted to provide some preferences. You should give:

“/usr/bin/python3’’
“/usr/local/lib/python3.7/dist-packages’’
”N’’
“N’’
“N’’
”-Wno-sign-compare’’
“y”

10. Finally you upload your .tflite file that contains the TensorFlow operators and requires the TensorFlow runtime at the root path and you execute:

By the time this colab was created a full set of archs was necessary i.e x86,x86_64,arm64-v8a,armeabi-v7a

WARNING = The procedure to generate the tensorflow-lite-select-tf-ops.aar requires high number of CPUs and high RAM. You should consider connecting this colab to a Google Cloud VM instance with minimum of 24 CPUs and 100GB of RAM and the procedure will take 1,5 hours. If you don’t then the default runtime will be terminated after 6 hours and the procedure will not have ended.

11. Having the two .aar files inside bazel-bin/tmp folder you can copy paste them inside your android folder in a libs folder.

12. Inside app’s build.gradle file you can use the dependencies:

Check this documentation on how you can use custom .aar files inside android.

13. Finally you build your project and you check how many MBs you have saved from your final .apk file.

All the code can be viewed at this github repository where you can download it and use it at your disposal inside a Colab environment.

Conclusion
If you have a custom .tflite file that contains TensorFlow operators that will require the core TensorFlow runtime inside your android project then you can follow the above procedure and reduce the binary size of the TensorFlow Lite interpreter. The two .aar files that are generated can be placed inside the android project and reduce a significant amount of the final .apk

--

--

George Soloupis

I am a pharmacist turned android developer and machine learning engineer. Right now I’m a senior android developer at Invisalign, a ML & Android GDE.