2013-01-21 59 views
1

我想在eclipse juno中創建一個基於JNI的Android項目。如何在Eclipse中創建JNI android項目

如何在Android中使用Java和C++創建簡單的「Hello World」項目。有沒有任何教程可以幫助我通過使用JNI上述應用程序。

通過運行應用程序它顯示了以下錯誤

enter image description here

回答

5

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!"); 

} 

Android.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構建庫(如果您正在窗口中開發),與提及的i相同在這個例子中。並運行它。

+0

親愛的,它不與朱諾合作,bcoz它是非常古老的教程 – user1703737

+0

親愛的,你得到什麼錯誤?我試過它在juno和它正常工作。請確保您使用的是最新的NDK和Android SDK – Sunny

+0

我添加了錯誤的屏幕快照請求檢查它。 – user1703737