简体   繁体   English

如何在Android项目中使用NDK?

[英]How to use NDK in android project?

I need to use some native c/c++ in my project, so I need to use the NDK. 我需要在项目中使用某些本机c / c ++,因此需要使用NDK。 Is there an easy way to set it up in eclipse? 有没有一种简单的方法可以在Eclipse中进行设置?

Thanks. 谢谢。

There are following steps 有以下步骤

1 : Create a jni folder in your project directory 1:在项目目录中创建一个jni文件夹

2 : Create a file name Android.mk in your newly created folder jni and create a new file of C or C++, lets we consider hear we use C file and name of file is MyNativeC.c 2:在新创建的文件夹jni中创建一个文件名Android.mk并创建一个新的C或C ++文件,让我们考虑听到我们使用了C文件,文件名为MyNativeC.c

3: now type following code in Android.mk file 3:现在在Android.mk文件中键入以下代码

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_LDLIBS := -llog

LOCAL_MODULE    := local_module_ndk // this is the name of local module by which you can call the Local source file
LOCAL_SRC_FILES := MyNativeC.c// hear we can pass the name of your native file name hear we are use MyNativeC.c file so we pass the name of MyNativeC.c in local source file name

include $(BUILD_SHARED_LIBRA

4 now open MyNativeC.c file and create two method which you want to call from your android code(from your Activity) hear we create following code 4现在打开MyNativeC.c文件并创建两个方法,您要从您的android代码中调用(从您的Activity中),听到我们创建以下代码

    #include <jni.h>
#include <string.h>
#include <stdio.h>
#include <android/log.h>

#define DEBUG_TAG "MY_NDK_DEMO"


jstring Java_com_myNDKDemo_MainActivity_getString(JNIEnv * env, jobject this, jint value1, jint value2)
{
    char *szFormat = "Addition : %i";
    char *szResult;


    jlong sum = value1+value2;


    szResult = malloc(sizeof(szFormat) + 20);


    sprintf(szResult, szFormat, sum);

    jstring result = (*env)->NewStringUTF(env, szResult);


    free(szResult);

    return result;
}

5 now edit your activity where you want to call the native code, 5现在在您要调用本机代码的位置编辑活动,

first create a static block where we have to load the library of native code. 首先创建一个静态块,在这里我们必须加载本机代码库。

hear we show the code of my activity name is MainActivity.java 听到我们显示我的活动名称为MainActivity.java的代码

package com.myNDKDemo

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {
    private static final String DEBUG_TAG = "MainActivity";


    private native String getStringAdd(int fist, int second);

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

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        button b = new Button(this);

        b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


                            Toast.makeText(getApplicationContext(),""+getStringAdd(100,200), 2000).show();

        }
    });

           setContentView(b);

    }
}

6 Now first compile the c code , for compile c code first you have to NDK development kit , 6现在首先编译c代码 ,要首先编译c代码 ,您必须使用NDK开发套件

now open run. 现在开始运行。 and type cmd 然后输入cmd

now go to project path 现在转到项目路径

就我而言,我是在Destop中创建应用程序的,所以请听听我们给出我应用程序的路径

after that typw the path of the my NDK's ndk-build file path 打字后,我的NDK的ndk-build文件路径

在此处输入图片说明

now we press enter the automatic it create libs directory in your project 现在我们在您的项目中按进入自动它创建库目录 在此处输入图片说明

7 If you see in your project, there are libs and obj created automatically. 7如果在项目中看到,则会自动创建库和obj

8.Refresh (right Click) the JNI folder (refresh it every time you build using teh ndk-build,this actually loads the newly built shared library in the libs folder.) 8.刷新(右键单击)JNI文件夹(每次使用ndk-build进行刷新时,实际上会将新建的共享库加载到libs文件夹中。)

9.Now run your android project, when press the button the it will show you the addition 9.现在运行您的android项目,当按下按钮时,它将显示您的添加

thanks 谢谢

Ok, after I spent some time experimenting, I can say that the best way for a beginner to start using NDK in eclipse is first to go over this tutorial: http://marakana.com/forums/android/examples/49.html and simply create the necessary files for the jni folder (but don't do anything else). 好的,在花了一些时间进行实验之后,我可以说,对于初学者来说,在eclipse中开始使用NDK的最佳方法是首先阅读本教程: http : //marakana.com/forums/android/examples/49.html并只需为jni文件夹创建必要的文件(但无需执行其他任何操作)。 Then you should read this http://mobilepearls.com/labs/ndk-builder-in-eclipse/ and follow the steps. 然后,您应该阅读此http://mobilepearls.com/labs/ndk-builder-in-eclipse/并按照步骤操作。 Then you are ready. 那你准备好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM