Ошибка TensorFlow Lite в сборке выпуска: java.lang.NoSuchMethodError: нет нестатического метода «Lorg/tensorflow/lite/XnnpackDelegate;.<init>(JJ)V»

Я пытаюсь создать приложение для обнаружения объектов на основе TensorFlow Lite Object Detection Демонстрация для Android. Все работает хорошо, когда я запускаю вариант отладки, но когда я запускаю вариант выпуска, я получаю следующую ошибку:

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 (: приложение)

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')
 }
0
0
42
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

В примере приложения есть эта строка кода в TFLiteObjectDetectionAPIModel.java:

options.setUseXNNPACK(true);

Рассматривая документы TFLite для setUseXNNPACK(), кажется, что это экспериментальная функция (на момент поднятия этого вопроса). Удаление этой строки кода решило проблему с релизной сборкой.

Другие вопросы по теме