首页 > 解决方案 > 它说“尝试加载库”然后应用程序退出而没有任何信息

问题描述

我已经下载了一个使用 tensorflow 实现情感识别的 android 程序。我已经完成了环境配置的工作。而且我发现当我使用识别应用程序时,日志会说,"Trying to load lib /data/app-lib/com.example.alex.opencvdemo-1/libtensorflow_inference.so 0xa4f77c88"然后应用程序将退出。我不知道如何解决这个问题

这是我的 loadLibrary 代码

static {
        //load libtensorflow_inference.so
      //System.load("/app/com.example.alex.opencvdemo/libs/libtensorflow_interface.so");
        System.loadLibrary("tensorflow_inference");
        Log.e("tensorflow","libtensorflow_inference.so is successfully load");
    }

以下是我的 build.gradle 应用程序:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.alex.opencvdemo"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
        multiDexEnabled true
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    buildTypes {
        debug {
            ndk { abiFilters "armeabi-v7a","x86"}
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets{
        main {
            jni.srcDirs = []
            //jni库的调用会到资源文件夹下libs里面找so文件
            jniLibs.srcDirs = ['libs']
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')
    implementation 'com.android.support:appcompat-v7:28.0.0-beta01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    compile project(':openCVLibrary342')
    implementation files('libs/libandroid_tensorflow_inference_java.jar')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
    destinationDir file("$buildDir/native-libs")
    baseName 'native-libs'
    from fileTree(dir: 'libs', include: '**/*.so')
    into 'lib/'
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
}

标签: androidtensorflowandroid-ndk

解决方案


我之前遇到的可能原因

  1. 更好地检查您的本机库是否与“tensorflow_inference”匹配。因为在你的 build.gradle 它显示了 native_lib 类似的东西。
  2. 验证 tensorflow_inference 是否已添加到 Cmakelists.txt 中。

  3. 如果您使用 openCV,请确保您已正确导入本机库


推荐阅读