user1703737
user1703737

Reputation: 533

How to create JNI android project in Eclipse

I want to create a JNI based Android project in eclipse juno.

How can I create a simple "Hello World" Project in android by using Java and C++. Is there any tutorial that could help me to the above mentioned app by using JNI.

By running the app it shows the following errors

enter image description here

Upvotes: 1

Views: 8574

Answers (1)

Sunny
Sunny

Reputation: 14808

http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/

This is a great tutorial to start with NDK.

Ok Here is the code 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)

Put Android.mk and NDK.cpp in jni folder Now build the library using cygwin (if you are developing on window), same as mention in the example. and run it.

Upvotes: 5

Related Questions