Android实现对c++方式调用
There are example about how Android platform call c code via NDK , which are in android-ndk-r8(version) dirctory :
/home/dengwei/android-NDK/samples
However , there is NOT a example about how to call c++ code via NDK. This article is about how android call c++ code.
The following is a modify version of hello-jni (/home/dengwei/android-NDK/samples/hello-jni), I copy it into hello-jni-cpp
LOCAL_PATH := $(call my-dir)
LOCAL_CPP_EXTENSION := .cpp
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.cpp
include $(BUILD_SHARED_LIBRARY)
////////////////$mv hello-jni.c hello.jni.cpp///////////////////////////////
////////////////Next , hello-jni.c:////////////////////////////////////////
#include <string.h>
#include <jni.h>
class myMath
{
public:
static int myAdd(int x,int y){return x+y;}
};
extern "C"
{
JNIEXPORT jint JNICALL Java_com_example_hellojni_HelloJni_myAdd
( JNIEnv* env,jobject thiz )
{
return myMath::myAdd(10,20);
}
}
/////////////////Last , HelloJni.java:///////////////////////////////////////
package com.example.hellojni;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class HelloJni extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
System.loadLibrary("hello-jni");
TextView tv = new TextView(this);
int z=myAdd();
tv.setText(Integer.toString(z));
setContentView(tv);
}
static {
System.loadLibrary("hello-jni");
}
native static int myAdd();
}
sometimes you might come to an error : could NOT find somefile.o ,what you need to do is : $ ndk-build clean
EOF