30 likes | 283 Views
activity_main.xml. Mainactivity.java. < RelativeLayout xmlns:android = "http://schemas.android.com/ apk /res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = " match_parent " android:layout_height = " match_parent "
E N D
activity_main.xml Mainactivity.java <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/callbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="31dp" android:text="Button" /> </RelativeLayout> package com.example.test_jni_1; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.util.Log; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button bv; private TextViewtv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bv= (Button)findViewById(R.id.callbtn); tv= (TextView)findViewById(R.id.textView1); bv.setOnClickListener(new OnClickListener() { public void onClick(View v) { NativeCallnativeCall = new NativeCall(); int ret = nativeCall.add(10, 20); String retStr = nativeCall.stringFromJNI(); Log.i("TAG", retStr + ret); tv.setText(retStr); } }); } @Override public booleanonCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
NativeCall.java package com.example.test_jni_1; public class NativeCall { static { System.loadLibrary("my_lib"); } public native String stringFromJNI(); public native int add(int a, int b); } My_lib.c #include "com_example_test_jni_1_NativeCall.h" JNIEXPORT jstring JNICALL Java_com_example_test_1jni_11_NativeCall_stringFromJNI(JNIEnv *env, jobjectobj) { return (*env)->NewStringUTF(env, "Hello JNI!"); } JNIEXPORT jint JNICALL Java_com_example_test_1jni_11_NativeCall_add(JNIEnv *env, jobjectobj, jint a, jint b) { return a + b; } mkdir ../../jnimv com_example_test_jni_1_NativeCall.h ../../jni ⑥ Android.mk 파일 작성위치 : [HelloAndroid]-[jni]-[Android.mk]다운받은 Android NDK 폴더의 /sample/hello-jni/jni의 Android.mk 파일을 현재 프로젝트의 /jni폴더에 복사한 후, 다음과 같이 수정하여 사용합니다. 수정 된 내용은 19, 20 라인의 LOCAL_MODULE 과 LOCAL_SRC_FILES 입니다. LOCAL_MODULE := my_lib LOCAL_SRC_FILES := my_lib.c