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

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

我正在尝试制作一个基于 TensorFlow Lite Object Detection Android Demo 的对象检测应用程序。当我 运行 调试变体时一切正常,但是当我 运行 发布变体时我得到以下错误:

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')
 }

在示例应用程序中,有 this line of code in TFLiteObjectDetectionAPIModel.java:

options.setUseXNNPACK(true);

正在为 setUseXNNPACK() 审查 the TFLite docs,这似乎是一个实验性功能(当时提出这个问题)。删除该行代码解决了发布版本的问题。