Search code examples
androidtensorflow-liteobject-detection-api

TensorFlow Lite Error in Release Build: java.lang.NoSuchMethodError: no non-static method "Lorg/tensorflow/lite/XnnpackDelegate;.<init>(JJ)V"


I am trying to make an Object Detection app based off the TensorFlow Lite Object Detection Android Demo. Everything works well when I run the debug variant, but when I run the release variant I get the following error:

2022-05-10 20:36:45.857 5791-5791/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app, PID: 5791
    java.lang.NoSuchMethodError: no non-static method "Lorg/tensorflow/lite/XnnpackDelegate;.<init>(JJ)V"
        at org.tensorflow.lite.NativeInterpreterWrapper.createXNNPACKDelegate(Native Method)
        at org.tensorflow.lite.NativeInterpreterWrapper.<init>(:11)
        at org.tensorflow.lite.NativeInterpreterWrapperExperimental.<init>(Unknown Source:0)
        at org.tensorflow.lite.a.<init>(Unknown Source:2)
        at u4.b.a(:11)

build.gradle (:app)

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 30
        targetSdkVersion 31
        versionCode 1
        versionName "1.0.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        signingConfig signingConfigs.debug
    }

    buildFeatures {
        viewBinding true
        mlModelBinding true
    }

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            minifyEnabled false
            testCoverageEnabled true
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    
    kotlinOptions {
        jvmTarget = '1.8'
    }
    
    flavorDimensions "tfliteInference"
    productFlavors {
        interpreter {
            dimension "tfliteInference"
        }
    }
    
    androidResources {
        noCompress 'tflite'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
    implementation 'org.tensorflow:tensorflow-lite-support:0.4.0'
    implementation 'org.tensorflow:tensorflow-lite-metadata:0.4.0'
    implementation project(path: ':lib_interpreter')
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
    implementation 'androidx.core:core-splashscreen:1.0.0-beta02'
    implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-savedstate:2.4.1'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
    implementation 'com.google.code.gson:gson:2.9.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

build.gradle (:lib_interpreter)

apply plugin: 'com.android.library'

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        minSdkVersion 30
        targetSdkVersion 31

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    androidResources {
        noCompress 'tflite'
    }
    lint {
        abortOnError false
        checkReleaseBuilds false
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation('org.tensorflow:tensorflow-lite:2.8.0')
    implementation ('org.tensorflow:tensorflow-lite-metadata:0.4.0')
 }

Solution

  • In the example app, there is this line of code in TFLiteObjectDetectionAPIModel.java:

    options.setUseXNNPACK(true);
    

    Reviewing the TFLite docs for setUseXNNPACK(), it seems that this is an experimental feature (at the time this question was raised). Removing that line of code resolved the problem with the release build.