我正在使用Eclipse来试验Android NDK编程。我的平台信息如下:
我编写了一个非常简单的原生c函数以及Android.mk,如下所示:
----------- .. / 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)
现在我尝试使用ndk-build来生成共享库,但没有任何反应。甚至没有出现错误消息。见下文:
-------来自Cygwin终端---------
Administrator@XP-201011081632 /cygdrive/d/eclipse_projects/workspace/NDKString/jni
$ ndk-build
Administrator@XP-201011081632 /cygdrive/d/eclipse_projects/workspace/NDKString/jni
$
当然,我已经设置了环境变量PATH并在\ jni中构建它。
我现在很困惑;请帮我!!
我在Android.make文件中删除了#。现在它可以在/ lib中生成相应的.so文件。但是当我在活动中调用本机方法时,发生了运行时错误。 Logcat信息如下。你能帮帮我吗?
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.learn.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}
答案 0 :(得分:1)
Salw已经指出你需要改变扩展名。
.mk文件中的#
是注释,基本上你的make文件是一个空的makefile。从.mk文件中删除#
。
此外,您还需要提供包含头文件的include目录。