NDK的Android应用程序停止工作 [英] Android ndk app stops working
问题描述
我一直试图让添加原生code到我的Android应用程序,所以我已经通过的Android NDK过多看开始教程都无济于事。我不能让任何应用程序在我的仿真器或我的银河S3运行。我不断收到同样的错误:不幸的是,[AppName的]已停止工作
我的NDK-build命令成功地工作,我得到一个.so文件。我不知道什么是.so文件应该是什么样子。这里是我的
它看起来像废话给我,但我知道什么?
这是我的logcat
http://txt.do/klod
它说本地找不到方法
我有以下的code作为我的主要(唯一的)活动
进口android.app.Activity;
进口android.os.Bundle;公共类NDKSetupActivity延伸活动{静态的 {
的System.loadLibrary(ndksetup);
}@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity);
PRINTLOG(你好!);
}私人本地无效PRINTLOG(字符串logThis);
}
这是我的C code(称为native.c)
的#include< jni.h>
#包括LT&;&string.h中GT;
#包括LT&;机器人/ log.h>#定义DEBUG_TAGNDKSetupActivity无效com_example_ndksetup_NDKSetupActivity_printLog(JNIEnv的* ENV,jobject这个,的jstring logString)
{
jboolean isCopy;
为const char * szLogString =(* ENV) - GT; GetStringUTFChars(ENV,logString,&安培; isCopy);__android_log_print(ANDROID_LOG_DEBUG,DEBUG_TAG,NDK:%S,szLogString);(* ENV) - GT; ReleaseStringUTFChars(ENV,logString,szLogString);
}
这是我的Android.mk文件
LOCAL_PATH:= $(叫我-DIR)包括$(CLEAR_VARS)LOCAL_LDLIBS:= -llogLOCAL_MODULE:= ndksetup
LOCAL_SRC_FILES:= native.c包括$(BUILD_SHARED_LIBRARY)
这是我的清单
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.ndksetup
安卓版code =1
机器人:=的versionName1.0><用途-SDK
安卓的minSdkVersion =19
机器人:targetSdkVersion =19/><应用
机器人:allowBackup =真
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名字=。NDKSetupActivity
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途>< /清单>
您缺少的 的Java _
preFIX。尝试重命名在 native.c
函数名称 Java_com_example_ndksetup_NDKSetupActivity_printLog
。
希望这有助于。
I've been trying to get started on adding native code to my android app so I've looked through a plethora of Android NDK tutorials all to no avail. I can't get any of the apps to run on my emulator or my Galaxy S3. I keep getting the same error: "Unfortunately, [AppName] has stopped working" My ndk-build command works successfully and I get a .so file. I don't know what the .so file is supposed to look like. Here's mine
it looks like gibberish to me but what do I know?
This is my logcat
http://txt.do/klod
It says Native Method Not Found
I have the following code as my main (and only) activity
import android.app.Activity;
import android.os.Bundle;
public class NDKSetupActivity extends Activity {
static {
System.loadLibrary("ndksetup");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
printLog("Hello!!");
}
private native void printLog(String logThis);
}
This is my C code (called native.c)
#include <jni.h>
#include <string.h>
#include <android/log.h>
#define DEBUG_TAG "NDKSetupActivity"
void com_example_ndksetup_NDKSetupActivity_printLog(JNIEnv *env, jobject this, jstring logString)
{
jboolean isCopy;
const char * szLogString = (*env)->GetStringUTFChars(env, logString, &isCopy);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK: %s", szLogString);
(*env)->ReleaseStringUTFChars(env, logString, szLogString);
}
This is my Android.mk file
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE := ndksetup
LOCAL_SRC_FILES := native.c
include $(BUILD_SHARED_LIBRARY)
And this is my Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ndksetup"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".NDKSetupActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You are missing Java_
prefix. Try renaming the function name in native.c
to Java_com_example_ndksetup_NDKSetupActivity_printLog
.
Hope this helps.
这篇关于NDK的Android应用程序停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!