Running into `Missing class org.tensorflow.lite.gpu.GpuDelegateFactory$Options` when building an APK
I've been putting together a prototype for a tflite model, and although the app is running smoothly in the emulator, I'm running into this error message when trying to build an apk file.
ERROR: Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in /home/dorian/StudioProjects/hello_world/build/app/outputs/mapping/release/missing_rules.txt.
ERROR: R8: Missing class org.tensorflow.lite.gpu.GpuDelegateFactory$Options (referenced from: void org.tensorflow.lite.gpu.GpuDelegate.<init>() and 1 other context)
FAILURE: Build failed with an exception.
...
I've tried a number of things, but so far nothing has helped. It seems to be a missing dependency, but it's unclear to me what to add. Also adding tensorflow to proguard-rules.pro didn't help either.
Anyone else ran into this and solved it?
Of course as soon as you post an issue, you figure out the solution. Following some suggestions in this thread, adding this to android/app/build.gradle ended up solving it:
dependencies {
implementation "org.tensorflow:tensorflow-lite-gpu-api:+"
}
Possibly a missing dependency somewhere in tflite_flutter?
Caveat
For production cases it is preferable to specify the version you want to work with rather than leave it on auto-pilot with "+"
Tensorflow though great is famous for having breaking changes across versions, leaving '+' means each time a build is made it should try to pick the latest version, which might end up one day breaking your workflow someday especially for CI/CD pipelines
Preferable to specify a stable version like this instead:
dependencies {
implementation "org.tensorflow:tensorflow-lite-gpu-api:2.12.0"
}
Spending time searching the correct version match might be a pain but it will save you time in the future