gpt4 book ai didi

android - 如何在 Eclipse 中创建 JNI android 项目

转载 作者:行者123 更新时间:2023-11-29 15:24:34 25 4
gpt4 key购买 nike

我想在 eclipse juno 中创建一个基于 JNI 的 Android 项目。

如何使用 Java 和 C++ 在 android 中创建一个简单的“Hello World”项目。是否有任何教程可以通过使用 JNI 帮助我完成上述应用程序。

通过运行应用程序,它会显示以下错误

enter image description here

最佳答案

http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/

这是一个很好的 NDK 入门教程。

好的这是代码 Activity --

package com.example.ndk;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;

public class MainActivity extends Activity {

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

// declare the native code function - must match ndkfoo.c
private native String invokeNativeFunction();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// this is where we call the native code
String hello = invokeNativeFunction();

new AlertDialog.Builder(this).setMessage(hello).show();
}

}

NDK.cpp

#include <string.h>
#include <jni.h>


jstring Java_com_example_ndk_MainActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {

return (*env)->NewStringUTF(env, "Hello from native code!");

}

安卓.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

# Here we give our module name and source file(s)
LOCAL_MODULE := NDK
LOCAL_SRC_FILES := NDK.c

include $(BUILD_SHARED_LIBRARY)

将Android.mk和NDK.cpp放入jni文件夹现在使用 cygwin 构建库(如果您在窗口上开发),与示例中提到的相同。并运行它。

关于android - 如何在 Eclipse 中创建 JNI android 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14435032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com