android - 提供使用 Android NDK 的帮助

标签 android android-ndk-r5

我想在我的项目中使用 android NDK。所以我的问题是,如何从 android NDK 集成和执行 hello-jni 的默认示例项目?请给我一步一步的解决方案......我对此一无所知......

最佳答案

1.    First Create Android Project 
2.    Then Create Folder  in the Project with name jni (Dont use other name)

3.       Create File With the Android.mk ( Rightclick on the jni folder ->New -> File -                                                                                 >Android.mk)
// Coding To Build Path and Access
LOCAL_PATH :=$(call my-dir)         // This is Common
include $(CLEAR_VARS)               // This is common`enter code here`
LOCAL_MODULE    := myCFile  // myCFile is ur own  Name of the module
                that will be    called in Activity)
LOCAL_SRC_FILES := my.c   // Our C File What should be given in C
                                                                             file creation

include $(BUILD_SHARED_LIBRARY)


4.    Create C File like above With ur own name with the extension .c (This file will be
                                                           used  in LOCAL_SRC_FILES )
                    C File Will Look Like this:
Note :
// Your Package name com.JniMyDemo Then Your Activity File and then the Function name for c file
So that I used jint(return value) 
(Java) its must
(com_JniDemo) is  Specified in Package name(com.JniDemo) we couldnt use . for package so 
we put underscore for c file )
(JniMyDemoActivity) Is my class

First two arguement is Important one others are our own variable for function //

Include two header file string.h,jni.h


#include "string.h"
#include "jni.h"

jint Java_com_JniMyDemo_JniMyDemoActivity_add(JNIEnv *env,jobject jobj,jint n1,jint n2)
{
jint a,b,c;
a=n1;
b=n2;
return (a+b);
}

5.  Then Call the The C File Like this 

// Activity Look like this 

public class JniMyDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView txt    =new TextView(this);
        txt.setText(" "+add(100,100));
        setContentView(txt);
    }

    private native int add(int i,int j,int k);
    static
    {
        System.loadLibrary("myCFile");
    }
}  

6.      Then Compile the C File by 
    Starting Terminal (cmd prompt) Type the Project Path like ( cd  Project Path) and Enter
    Then type the          ( Ndk Location/ndk-build) it will automatically compile & ur jni             folder
7.   If it success means Start the Actvity  it will show the Result of Addition

关于android - 提供使用 Android NDK 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6624350/

相关文章:

android - 从 native 代码访问 android 外部 SD 卡

android - 使用 pocketsphinx_wrap.c 为 Android 构建 PocketSphinx 演示项目时出错

android - 如何在客户端使用 URL 从服务器下载多个图像

java - 尝试压缩位图时出现 OutOfMemoryError

android - 用指纹保护值(value)

android - 在摩托罗拉手机上运行 ndk-gdb 时找不到包错误

android - 尝试使用 Cygwin 在 Windows 上使用 NDK 构建适用于 Android 的 PocketSphinx 时出现问题

android - 在 NDK 版本 (android-ndk-r8-windows.) 上编译 protobuf-2.4.1 代码

android - 订阅 API 上的 "customer' 卡被拒绝的令人难以置信的高比率

android - React Native 模块与 Cordova 插件有何不同?