I am using Eclipse to experiment Android NDK programming. My platform info is as follows:
- Windows 7.
- Eclipse platform. I can use this eclipse platform to build android apps without hassle; no problem with it.
- Cygwin with development package installed. The version of "make" is GNU Make 3.81.
I code a very simple native c function as well as Android.mk shown as follows:
-----------../jni/get_string_native.c---------
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include <stdio.h>
jstring Java_geil_learn_ndk_NDKStringActivity_getString(JNIEnv * env, jobject this, jint value1, jint value2)
{
char *szFormat = "The sum of the two numbers is: %i";
char *szResult;
// add the two values
jlong sum = value1+value2;
// malloc room for the resulting string
szResult = malloc(sizeof(szFormat) + 20);
// standard sprintf
sprintf(szResult, szFormat, sum);
// get an object string
jstring result = (*env)->NewStringUTF(env, szResult);
// cleanup
free(szResult);
return result;
}
--------------../jni/Android.make----------
# LOCAL_PATH := $(call my-dir)
#
# include $(CLEAR_VARS)
#
# LOCAL_LDLIBS := -llog
#
# LOCAL_MODULE := get_string_native
# LOCAL_SRC_FILES := get_string_native.c
#
# include $(BUILD_SHARED_LIBRARY)
Now I tried to use ndk-build to produce the shared library, but nothing happen. Even no error message showed up. See below:
------- From Cygwin terminal ---------
Administrator@XP-201011081632 /cygdrive/d/eclipse_projects/workspace/NDKString/jni
$ ndk-build
Administrator@XP-201011081632 /cygdrive/d/eclipse_projects/workspace/NDKString/jni
$
Surely, I have set up the environment variable PATH and build it in \jni.
I am really confused now; please help me!!
I have removed # in the Android.make file. Now it can generate the corresponding .so file in /lib. But when I call the native method in an activity, run-time error occurred. Logcat info is as follows. Would u please help me?
Trying to load lib /data/data/geil.learn.ndk.NDKStringAcivity/lib/libnative.so 0x405151f0
Added shared lib /data/data/geil.learn.ndk.NDKStringAcivity/lib/libnative.so 0x405151f0
No JNI_OnLoad found in /data/data/geil.learn.ndk.NDKStringAcivity/lib/libnative.so 0x405151f0, skipping init
60): Displayed geil.learn.ndk.NDKStringAcivity/.NDKStringActivity: +1s145ms
No implementation found for native Lgeil/learn/ndk/NDKStringAcivity/NDKStringActivity;.getString (II)Ljava/lang/String;
344): Shutting down VM threadid=1: thread exiting with uncaught exception (group=0x40015560)
344): FATAL EXCEPTION: main
344): java.lang.UnsatisfiedLinkError: getString
344): at geil.learn.ndk.NDKStringAcivity.NDKStringActivity.getString(Native Method)
344): at geil.learn.ndk.NDKStringAcivity.NDKStringActivity.onClick(NDKStringActivity.java:40)
344): at android.view.View.performClick(View.java:2485)
344): at android.view.View$PerformClick.run(View.java:9080)
344): at android.os.Handler.handleCallback(Handler.java:587)
344): at android.os.Handler.dispatchMessage(Handler.java:92)
344): at android.os.Looper.loop(Looper.java:123)
344): at android.app.ActivityThread.main(ActivityThread.java:3683)
344): at java.lang.reflect.Method.invokeNative(Native Method)
344): at java.lang.reflect.Method.invoke(Method.java:507)
344): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
344): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
344): at dalvik.system.NativeStart.main(Native Method)
60): Force finishing activity geil.learn.ndk.NDKStringAcivity/.NDKStringActivity
60): Activity pause timeout for HistoryRecord{407868c8) geil.learn.ndk.NDKStringAcivity/.NDKStringActivity} Sending signal. PID: 344 SIG: 9
60): Process geil.lear开发者_JS百科n.ndk.NDKStringAcivity (pid 344) has died.
60): WIN DEATH: Window{4073a7c0 geil.learn.ndk.NDKStringAcivity/geil.learn.ndk.NDKStringAcivity.NDKStringActivity paused=false}
(60): Got RemoteException sending setActive(false) notification to pid 344 uid 10037
60): Activity destroy timeout for HistoryRecord{407868c8 geil.learn.ndk.NDKStringAcivity/.NDKStringActivity}
As Salw has already pointed out you will need to change the extension.
A #
in the .mk file is a comment, you basically your make file is an empty makefile. Remove the #
from the .mk file.
Also you will need to give the include directory which contains the header files..
精彩评论