3

I am developing an app requiring CPU information. I placed the cpuinfo library in the jni folder. but i got this error:

java.lang.UnsatisfiedLinkError: Couldn't load cpuinfo from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.iman.marashi.deviceinfo-2.apk,libraryPath=/data/app-lib/com.iman.marashi.deviceinfo-2]: findLibrary returned null

that error got from this code:

static {
    try {
        System.loadLibrary("cpuinfo");
        isAvailable = true;
    } catch (UnsatisfiedLinkError ule) {
        Log.e("Loading cpuinfo native library", "" + ule);
    }

cpuinfo.c code:

 #include <jni.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <cpu-features.h>
 #include <android/log.h>

 #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "jason", __VA_ARGS__)
 #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,   "jason", __VA_ARGS__)
 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,    "jason", __VA_ARGS__)
 #define LOGW(...) __android_log_print(ANDROID_LOG_WARN,    "jason", __VA_ARGS__)
 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,   "jason", __VA_ARGS__)

 const int cpu_features_list[] = {
     ANDROID_CPU_ARM_FEATURE_ARMv7,
     ANDROID_CPU_ARM_FEATURE_VFPv3,
     ANDROID_CPU_ARM_FEATURE_NEON,
     ANDROID_CPU_ARM_FEATURE_LDREX_STREX,
     ANDROID_CPU_ARM_FEATURE_VFPv2,
     ANDROID_CPU_ARM_FEATURE_VFP_D32,
     ANDROID_CPU_ARM_FEATURE_VFP_FP16,
     ANDROID_CPU_ARM_FEATURE_VFP_FMA,
     ANDROID_CPU_ARM_FEATURE_NEON_FMA,
     ANDROID_CPU_ARM_FEATURE_IDIV_ARM,
     ANDROID_CPU_ARM_FEATURE_IDIV_THUMB2,
     ANDROID_CPU_ARM_FEATURE_iWMMXt,
     ANDROID_CPU_X86_FEATURE_SSSE3,
     ANDROID_CPU_X86_FEATURE_POPCNT,
     ANDROID_CPU_X86_FEATURE_MOVBE
 };

 const char cpu_features_string_list[][20] = {
     "armv7",
     "vfpv3",
     "neon",
     "ldrex_strex",
     "vfpv2",
     "vfp_d32",
     "vfp_fp16",
     "vfp_fma",
     "neon_fma",
     "idiv_arm",
     "idiv_thumb2",
     "iwmmxt",
     "ssse3",
     "popcnt",
     "movbe"
 };

 void appendString(char *strDestination, char *strSource) {
     strcat(strDestination, " ");
     strcat(strDestination, strSource);
 }

 char* get_cpu_family() {
     if (android_getCpuFamily() == ANDROID_CPU_FAMILY_ARM) {
         return "ARM";
     } else if (android_getCpuFamily() == ANDROID_CPU_FAMILY_X86) {
         return "X86";
     } else if (android_getCpuFamily() == ANDROID_CPU_FAMILY_MIPS) {
         return "MIPS";
     } else {
         return "Unknown";
     }
 }

 char* get_cpu_features() {
     char cpu_features[256] = "";
     uint64_t features = android_getCpuFeatures();

     int i = 0;
     int features_length = sizeof(cpu_features_list) / sizeof(int);
     for (i = 0; i < features_length; i++) {
         if (features & cpu_features_list[i]) {
             appendString(cpu_features, cpu_features_string_list[i]);
         }
     }

     return cpu_features;
 }

 jstring
 Java_hardwareinfo_HardwareInfoActivity_getCpuFamilyFromJNI( JNIEnv* env,
                                                jobject thiz )
 {
     return (*env)->NewStringUTF(env, get_cpu_family());
 }

 jstring
 Java_hardwareinfo_HardwareInfoActivity_getCpuCountFromJNI( JNIEnv* env,
                                                jobject thiz )
 {
     return android_getCpuCount();
 }

 jstring
 Java_hardwareinfo_HardwareInfoActivity_getCpuFeaturesFromJNI( JNIEnv* env,
                                                jobject thiz )
 {
     char buffer[256];
     strlcpy(buffer, get_cpu_features(), sizeof buffer);
     return (*env)->NewStringUTF(env, buffer);
 }

Application.mk code:

# Build both ARMv5TE and ARMv7-A machine code.
APP_ABI := armeabi armeabi-v7a

Android.mk code:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cpuinfo
LOCAL_SRC_FILES := cpuinfo.c
ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
LOCAL_CFLAGS := -DHAVE_NEON=1
#LOCAL_SRC_FILES += cpuinfo-intrinsics.c.neon
endif
LOCAL_STATIC_LIBRARIES := cpufeatures
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
$(call import-module,cpufeatures)
JoshDM
  • 4,939
  • 7
  • 43
  • 72
  • 1
    Can you post your native library code too? There are plenty of similar questions on stack overflow already, do these solve your issue? – abcdef Mar 06 '15 at 06:14
  • I looked but could not find a solution for my case. –  Mar 06 '15 at 06:28
  • Can you also post the package and class name of class that implements getCpuFamilyFromJNI? – abcdef Mar 06 '15 at 06:40
  • class **HardwareInfoActivity** package **hardwareinfo;** –  Mar 06 '15 at 06:44
  • Does anything here help? http://stackoverflow.com/questions/3262440/how-to-resolve-the-java-lang-unsatisfiedlinkerror-in-ndk-in-android – abcdef Mar 06 '15 at 07:28

0 Answers0