2011-07-08 40 views
1

我想在我的項目中使用android NDK。所以我的問題是,如何整合並執行Android NDK的hello-jni的默認示例項目?請給我提供一步一步的解決方案...我對此一無所知......爲使用Android NDK提供幫助

+0

我已經在這裏編寫了一步一步的教程。 http://www.permadi.com/blog/2011/09/setting-up-android-jni-projects-in-windows-eclipse-and-sequoyah/希望它有幫助。 – 2011-09-14 07:02:53

+0

http://stackoverflow.com/questions/1541884/android-jni-guidelines/10316693#10316693 這個鏈接顯示了一步一步的過程來創建/構建和使用android ndk。 – Raulp

回答

5
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 
1

瞭解它是記住事情的艱難方法。您從developers.android.com下載的Andaroid NDK內部,您將擁有docs文件夾,您將在其中擁有不同的文檔,這些文檔將清楚地告訴您應該如何以及何時使用NDK。尤其是完全通過 OVERVIEW.HTML。 http://developer.android.com/sdk/ndk/index.html

我這樣做,當我第一次使用NDK。我強烈建議只有這樣..

如果你感到困惑什麼是本地代碼 - >它是你想用於Android應用程序的C或C++或彙編代碼。這被稱爲本地代碼,因爲它不是用java語言編寫的。

所有最優秀的。

+0

我是NDK的新手,已經多次閱讀了OVERVIEW.HTML,並且有更好的教程。閱讀並不會造成傷害,但遺漏了很多細節,主要是建立它(這是他想要做的)。 – JuiCe