Android Studio中使用Gradle的UnsatisfiedLinkError

时间:2015-02-28 02:11:16

标签: android-studio gradle java-native-interface native

我最近从Eclipse迁移到Android Studio,但在访问本机功能时遇到了一些麻烦。调用的第一个本机函数是nativeClassInit()。我见过的关于这个问题的大多数内容涉及缺少“静态”标识符,但我已将其包含在MainActivity.java中。我已经尝试了几天,并且已经没有想法了。非常感谢任何建议!

System.loadLibrary函数不返回null,当我运行adb shell ls -l package / name / path / lib时,我能够看到应用程序的* .so文件:

-rwxr-xr-x system   system       5096 2015-03-27 18:32 libPixa.so
-rwxr-xr-x system   system     775728 2015-03-27 18:32 libgnustl_shared.so
-rwxr-xr-x system   system   24998736 2015-03-26 15:01 libgstreamer_android.so

我是Gradle的新手,所以请随意批评我的build.gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 18
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.sbimagingsystems.pixa"
        minSdkVersion 14
        targetSdkVersion 19

        ndk {
            moduleName "gstreamer_android" // Name of C++ module (i.e. libSeePlusPlus)
            moduleName "Pixa"
            cFlags "-std=c++11 -fexceptions" // Add provisions to allow C++11 functionality
            stl "gnustl_shared" // Which STL library to use: gnustl or stlport

        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDir 'src'
            res.srcDir 'res'
            assets.srcDir 'assets'

            jniLibs.srcDir 'src/main/libs'
            jni.srcDirs = ['src/main/jni']      // location of native code files
            //jniLibs.srcDirs = ['jniLibs']
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.0.0'
    compile files('libs/acra-4.5.0.jar')
    compile files('libs/dropbox-android-sdk-1.5.4.jar')
    compile files('libs/httpmime-4.0.3.jar')
    compile files('libs/json_simple-1.1.jar')
    compile files('libs/pinchzoom.jar')
    compile files('libs/universal-image-loader-1.8.7-snapshot-with-sources.jar')
}

local.properties:

gst.dir=/home/android-dev/AndroidDev/Installation/gstreamer
sdk.dir=/home/android-dev/Android/Sdk
ndk.dir=/home/android-dev/AndroidDev/Installation/android-ndk-r9c

MainActivity.java

public class MainActivity extends FragmentActivity implements
        SurfaceHolder.Callback, OnClickListener, OnItemClickListener
{

    private native void nativeInit(); // Initialize native code, build pipeline,
    // etc

    private native void nativeFinalize(); // Destroy pipeline and shutdown
    // native code

    private native void nativePlay(); // Set pipeline to PLAYING

    public native void nativePause(); // Set pipeline to PAUSED

    private static native boolean nativeClassInit(); // Initialize native class:
    // cache Method IDs for
    // callbacks

    // more code

    static
    {
        System.loadLibrary("Pixa");
        System.loadLibrary("gstreamer_android");

        nativeClassInit();
    }
}

Pixa.c:

/* List of implemented native methods */
static JNINativeMethod native_methods[] = {
  { "nativeInit", "()V", (void *) gst_native_init},
  { "nativeFinalize", "()V", (void *) gst_native_finalize},
  { "nativePlay", "()V", (void *) gst_native_play},
  { "nativePause", "()V", (void *) gst_native_pause},
  { "nativeSurfaceInit", "(Ljava/lang/Object;)V", (void *) gst_native_surface_init},
  { "nativeSurfaceFinalize", "()V", (void *) gst_native_surface_finalize},
  { "nativeClassInit", "()Z", (void *) gst_native_class_init},
  { "nativeStartRecord", "()V", (void *) gst_native_record_start},
  { "nativeStopRecord", "()V", (void *) gst_native_record_stop}
};

/* Library initializer */
jint JNI_OnLoad(JavaVM *vm, void *reserved) {
  JNIEnv *env = NULL;

  java_vm = vm;

  if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {
    __android_log_print (ANDROID_LOG_ERROR, "Receiver", "Could not retrieve JNIEnv");
    return 0;
  }
  jclass klass = (*env)->FindClass (env, "com/sbimagingsystems/pixa/MainActivity");
  (*env)->RegisterNatives (env, klass, native_methods, G_N_ELEMENTS(native_methods));

  pthread_key_create (&current_jni_env, detach_current_thread);
  return JNI_VERSION_1_4;
}

最后,LogCat:

02-27 18:06:41.799    8248-8248/com.sbimagingsystems.pixa E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.sbimagingsystems.pixa, PID: 8248
java.lang.UnsatisfiedLinkError: Native method not found: com.sbimagingsystems.pixa.MainActivity.nativeClassInit:()Z
        at com.sbimagingsystems.pixa.MainActivity.nativeClassInit(Native Method)
        at com.sbimagingsystems.pixa.MainActivity.<clinit>(MainActivity.java:3197)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1208)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
        at android.app.ActivityThread.access$900(ActivityThread.java:169)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5479)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

使用

jni.srcDirs = [] //disable automatic ndk-build call

禁用Android工作室的自动NDK构建。而是在命令行上使用ndk-build构建。这将生成您的.so文件,并且能够正确链接/