Single-step debugging and log printing of the JNI program in Android Studio, androidjni
Recently, an algorithm (collision detection) needs to be implemented in C ++ to share the same program in IOS and ANDROID.
The following describes some things that android has encountered when calling this program. (Some Related Articles are searched online in the process, most of which are about tutorials in the eclipse environment. Therefore, it is necessary to write this article so that you do not forget it and make a detour)
The algorithm has been completed. The next natural process is to debug the program in one step and print logs. My IDE environment: Android Studio 1.5.1, ndk is the android-ndk-r10e. First, how to set the ndk directory, how to put the. h and. cpp files in the project, How to Write similar
JNIEXPORT jboolean JNICALL Java_com_example_shenzhigang_collisiondetection_Peng_isCollided
Such code, and so on. There are many tutorials on the Internet and will not be described.
Support for One-Step debugging:
Step 1: Open the build. gradle file of the module and add code similar to the following to the defaultConfig node.
ndk {
moduleName "HelloJNI"
stl "stlport_static"
ldLibs "log"
}LdLibs "log" here will be mentioned later, moduleName "HelloJNI" Here HelloJNI only needs
System.loadLibrary("HelloJNI");Here, we can.
Step 2: Add the following code to buildTypes (also the file in step 1)
debug {
jniDebuggable true
}
Step 3: Synchronize the build. gradle file. If there are no red forks in the icons, select app-native. DEBUG now .....
========================================================== ========================================================== ====================
The following describes how to output logs in the jni program. Here is the difference between android studio and eclipse. It is also the main purpose of my article:
Step 1: Open the build. gradle file of the module and add the following code to the ndk node under defaultConfig.
ldLibs "log"
By the way, in eclipse, you can directly modify the Android. mk file and add Code such as LOCAL_LDLIBS: =-L $ (SYSROOT)/usr/lib-llog. Although android studio will eventually generate the Android. mk file, if this file is modified, it will be automatically overwritten every build.
Step 2: This step is the same as that in eclipse. Under the file that needs to print logs,
#include <android/log.h>
Use the _ android_log_print function.