debian linux android ndk compile env setup
1. install debian linux 6.0.6
2. edit source list file
3. setup gcc g++ compile env
apt-get install gcc g++ gdb automake make autoconf etc
4. download android ndk package
android-ndk-r8b-linux-x86.tar
5. unzip ndk and install in /opt
tar xvf android-ndk-r8b-linux-x86.tar
cp -r android-ndk-r8b /opt
6. set compile path
edit /etc/profile
ANDROID_NDK_ROOT=/opt/android/android-ndk-r8b
export PATH=$ANDROID_NDK_ROOT:$PATH
for example:
1. copy ndk sample hello-jni into my home
cp -r /opt/android/android-ndk-r8b/samples/hello-jni /home/mytest
Android.mk content is as followings.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
hello-jni.c file content is as followings. (modified)
#include <string.h>
#include <jni.h>
jstring
Java_hello1_world_TestHello1Activity_stringFromJNI( JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
2. run ndk-build in test/hello-jni directory
3. copy libs/armeabi/libhello-jni.so into local windows
4. copy libhello-jni.so into project libs\armeabi
5. open android simulator
6. compile hello1.world.TestHello1Activity project by eclipse
run hello1.world.TestHello1Activity project as android application
7. procedure is over
8. source code for java jni call
package hello1.world;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class TestHello1Activity extends Activity {
private Button btn_save;
private EditText edit_name;
private EditText edit_telno;
private EditText edit_text;
public native String stringFromJNI();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
setListensers();
InitEdit();
}
private void findViews() {
btn_save = (Button) findViewById(R.id.cm_save);
edit_name = (EditText) findViewById(R.id.cm_name);
edit_telno = (EditText) findViewById(R.id.cm_telno);
edit_text = (EditText) findViewById(R.id.cm_text);
}
private void setListensers() {
btn_save.setOnClickListener(save);
}
private OnClickListener save = new OnClickListener() {
public void onClick(View v) {
String name = edit_name.getText().toString();
String telno = edit_telno.getText().toString();
edit_text.setText(stringFromJNI() + edit_text.getText() + " " + name + " " + telno);
}
};
private void InitEdit() {
edit_name.setText(null);
edit_telno.setText(null);
}
static {
System.loadLibrary("hello-jni");
}
}