- Home /
How to intergrate Tensorflow to Android native plugin
I know Unity has release TensorFlowSharp. But is it possible to intergrate Tensorflow android to Android plugin. I have import to gradle of Android plugin:
implementation "org.tensorflow:tensorflow-android:1.8.0"
also copy tensorflow aar to plugin folder of Unity project. But i am stuck in building Unity app in Android. Error:
com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: Unable to pre-dex
CommandInvokationFailure: Gradle build failed.
/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java -classpath "/Applications/Unity/PlaybackEngines/AndroidPlayer/Tools/gradle/lib/gradle-launcher-4.0.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx2048m" "assembleRelease"
stderr[
Dex: Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
Answer by DoritoDog · Jun 21, 2019 at 04:07 PM
In case anyone has this problem in the future, these steps worked for me:
Download the tensorflow lite aar at https://mvnrepository.com/artifact/org.tensorflow/tensorflow-lite/1.13.1
Create a "libs" folder in your plugin's root directory and add the downloaded AAR there
In your plugin's gradle file add
compileOnly files('libs/tensorflow-lite-1.13.1.aar')
Add the AAR to your Unity project in Assets/Plugins/Android
compileOnly makes it so that gradle doesn't include the AAR in your plugin, but can still access it at runtime if you have added it to your Unity project's plugins directory.