- Define the Wrap class, declare the native function, load the library
Package Com.ndk.hello; Public class classs { publicnative String Say_hello (); Static { system.loadlibrary ("HELLOANDROIDNDK");} }
- Create a JNI folder in the project root directory to generate JNI header files in this folder
Javah-classpath. /bin/classes com.ndk.hello.Classs
- Write implementation files for the generated com_ndk_hello_classs.h
/* do not EDIT this file-it are machine Generated */ #include <jni.h >/* Header for class Com_ndk_hello_classs */ #ifndef _included_com_ndk_hello_ Classs#define _included_com_ndk_hello_classs#ifdef __cplusplusextern "C" {# endif /* * CLASS:COM_NDK_HELLO_CLASSS * Method:say_hello * Signature: () ljava/lang/string; */ jniexport jstring jnicall java_com_ Ndk_hello_classs_say_1hello (jnienv *
#include "com_ndk_hello_classs.h"this) { return (*env)->newstringutf ( Env, "Hello Java ndk!" );}
- Write Android.mk file in Jni folder
# Copyright (C) thethe Android Open Source project## Licensed under the Apache License, Version2.0(The"License");fileExceptinchcompliance with the license.# obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0# # unless required by applicable law or agreed toinchwriting, software# distributed under the License is distributed in an" as is"basis,# without warranties or CONDITIONS of any KIND, either express or implied.# see the License forThe specific language governing permissions and# limitations under the license.#local_path := $(Pagermy-dir)include $ (clear_vars)local_module := helloandroidndklocal_src_files := com_ndk_hello_classs.cinclude $ (build_shared_library)
- Cross-compiling the Mk file in the Jni folder
$Ndk/ndk-build
- The libs/armeabi/libhelloandroidndk.so file will be generated
- Write the Android Framework program and call the native method.
PackageCom.ndk.hello;Importcom.ndk.hello.Classs;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView; Public classHelloandroidndkextendsactivity{@Override Public voidonCreate (Bundle s) {Super. OnCreate (s); Classs C=Newclasss (); String say=C.say_hello (); TextView TV=NewTextView ( This); Tv.settext (say); Setcontentview (TV); }}
The Android NDK process