Step 3 : Copying the Custom Operations to the TensorFlow build path

This is the crucial step, which involves you copying the headers for your custom operations to your TensorFlow Lite build path.

To do this, navigate to tensorflow/lite/java/src/main/native and paste all your header files for the custom operations here.

For example, I pasted the header files for my custom ops ( predict.h , normalize.h and extract_features.h ) here.

Pro Tip : If you don’t have the header files ( .h ) and only have the source ( .cc ) files for your ops, simply rename the .cc file to .h and it should work. The C++ build system knows no difference between these two, so it’s all the same conventions.

Now, open the builtin_ops_jni.cc file and add headers for the custom ops that you just added above.

After modifying, it should look something like this :

You can see I added imports for the header files at the very top.

Next up, edit the BUILD file in the same directory as a text file. Find the block named hdrs and add your header files that you just copied above.

The result should look like :

And that’s it!

Off to the next step now— building the TensorFlow dependency with these custom ops.

Step 4 : Building the Custom TensorFlow Dependency

Go back to the root directory of your cloned GitHub repository and type in the following command to compile the TensorFlow Lite build :

That’s it! Go grab a beer and wait for the build process to complete… It’s going to take a while (took me around 15–20 mins on a 13' MacBook Pro).

Note : If you encounter a build error that says something like this—

Don’t fret! Simply go to the following link and scroll down until you find the Command Line Tools option. Download and extract the downloaded zip file and install the latest Android SDK platform tools and build tools using the sdkmanager binary inside the bin folder.

A detailed description on doing this can be found here:

TL;DR : You need to execute the following commands :

./sdkmanager "platforms;android-27"

./sdkmanager "build-tools;27.0.2"

Once the build is complete, you should find the resultant .aar file in the following folder :

bazel-genfiles/tensorflow/contrib/lite/java/

Simply cd here and you should see tensorflow-lite.aar , which is the custom TensorFlow Lite dependency we just built.

Step 5 : Importing the .aar file into your Android Project

Considering the steps we’ve gone through above, this step is relatively easy.

To import an .aar file into your Android Project, simply open your project in Android Studio and then go to File -> New -> New Module

Then from the options, select the option named, Import .JAR/.AAR package.

Next, specify the location for your .aar file (the same that was inside bazel-genfiles/tensorflow/contrib/lite/java/) and press Finish.

After this is done, you should see a new module added to your project with the name tensorflow-lite

To include this module in your app, simply go to your app’s build.gradle file and add the following line under dependencies :

implementation project(':tensorflow-lite')

Note : Be sure to remove the default TensorFlow Lite dependency that you had added to your project earlier before adding this custom TensorFlow Lite build. Otherwise, you might get some unwanted results.

And that’s about it! :) Congratulations on making it to the end. Hopefully you managed to produce your own TensorFlowLite build.

If you faced any errors, or if you felt there was something missing in the post, feel free to let me know in the comments below, and I’ll be happy to take another look!

I’ll see you later! 👋🏼👋🏼👋🏼

Thanks for reading! If you enjoyed this story, please click the 👏 button and share to find others! Feel free to leave a comment 💬 below.

Have feedback? Let’s connect on Twitter.

Do you want to start building awesome Android Apps? Check out my course on Coding Bocks .

Discuss this post on Hacker News and Reddit