【问题标题】:Unsatisfiedlinkerror OpenALPR test project for androidUnsatisfiedlinkerror OpenALPR 安卓测试项目
【发布时间】:2015-06-13 07:04:00
【问题描述】:

这几天我正在尝试构建OpenALPR example project for Android。它构建并启动,但在调用本机方法以识别它之后会出现异常:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)

Caused by: java.lang.UnsatisfiedLinkError: Native method not found: org.openalpr.AlprJNIWrapper.recognizeWithCountryRegionNConfig:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)Ljava/lang/String;
at org.openalpr.AlprJNIWrapper.recognizeWithCountryRegionNConfig(Native Method)
at org.openalpr.app.AlprFragment$AlprTask.doInBackground(AlprFragment.java:78)
at org.openalpr.app.AlprFragment$AlprTask.doInBackground(AlprFragment.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
... 4 more

我什么都做不了,它总是出现。

应用程序.mk

APP_ABI := armeabi-v7a
APP_CPPFLAGS := -frtti -fexceptions
APP_STL := gnustl_static

在我所有尝试之后的 Android.mk

LOCAL_PATH := $(call my-dir)
LIB_PATH := $(LOCAL_PATH)/../libs/armeabi-v7a

include $(CLEAR_VARS)

LOCAL_MODULE := leptonica
LOCAL_SRC_FILES := 3rdparty/liblept.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := tesseract
LOCAL_SRC_FILES := 3rdparty/libtess.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := simpleini
LOCAL_SRC_FILES := 3rdparty/libsimpleini.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := support
LOCAL_SRC_FILES := 3rdparty/libsupport.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := openalpr
LOCAL_SRC_FILES := 3rdparty/libopenalpr-static.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)

OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=off

include d:\Other\robovisor_mobile\OpenCV-android-sdk\sdk\native\jni\OpenCV.mk

LOCAL_MODULE := openalpr-native
SOURCE_LIST := $(wildcard $(LOCAL_PATH)/*.cpp)
HEADER_LIST := $(wildcard $(LOCAL_PATH)/*.h)
LOCAL_SRC_FILES := AlprJNIWrapper.cpp
LOCAL_SRC_FILES += $(HEADER_LIST:$(LOCAL_PATH)/%=%)
LOCAL_SRC_FILES += $(SOURCE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_EXPORT_C_INCLUDES += /home/sujay/builds/src/openalpr/src/openalpr
LOCAL_EXPORT_C_INCLUDES += /home/sujay/builds/src/OpenCV-2.4.9-android-sdk/sdk/native/include
FILE_LIST := $(foreach dir, $(LOCAL_EXPORT_C_INCLUDES), $(wildcard $(dir)/*.cpp))
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)

LOCAL_C_INCLUDES += /home/sujay/builds/src/openalpr/src/openalpr
LOCAL_C_INCLUDES += /home/sujay/builds/src/OpenCV-2.4.9-android-sdk/sdk/native/include
LOCAL_C_INCLUDES += /home/sujay/tools/android-ndk-r10/platforms/android-19/arch-arm/usr/include
LOCAL_SHARED_LIBRARIES += tesseract leptonica
LOCAL_STATIC_LIBRARIES += openalpr support simpleini
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

AlprJNIWrapper.cpp 包含我需要的原生函数

/**
 * Created by sujay on 13/11/14.
 */
#include <string>
#include <sstream>
#include <cstdio>
#include <iostream>

// openCV includes
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

// open alpr includes
#include "support/filesystem.h"
#include "support/timing.h"
#include "alpr.h"
#include "cjson.h"

#include "AlprJNIWrapper.h"
#include "AlprNative.h"

using namespace alpr;

JNIEXPORT jstring JNICALL Java_org_openalpr_AlprJNIWrapper_recognize(JNIEnv *env,
        jobject object, jstring jimgFilePath, jint jtopN)
{
    jstring defaultCountry = env->NewStringUTF("us");
    jstring defaultRegion = env->NewStringUTF("");
    jstring defaultConfigFilePath = env->NewStringUTF(CONFIG_FILE);
    return _recognize(env, object, defaultCountry, defaultRegion, jimgFilePath, defaultConfigFilePath, jtopN);
}

JNIEXPORT jstring JNICALL Java_org_openalpr_AlprJNIWrapper_recognizeWithCountryNRegion(
        JNIEnv *env, jobject object, jstring jcountry,
        jstring jregion, jstring jimgFilePath, jint jtopN)
{
    jstring defaultConfigFilePath = env->NewStringUTF(CONFIG_FILE);
    return _recognize(env, object, jcountry, jregion, jimgFilePath, defaultConfigFilePath, jtopN);
}

JNIEXPORT jstring JNICALL Java_org_openalpr_AlprJNIWrapper_recognizeWithCountryRegionNConfig
  (JNIEnv *env, jobject object, jstring jcountry, jstring jregion,
          jstring jimgFilePath, jstring jconfigFilePath, jint jtopN)
{
    return _recognize(env, object, jcountry, jregion, jimgFilePath, jconfigFilePath, jtopN);
}

jstring _recognize(JNIEnv *env, jobject object,
        jstring jcountry, jstring jregion, jstring jimgFilePath,
        jstring jconfigFilePath, jint jtopN)
{

    const char* countryChars = env->GetStringUTFChars(jcountry, NULL);

    std::string country(countryChars);

    env->ReleaseStringUTFChars(jcountry, countryChars);

    if(country.empty())
    {
        country = "us";
    }

    const char* configFilePathChars = env->GetStringUTFChars(jconfigFilePath, NULL);

    std::string configFilePath(configFilePathChars);

    env->ReleaseStringUTFChars(jconfigFilePath, configFilePathChars);

    if(configFilePath.empty())
    {
        configFilePath = "/etc/openalpr/openalpr.conf";
    }

    const char* imgFilePath = env->GetStringUTFChars(jimgFilePath, NULL);

    int topN = jtopN;

    std::string response = "";

    cv::Mat frame;
    Alpr alpr(country, configFilePath);

    const char* regionChars = env->GetStringUTFChars(jregion, NULL);

    std::string region(regionChars);

    env->ReleaseStringUTFChars(jregion, regionChars);

    if(region.empty())
    {
        alpr.setDetectRegion(true);
        alpr.setDefaultRegion(region);
    }


    alpr.setTopN(topN);

    if (alpr.isLoaded() == false) {
        env->ReleaseStringUTFChars(jimgFilePath, imgFilePath);
        response = errorJsonString("Error initializing Open Alpr");
        return env->NewStringUTF(response.c_str());
    }

    if(fileExists(imgFilePath))
    {
        frame = cv::imread(imgFilePath);
        response = detectandshow(&alpr, frame, "");
    }
    else
    {
        response = errorJsonString("Image file not found");
    }
    env->ReleaseStringUTFChars(jimgFilePath, imgFilePath);
    return env->NewStringUTF(response.c_str());
}

JNIEXPORT jstring JNICALL Java_org_openalpr_AlprJNIWrapper_version
  (JNIEnv *env, jobject object)
{
    return env->NewStringUTF(Alpr::getVersion().c_str());
}

std::string detectandshow(Alpr* alpr, cv::Mat frame, std::string region) 
{
    std::vector < uchar > buffer;
    std::string resultJson = "";
    cv::imencode(".bmp", frame, buffer);

    std::vector < char > buffer1;

    for(std::vector < uchar >::iterator i = buffer.begin(); i < buffer.end(); i++) {
        buffer1.push_back(*i);
    }

    timespec startTime;
    getTimeMonotonic(&startTime);

    //std::vector < AlprResults > results = alpr->recognize(buffer);
    AlprResults results = alpr->recognize(buffer1);

    timespec endTime;
    getTimeMonotonic(&endTime);
    double totalProcessingTime = diffclock(startTime, endTime);

    //if (results.size() > 0)
    {
        resultJson = alpr->toJson(results/*, totalProcessingTime*/);
    }

    return resultJson;
}

std::string errorJsonString(std::string msg) 
{
    cJSON *root;
    root = cJSON_CreateObject();
    cJSON_AddTrueToObject(root, "error");
    cJSON_AddStringToObject(root, "msg", msg.c_str());

    char *out;
    out = cJSON_PrintUnformatted(root);

    cJSON_Delete(root);

    std::string response(out);

    free(out);
    return response;
}

AlprJNIWrapper.java 调用本地方法

/**
 * 
 */
package org.openalpr;

/**
 * @author sujay
 *
 */
public class AlprJNIWrapper implements Alpr {

        static { 
            System.loadLibrary("lept");
            System.loadLibrary("tess");
            System.loadLibrary("opencv_java");
            System.loadLibrary("openalpr-native");
        }

    /* (non-Javadoc)
     * @see org.openalpr.Alpr#recognize(java.lang.String, int)
     */
    @Override
    public native String recognize(String imgFilePath, int topN);

    /* (non-Javadoc)
     * @see org.openalpr.Alpr#recognizeWithCountryNRegion(java.lang.String, java.lang.String, java.lang.String, int)
     */
    @Override
    public native String recognizeWithCountryNRegion(String country, String region,
            String imgFilePath, int topN);

    /* (non-Javadoc)
     * @see org.openalpr.Alpr#recognizeWithCountryRegionNConfig(java.lang.String, java.lang.String, java.lang.String, java.lang.String, int)
     */
    @Override
    public native String recognizeWithCountryRegionNConfig(String country,
            String region, String imgFilePath, String configFilePath, int topN);

    /*
     * (non-Javadoc)
     * @see org.openalpr.Alpr#version()
     */
    @Override
    public native String version();
}

已编辑

我的手机上有关于处理器的信息。

Processor       : ARMv7 Processor rev 3 (v7l)
processor       : 0
BogoMIPS        : 1993.93

Features        : swp half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpv4
idiva idivt
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xc07
CPU revision    : 3

再次编辑

我试图在结果 .so 文件中获取有关函数的信息,这就是我得到的:

Symbol table '.dynsym' contains 6 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 00000000     0 FUNC    GLOBAL DEFAULT  UND __cxa_finalize
     2: 00000000     0 FUNC    GLOBAL DEFAULT  UND __cxa_atexit
     3: 00002004     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
     4: 00002004     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
     5: 00002004     0 NOTYPE  GLOBAL DEFAULT  ABS _end

我从:

<ndk_path>\toolchains\arm-linux-androideabi-4.8\prebuilt\windows\bin>arm-linux-androideabi-readelf.exe 
-Ws <projects_path>\libs\armeabi-v7a\libopenalpr-native.so

我做错了吗?或者.so文件中真的没有我的函数?

【问题讨论】:

  • 样本可能已过时。虽然开发人员更新了 NDK 库,但他们没有在示例项目中进行以下更改。或者你的 ARM 没有 lib 版本。
  • 没有库,但项目构建?我希望不会。
  • 当然有库。但没有适合您手臂的版本。这样项目将毫无问题地编译。但是,它不适用于您的特定设备。假设您的设备是基于 MIPS 的,并且该项目只有 arm-v7。或者你的设备是 Intel x86。
  • 你能看到吗:APP_ABI := armeabi-v7a,你的设备臂是什么?
  • 添加了有关要提问的处理器的信息。

标签: java android c++ android-ndk


【解决方案1】:

可能有几个问题。
首先,您在不受支持的设备上运行示例,例如为 armeabi-v7 构建的 lib(由于 make 文件中的 APP_ABI := armeabi-v7a 设置肯定如此)并且您的设备是 intel x86 或低于 7 armeabi 版本等。
可能是该示例在 lib 项目已更新时已过时,因此某些方法名称已更改或方法已被删除,等等。
还编译的 NDK 库对包敏感,因此如果您将 JNI 类放入不同的包中,它也不会工作。
此外,必须将本机库 .so-file 放置到项目的正确位置,而不是通常放置 jar-libs 的 libs 文件夹。它与 android studio 有点不同:

...\src\main\jniLibs\aremabi-v7a\libYourLib.so(适用于 aremabi-v7a 版本)
...\src\main\jniLibs\x86\libYourLib.so(适用于x86版本等)

更新:
不要忘记尊重最低 API 级别,即 19。应用程序无法在 API 级别较低的设备上运行,我的意思是您可以在项目中更改最低 API 级别 - 不要这样做。
这里的问题是libopenalpr-native.so 的名称中有一个破折号- 字符。 Dash 是 AOS 中资源命名的受限字符。所以我把它换成了“_”和app works now 并在这里替换它:

System.loadLibrary("openalpr_native");

我没有使用您的 .so 版本,而只使用了项目一中包含的版本。
更新
看看:http://prntscr.com/6w6qfx 你的库只有 5kb 与原来的 1.7Mb 相比,肯定有问题。它在评论中解释了您的问题:

为什么 System.loadLibrary("openalpr-native"); 没有错误?一世 只是无法理解这种情况 - 创建 libopenalpr-native.so 文件,它加载到程序,没有方法。

您可以只使用示例项目中包含的原始库吗?只是为了测试。

【讨论】:

  • 那为什么编译时没有报错呢?为什么System.loadLibrary("openalpr-native"); 没有错误?我只是无法理解这种情况 - 创建libopenalpr-native.so 文件,它加载到程序,没有方法。
  • 试图为另一个处理器获取库 - java.lang.UnsatisfiedLinkError: Couldn't load lept from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.robovisormobile-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.robovisormobile-1, /vendor/lib, /system/lib]]]: findLibrary returned null
  • 你确定你没有改包名吗?
  • 是的,我确定。刚刚复制了另一个 .so 文件。
  • 不,我的意思是在使用 lib 和 JNI 的应用程序中。原来的 lib src 必须保持不变。我的意思是您应该按原样尝试完全不做任何更改的示例应用程序。因为我猜你是根据 sample 或 lib 制作了自己的应用程序。
【解决方案2】:

在您的库中找不到正确的方法是由于名称不匹配,您应该在 C++ 代码中使用 extern "C" 包装您的 JNI 函数代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 2014-08-09
    • 1970-01-01
    相关资源
    最近更新 更多