【问题标题】:provide help for using Android NDK提供使用 Android NDK 的帮助
【发布时间】:2011-10-01 05:56:33
【问题描述】:

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

【问题讨论】:

标签: android android-ndk-r5


【解决方案1】:
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

【讨论】:

    【解决方案2】:

    这里有一个相当不错的 NDK 初学者教程:Getting Started with the NDK

    【讨论】:

      【解决方案3】:

      记住事情的艰难方法。在您从developers.android.com 下载的Andaroid NDK 中,您将拥有docs 文件夹,其中您将拥有不同的文档,这些文档将清楚地告诉您应该如何以及何时使用NDK。特别是通过 OVERVIEW.HTML 完全。 http://developer.android.com/sdk/ndk/index.html

      我第一次使用 NDK 时就是这样做的。我强烈推荐这种方式..

      如果您对什么是本机代码感到困惑 -> 它是您想要在 Android 应用程序中使用的 C 或 C++ 或汇编代码。这被称为本机代码,因为它不是 Java 语言。

      一切顺利。

      【讨论】:

      • 我是 NDK 的新手,并且已经阅读了 OVERVIEW.HTML 几遍,并且那里有更好的教程。阅读不会有什么坏处,但是有很多细节被遗漏了,主要是设置它(这是他想做的)。
      【解决方案4】:

      您在这里有一个很棒的教程,解释了使用 NDK 所需的所有内容,如何创建函数以及如何使用它们,并且很容易理解。 http://marakana.com/s/introduction_to_ndk,1153/index.html,对不起我的英语,不是我的母语。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-18
        相关资源
        最近更新 更多