简体   繁体   中英

java.lang.UnsatisfiedLinkError in NDK android studio

I am trying to make a simple program using NDK but i am getting the same error again and again i have applied all the solutions from stack overflow but still i am getting same error. Eventhough, my code compiles correctly and my libraries load correctly but when i run my app it throws "java.lang.UnsatisfiedLinkError" Exception .here is my complete code. plz help me . your help will be appretiated

here is my code

package com.example.nasir.ndk2exmp;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Log.e("JNI Message", HelloJNI());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    static {
        System.loadLibrary("HelloJNI");
    }
    public native String HelloJNI();

}

This is my Header File

#include <jni.h>


/* Header for class com_example_nasir_ndk2exmp_MainActivity */

#ifndef _Included_com_example_nasir_ndk2exmp_MainActivity
#define _Included_com_example_nasir_ndk2exmp_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
#undef com_example_nasir_ndk2exmp_MainActivity_MODE_PRIVATE
#define com_example_nasir_ndk2exmp_MainActivity_MODE_PRIVATE 0L
#undef com_example_nasir_ndk2exmp_MainActivity_MODE_WORLD_READABLE
#define com_example_nasir_ndk2exmp_MainActivity_MODE_WORLD_READABLE 1L
#undef com_example_nasir_ndk2exmp_MainActivity_MODE_WORLD_WRITEABLE
#define com_example_nasir_ndk2exmp_MainActivity_MODE_WORLD_WRITEABLE 2L
#undef com_example_nasir_ndk2exmp_MainActivity_MODE_APPEND
#define com_example_nasir_ndk2exmp_MainActivity_MODE_APPEND 32768L
#undef com_example_nasir_ndk2exmp_MainActivity_MODE_MULTI_PROCESS
#define com_example_nasir_ndk2exmp_MainActivity_MODE_MULTI_PROCESS 4L
#undef com_example_nasir_ndk2exmp_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING
#define com_example_nasir_ndk2exmp_MainActivity_MODE_ENABLE_WRITE_AHEAD_LOGGING 8L
#undef com_example_nasir_ndk2exmp_MainActivity_BIND_AUTO_CREATE
#define com_example_nasir_ndk2exmp_MainActivity_BIND_AUTO_CREATE 1L
#undef com_example_nasir_ndk2exmp_MainActivity_BIND_DEBUG_UNBIND
#define com_example_nasir_ndk2exmp_MainActivity_BIND_DEBUG_UNBIND 2L
#undef com_example_nasir_ndk2exmp_MainActivity_BIND_NOT_FOREGROUND
#define com_example_nasir_ndk2exmp_MainActivity_BIND_NOT_FOREGROUND 4L
#undef com_example_nasir_ndk2exmp_MainActivity_BIND_ABOVE_CLIENT
#define com_example_nasir_ndk2exmp_MainActivity_BIND_ABOVE_CLIENT 8L
#undef com_example_nasir_ndk2exmp_MainActivity_BIND_ALLOW_OOM_MANAGEMENT
#define com_example_nasir_ndk2exmp_MainActivity_BIND_ALLOW_OOM_MANAGEMENT 16L
#undef com_example_nasir_ndk2exmp_MainActivity_BIND_WAIVE_PRIORITY
#define com_example_nasir_ndk2exmp_MainActivity_BIND_WAIVE_PRIORITY 32L
#undef com_example_nasir_ndk2exmp_MainActivity_BIND_IMPORTANT
#define com_example_nasir_ndk2exmp_MainActivity_BIND_IMPORTANT 64L
#undef com_example_nasir_ndk2exmp_MainActivity_BIND_ADJUST_WITH_ACTIVITY
#define com_example_nasir_ndk2exmp_MainActivity_BIND_ADJUST_WITH_ACTIVITY 128L
#undef com_example_nasir_ndk2exmp_MainActivity_CONTEXT_INCLUDE_CODE
#define com_example_nasir_ndk2exmp_MainActivity_CONTEXT_INCLUDE_CODE 1L
#undef com_example_nasir_ndk2exmp_MainActivity_CONTEXT_IGNORE_SECURITY
#define com_example_nasir_ndk2exmp_MainActivity_CONTEXT_IGNORE_SECURITY 2L
#undef com_example_nasir_ndk2exmp_MainActivity_CONTEXT_RESTRICTED
#define com_example_nasir_ndk2exmp_MainActivity_CONTEXT_RESTRICTED 4L
#undef com_example_nasir_ndk2exmp_MainActivity_RESULT_CANCELED
#define com_example_nasir_ndk2exmp_MainActivity_RESULT_CANCELED 0L
#undef com_example_nasir_ndk2exmp_MainActivity_RESULT_OK
#define com_example_nasir_ndk2exmp_MainActivity_RESULT_OK -1L
#undef com_example_nasir_ndk2exmp_MainActivity_RESULT_FIRST_USER
#define com_example_nasir_ndk2exmp_MainActivity_RESULT_FIRST_USER 1L
#undef com_example_nasir_ndk2exmp_MainActivity_DEFAULT_KEYS_DISABLE
#define com_example_nasir_ndk2exmp_MainActivity_DEFAULT_KEYS_DISABLE 0L
#undef com_example_nasir_ndk2exmp_MainActivity_DEFAULT_KEYS_DIALER
#define com_example_nasir_ndk2exmp_MainActivity_DEFAULT_KEYS_DIALER 1L
#undef com_example_nasir_ndk2exmp_MainActivity_DEFAULT_KEYS_SHORTCUT
#define com_example_nasir_ndk2exmp_MainActivity_DEFAULT_KEYS_SHORTCUT 2L
#undef com_example_nasir_ndk2exmp_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL
#define com_example_nasir_ndk2exmp_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
#undef com_example_nasir_ndk2exmp_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL
#define com_example_nasir_ndk2exmp_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L
#undef com_example_nasir_ndk2exmp_MainActivity_MAX_NUM_PENDING_FRAGMENT_ACTIVITY_RESULTS
#define com_example_nasir_ndk2exmp_MainActivity_MAX_NUM_PENDING_FRAGMENT_ACTIVITY_RESULTS 65534L
#undef com_example_nasir_ndk2exmp_MainActivity_HONEYCOMB
#define com_example_nasir_ndk2exmp_MainActivity_HONEYCOMB 11L
#undef com_example_nasir_ndk2exmp_MainActivity_MSG_REALLY_STOPPED
#define com_example_nasir_ndk2exmp_MainActivity_MSG_REALLY_STOPPED 1L
#undef com_example_nasir_ndk2exmp_MainActivity_MSG_RESUME_PENDING
#define com_example_nasir_ndk2exmp_MainActivity_MSG_RESUME_PENDING 2L
/*
 * Class:     com_example_nasir_ndk2exmp_MainActivity
 * Method:    helloJNI
 * Signature: ()Ljava/lang/String;
 */



JNIEXPORT jstring JNICALL Java_com_example_nasir_ndk2exmp_MainActivity_HelloJNI
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

This is my C file

#include "com_example_nasir_ndk2exmp_MainActivity.h"

/*
 * Class:     com_example_nasir_ndk2exmp_MainActivity
 * Method:    helloJNI
 * Signature: ()Ljava/lang/String;
 */

JNIEXPORT jstring JNICALL Java_com_example_nasir_ndk2exmp_MainActivity_HelloJNI(JNIEnv * env, jobject obj)
{

  return (*env)->NewStringUTF(env, "Hello From JNI");

}

This is .mk file

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := HelloJNI
LOCAL_SRC_FILES := HelloJNI.c
include $(BUILD_SHARED_LIBRARY)

and this is build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.nasir.ndk2exmp"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        ndk{
            moduleName "HelloJNI"
        }


    }

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


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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:design:23.3.0'
} 

Your build.gradle disables the built-in call to ndk-build here:

jni.srcDirs = []

Are you sure the library is actually being built? If you do not see HelloJNI.so under the 'jniLibs' or 'libs' directory, you are not actually building any native code.

You don't really need the Android.mk file for a project this simple, but if you want to go this route ph0b has a great tutorial on using Android.mk with gradle. You would need to add a call to ndk-build from your build.gradle with something like this snippet from ph0b's tutorial:

// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
    } else {
        commandLine 'ndk-build', '-C', file('src/main').absolutePath
    }
}

If you choose to go with the experimental gradle support, the NDK samples document how to build and use native code with build.gradle. The build.gradle file from hello-jni is a good place to start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM