Denis Davydov Denis Davydov - 2 months ago 26
Android Question

NDK can't load library

I'm trying to make what should be the easiest thing a HelloWorld using NDK, but apparently I'm missing something. I've tried with 3 different tutorials and each and every time I get the same error ( UnsatisfiedLinkError and couldn't load ndk1 from loader. ). So let me explain:

I use ADT Eclipse as my Android environment. I've installed C/C++, I've downloaded NDK. And added the path as

export ANDROID_SDK="/Applications/adt-bundle-mac-x86_64-20131030/sdk"
export ANDROID_NDK="/Applications/adt-bundle-mac-x86_64-20131030/android-ndk-r9d"
export PATH="$PATH:$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools:$ANDROID_NDK"


Then I create a project in Eclipse named AndroidNDK1Sample. The package name is com.mamlambo.sample.ndk1
The activity name is AndroidNDK1SampleActivity and this is it's content:

package com.mamlambo.sample.ndk1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class AndroidNDK1SampleActivity extends Activity {

private native void helloLog(String logThis);

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_ndk1_sample);

helloLog("This will log to LogCat via the native call.");
}

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

}


I've created a jni folder in the root directory of the project and inside of it I have 2 files: Android.mk and native.c

Android.mk content:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_LDLIBS := -llog

LOCAL_MODULE := ndk1
LOCAL_SRC_FILES := native.c

include $(BUILD_SHARED_LIBRARY)


native.c content:

#include <jni.h>
#include <string.h>
#include <android/log.h>

#define DEBUG_TAG "NDK_AndroidNDK1SampleActivity"

void Java_com_mamlambo_sample_ndk1_AndroidNDK1SampleActivity_helloLog(JNIEnv * env, jobject this, jstring logThis)
{
jboolean isCopy;
const char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy);

__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis);

(*env)->ReleaseStringUTFChars(env, logThis, szLogThis);
}


So at this point if I'm not mistaken the coding part is over, now with my terminal I go to the root of my project and I run ndk-build, the result of which is:

Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 4 in ./AndroidManifest.xml
[armeabi] Compile thumb : ndk1 <= native.c
[armeabi] SharedLibrary : libndk1.so
[armeabi] Install : libndk1.so => libs/armeabi/libndk1.so


In my opinion the Warning has nothing to do with it. I've cleaned the project in eclipse and I ran it. And I get an error like UnsatisfiedLinkError and couldn't load ndk1 from loader.

Any hint will be really appreciated.

Thanks in advance.

Answer Source

I just found why all of this wasn't working. I was launching the app on an emulator on my computer running on Intel Atom CPU but NDK works only with ARM. Changed it now everything works fine.