2017-09-14 54 views
0

我想在Android Studio中使用JNI, 我在示例native-lib下添加了JNI函數。其通過Android Studio中默認創建CPP(之後加入C++支持)java.lang.UnsatisfiedLinkError:dlopen失敗64位而不是32位,而.so應該內置在x86

然而,我面臨一個錯誤

java.lang.UnsatisfiedLinkError: No implementation found for boolean com.example.user.project.Tracker.isTracking_0(boolean) (tried Java_com_example_user_project_Tracker_isTracking_10 and Java_com_example_user_project_Tracker_isTracking_10__Z)

我還試圖把它添加到MainActivity

static { 
     System.loadLibrary("tracker"); 
    } 

但我會得到另一個異常

java.lang.UnsatisfiedLinkError: dlopen failed: "/data/app/com.example.user.project/lib/x86/libtracker.so" is 64-bit instead of 32-bit

我可以看到我的圖書館已成功建成,這是軌道的消息也顯示,當構建

Build tracker x86 [1/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/fhog.cpp.o [2/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/tracker.cpp.o [3/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/ctracker.cpp.o [4/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/native-lib.cpp.o [5/5] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/x86/libtracker.so Build tracker mips [1/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/tracker.cpp.o [2/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/fhog.cpp.o [3/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/ctracker.cpp.o [4/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/native-lib.cpp.o [5/5] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/mips/libtracker.so Build tracker x86_64 [1/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/tracker.cpp.o [2/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/fhog.cpp.o [3/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/ctracker.cpp.o [4/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/native-lib.cpp.o [5/5] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/x86_64/libtracker.so Build tracker armeabi [1/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/tracker.cpp.o [2/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/fhog.cpp.o [3/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/ctracker.cpp.o [4/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/native-lib.cpp.o [5/5] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/armeabi/libtracker.so Build tracker armeabi-v7a [1/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/tracker.cpp.o [2/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/fhog.cpp.o [3/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/ctracker.cpp.o [4/5] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/native-lib.cpp.o [5/5] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libtracker.so

這是我CMakeLists

set (pathToOpenCv /home/user/OpenCV-android-sdk3.2) 
set (pathToProject /home/user/AndroidStudioProjects/project) 

cmake_minimum_required(VERSION 3.4.1) 

set(CMAKE_VERBOSE_MAKEFILE on) 

include_directories(${pathToOpenCv}/sdk/native/jni/include) 

FILE(GLOB_RECURSE cppfiles "src/main/cpp/*.cpp") 

add_library(lib_opencv SHARED IMPORTED) 

add_library(tracker SHARED ${cppfiles}) 

set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so) 

find_library(
       log-lib 
       log) 
target_link_libraries(tracker 
         ${log-lib} 
         lib_opencv 
         ) 

This is my native-lib.cpp

#include <jni.h> 
#include <string> 
extern "C" 
JNIEXPORT jboolean JNICALL Java_com_example_user_project_Tracker_isTracking_10 
     (JNIEnv *env, jboolean boolean) { 
    printf("\n c-boolean: %d ", boolean); 
    if (boolean == JNI_TRUE) { 
     printf("true"); 
    } else { 
     printf("false"); 
    } 
    boolean = false; 
    return boolean; 
}; 

那麼,Android工作室無法從我的.so庫中找到該方法,並出現java.lang.UnsatisfiedLinkError,這可能是什麼原因?另外,我不是爲什麼我的x86庫是64位而不是32位。

感謝

UPDATE

只需仔細檢查了Android日誌,我認爲它不正確加載跟蹤庫。我認爲現在的64位錯誤是主要問題。更新問題標題

java.lang.UnsatisfiedLinkError: dlopen failed: "/data/app/com.example.user.project/lib/x86/libtracker.so" is 64-bit instead of 32-bit

在的build.gradle 我已經設置了abiFilter,我的AVD是x86的設備

應用插件: 'com.android.application'

android { 
    compileSdkVersion 26 
    buildToolsVersion "26.0.1" 
    defaultConfig { 
     applicationId "com.example.user.project" 
     minSdkVersion 23 
     targetSdkVersion 26 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
     externalNativeBuild { 
      cmake { 
       cppFlags "" 
      } 
     } 
    } 

    sourceSets.main { 
     jniLibs.srcDir 'src/main/jniLibs' 
     jni.srcDirs = [] 
    } 



    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    externalNativeBuild { 
     cmake { 
      path "CMakeLists.txt" 
     } 
    } 

    defaultConfig { 
     ndk { 
      abiFilters "armeabi", "armeabi-v7a", "x86", "x86_64", "mips" 
     } 

    } 
} 

// call regular ndk-build(.cmd) script from app directory 


dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:26.+' 
    compile 'com.android.support.constraint:constraint-layout:1.0.2' 
    testCompile 'junit:junit:4.12' 
    compile project(':openCVLibrary320') 
} 
+1

我最終再次在Android Studio上重新創建新項目,解決了64位問題。但我仍然不明白是什麼導致了64位而不是32位的問題。 – chan3600

回答

0

這裏是什麼導致你的問題:

sourceSets.main { 
    jniLibs.srcDir 'src/main/jniLibs' 
    jni.srcDirs = [] 
} 

這些定義不安全地共存於externalNativeBuild

實質上,在新的Android Studio jni.srcDirs是不相關的。它通過分析cmake或ndk-build日誌來查找要使用哪些C/++文件。但即使您使用externalNativeBuild,AS也會將在jniLibs中找到的二進制文件複製到最終的APK中。不知怎的,一個錯誤版本的libtracker.so卡在src/main/jniLibs/x86目錄中。重要的是要記住,構建或重建通常不會在那裏刪除/更新二進制文件。

+0

謝謝,所以我手動刪除x86文件夾中的libtracker.so。並再次建立,現在它工作:D – chan3600

相關問題