The first of Android advanced calls C library functions in Java
This article lists the ways in which the NDK does not come out
After Android releases the NDK, you can use this
First, download ANDROID-NDKAfter downloading the compressed package extracted, for example: D:\android-ndk-r10e, the directory of Ndk-build.cmd is used to compile the batch command.
For easy compilation, it is recommended to add environment variables
Add D:\android-ndk-r10e to the path directory;
Take NDK's own example \samples\hello-jni as an example
To import an example into an Android project:
Hellojni.java as follows:
/** Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' As is ' BASIS, * without warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ PackageCom.example.hellojni;Importandroid.app.Activity;ImportAndroid.widget.TextView;ImportAndroid.os.Bundle; Public classHellojniextendsactivity{/**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); /*Create a TextView and set its content. * The text is retrieved by calling a native * function. */TextView TV=NewTextView ( This); Tv.settext (Stringfromjni ()); Setcontentview (TV); } /*A native method that's implemented by the * ' hello-jni ' native library, which are packaged * with this applic ation. */ Public nativeString Stringfromjni (); /*This is another native method declaration, which is *not* * implemented by ' Hello-jni '. This was simply to show that * you can declare as many native methods in your Java code * as you want, their implem entation is searched in the * currently loaded native libraries only the first time * you are call them. * * Trying to call this function would result in a * Java.lang.UnsatisfiedLinkError exception! */ Public nativeString Unimplementedstringfromjni (); /*This was used to load the ' Hello-jni ' Library on application * startup. The library has already been unpacked to */data/data/com.example.hellojni/lib/libhello-jni.so at * installation Time by the package manager. */ Static{system.loadlibrary ("Hello-jni"); }}
Observing the code above will reveal:
There is a process for importing system.loadlibrary
Two Nativie functions have also been defined
In view of the C source file
/** Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' As is ' BASIS, * without warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */#include<string.h>#include<jni.h>/*This was a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * APPS/SAMPLES/HELLO-JNI/PROJECT/SRC/COM/EXAMPLE/HELLOJNI/HELLOJN I.java*/Jstringjava_com_example_hellojni_hellojni_stringfromjni (jnienv*env, Jobject thiz) {#ifDefined (__arm__)#ifDefined (__arm_arch_7a__)#ifDefined (__arm_neon__)#ifDefined (__ARM_PCS_VFP)#defineABI "Armeabi-v7a/neon (hard-float)"#else #defineABI "Armeabi-v7a/neon"#endif #else #ifDefined (__ARM_PCS_VFP)#defineABI "armeabi-v7a (hard-float)"#else #defineABI "armeabi-v7a"#endif #endif #else #defineABI "Armeabi"#endif#elifDefined (__i386__)#defineABI "x86"#elifDefined (__x86_64__)#defineABI "x86_64"#elifDefined (__MIPS64)/* mips64el-* toolchain defines __mips__ too */#defineABI "Mips64"#elifDefined (__mips__)#defineABI "MIPS"#elifDefined (__aarch64__)#defineABI "ARM64-V8A"#else #defineABI "Unknown"#endif return(*env)->newstringutf (env,"Hello from JNI! Compiled with ABI"Abi".");}
Because we are taking this C source file to use, if the JAVA_COM_EXAMPLE_HELLOJNI_HELLOJNI_STRINGFROMJNI function name, in our Android project, the Java class declaration will be: com/ Example/hellojni/hellojni.java.
Second, compile, open the CMD command-line window
D:\>CD D:\android-ndk-r10e\samples\hello-jni\jnid:\android-ndk-r10e\samples\hello-jni\jni>ndk-build[arm64-V8A] Gdbserver: [aarch64-linux-android-4.9] libs/arm64-v8a/gdbserver[arm64-V8A] gdbsetup:libs/arm64-v8a/gdb.setup[x86_64] Gdbserver: [x86_64-4.9] libs/x86_64/gdbserver[x86_64] Gdbsetup:libs/x86_64/Gdb.setup[mips64] Gdbserver: [Mips64el-linux-android-4.9] libs/mips64/Gdbserver[mips64] Gdbsetup:libs/mips64/Gdb.setup[armeabi-V7A] Gdbserver: [arm-linux-androideabi-4.8] libs/armeabi-v7a/Gdbserver[armeabi-V7A] gdbsetup:libs/armeabi-v7a/Gdb.setup[armeabi] Gdbserver: [Arm-linux-androideabi-4.8] libs/armeabi/Gdbserver[armeabi] Gdbsetup:libs/armeabi/gdb.setup[x86] Gdbserver: [x86-4.8] libs/x86/Gdbserver[x86] Gdbsetup:libs/x86/gdb.setup
The compilation succeeds in generating the libhello-jni.so file in the D:\~\samples\hello-jni\libs\armeabi directory.
Not only Armeabi all other platforms will generate. so files
Now the program can run.
Stringfromjni () is actually calling the method in the. so file
Program Run Result:
Seventh of the "Android Advanced" use of the NDK