Command to package the so file into the apk
Use the original code of the android SDK to develop the APK and package the so file into the apk. This can only provide the user with an apk.
1. Meaning full-code so packaging.
A) create a project and use local calls. The java file is as follows:
Package test. jni;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class jnitest extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
TextView TV = new TextView (this );
Int x = 55;
Int y = 8;
System. loadLibrary ("apptest ");
// System. loadLibrary ("testapi ");
Int z = appadd (x, y );
TV. setText (x + "+" + y + "=" + z );
SetContentView (TV );
}
Public native int appadd (int x, int y );
}
B) Create Android. mk in the root directory of the project. The content is as follows.
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_MODULE_TAGS: = user eng
LOCAL_SRC_FILES: = $ (call all-subdir-java-files)
LOCAL_PACKAGE_NAME: = jnitest
LOCAL_JNI_SHARED_LIBRARIES: = libapptest
Include $ (BUILD_PACKAGE)
Include $ (LOCAL_PATH)/jni/Android. mk
# Use the folloing include to make our test apk.
Include $ (call all-makefiles-under, $ (LOCAL_PATH ))
The name of LOCAL_MODULE_TAGS must be the same as that of the target system. Otherwise, the generated apk file cannot be installed on the target system ,.
LOCAL_JNI_SHARED_LIBRARIES: = libapptest: place the so file we compiled into the libs/armeabi file in the apk file.
Include $ (LOCAL_PATH)/jni/Android. mk to compile a local file to become so
C) Create the jni folder in the root directory of the project and create Android. mk and callapp. c In the jni file. Their contents are as follows:
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_MODULE_TAGS: = user eng
LOCAL_MODULE: = libapptest
LOCAL_SRC_FILES: = callapp. c
LOCAL_C_INCLUDES + = \
$ (JNI_H_INCLUDE)
LOCAL_PRELINK_MODULE: = false
Include $ (BUILD_SHARED_LIBRARY)
LOCAL_MODULE_TAGS: it must be consistent with the project; otherwise, the compilation fails.
LOCAL_C_INCLUDES + = \
$ (JNI_H_INCLUDE) in order to make jni compilation pass, because the project calls the local library through jni
# Include <jni. h>
JNIEXPORT jint JNICALL Java _ test_jni_jnitest_appadd
(JNIEnv * env, jobject obj, jint x, jint y ){
Return x + y;
}
D) Place the project file in the SDK platform \ packages \ apps directory, and enter the directory. Enter mm in the command to compile the project. (set the project objectives before performing platform)
E) The compiled apk can be found in platform/out/target/product/sk886x/system/app. sk886x is the target platform. open the apk file and you can see libapptest. so exists in the libs/armeabi folder in the package. run the apk to run properly and display the correct answer.
2. Add messages to the so file of the local library.
A) Modify Android. mk in the jni folder to add
LOCAL_SHARED_LIBRARIES: = libutils
B) Callapp. c:
# Include <jni. h>
# Define LOG_TAG "TestThunderLib"
# Undef LOG
# Include <utils/Log. h>
JNIEXPORT jint JNICALL Java_test_jni_jnitest_appadd
(JNIEnv * env, jobject obj, jint x, jint y ){
LOGD ("TEST % d + % d = % d", x, y, x + y );
Return x + y; // add22 (x, y );
}
C) re-enter mm in the root directory of the project, compile the apk, run it on the target machine, and run the uart command logcat. The log output information such as TEST 55 + 66 = 121 is displayed.
3. On the basis of the above, I introduced another library file to call callapp. c.
A) This library is compiled using NDK. Make sure it is consistent with the target platform version. The file is as follows:
I. Add. c
# Include "add. h"
Int add22 (int x, int y ){
Return x + y;
}
Ii. Add. h
# Ifndef ADD_H
# Define ADD_H
Extern int add22 (int x, int y );
# Endif/* ADD_H */
Android. mk of iii. NDK
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_MODULE: = testapi
LOCAL_SRC_FILES: = add. c
LOCAL_PRELINK_MODULE: = false
Include $ (BUILD_STATIC_LIBRARY)
# Include $ (BUILD_SHARED_LIBRARY)
B) choose different built methods. After NDK compilation, you can get libtestapi. a, libtestapi. so
C) Add Android. mk in the jni folder under the project directory.
LOCAL_STATIC_LIBRARIES: = libtestapi
It indicates that libtestapi is compiled into libapptest. so as a static library.
D) The content of Callapp. c is as follows:
# Include <jni. h>
# Include "add. h"
# Define LOG_TAG "TestThunderLib"
# Undef LOG
# Include <utils/Log. h>
JNIEXPORT jint JNICALL Java_test_jni_jnitest_appadd
(JNIEnv * env, jobject obj, jint x, jint y ){
LOGD ("TEST % d + % d = % d", x, y, x + y );
Return add22 (x, y );
}
E) Enter mm in the root directory of the project. The compilation prompt is out/target/product/sk886x/obj/STATIC_LIBRARIES/libtestapi_intermediates/libtestapi. a cannot be found. In this case, we will get libtestapi from B. a. Put it in the prompt folder and re-compile it. the apk is successfully installed on the target machine.
4. On the basis of step 3, let callapp. c call libtesapi. so
A) modify the project root directory, android. mk
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_MODULE_TAGS: = user eng
LOCAL_SRC_FILES: = $ (call all-subdir-java-files)
LOCAL_PACKAGE_NAME: = jnitest
LOCAL_JNI_SHARED_LIBRARIES: = libapptest libtestapi
Include $ (BUILD_PACKAGE)
Include $ (LOCAL_PATH)/jni/Android. mk
# Use the folloing include to make our test apk.
Include $ (call all-makefiles-under, $ (LOCAL_PATH) Note that libapptest libtestapi must be in the same line. Otherwise, the previous one will not be packaged into the apk.
B) add Android. mk IN THE Jni directory
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_MODULE_TAGS: = user eng
LOCAL_MODULE: = libapptest
LOCAL_SRC_FILES: = callapp. c
LOCAL_C_INCLUDES + = \
$ (JNI_H_INCLUDE)
LOCAL_SHARED_LIBRARIES: = libutils libtestapi
# LOCAL_STATIC_LIBRARIES: = libtestapi
LOCAL_LDLIBS: =-ldl-llog
LOCAL_PRELINK_MODULE: = false
Include $ (BUILD_SHARED_LIBRARY)
C) Compile and prompt that libtestapi cannot be found. so, we will copy it to out/target/product/sk886x/obj/lib/AND/out/target/product/sk886x/system/lib.
D) run the apk and an error is displayed. because libtestapi. so is provided to libapptest using a dynamic shared library. so, so libtestapi. so you need to put it in/system/lib, push it to/system/lib through adb, and re-run the apk to run it correctly.