How to configure the debug environment and debug environment for NDK Development

Source: Internet
Author: User

How to configure the debug environment and debug environment for NDK Development

At the beginning of NDK development, Android Studio didn't provide native C/C ++ for breakpoint debugging. We all output logs for debugging, which is time-consuming and labor-intensive. Android Studio should be provided in version 2.2 to set the breakpoint debug function, and cmake compilation is also provided in this version.
I am still used to developing with NDK-Build (that is, setting Android. mk) when developing NDK. I will briefly explain how to debug with output logs:
1. First, add the logstore In the Android. mk setting MODULE, as shown in the following code:

include $(CLEAR_VARS)LOCAL_MODULE := ndktestLOCAL_SRC_FILES := native_lib.cLOCAL_LDLIBS += -llog include $(BUILD_SHARED_LIBRARY)

 

2. Add the following content in the native_lib.c implementation file:

# Include <android/log. h> // for convenient calling, output macro definition # define LOG_TAG "NATIVE_OUTPUT" # define LOGI (...) (void) _ android_log_print (ANDROID_LOG_INFO, LOG_TAG, _ VA_ARGS _) # define LOGE (...) (void) _ android_log_print (ANDROID_LOG_ERROR, LOG_TAG, _ VA_ARGS __))

 

3. Finally, the log can be output in the native_lib.c implementation file method, such:

LOGI("run here");LOGI("run i=%d", i);

 

The above is a complicated debugging method. Every time you write the log, you have to re-compile the code. Now we will describe how to set up native C/C ++ breakpoint debugging methods:
1. First, upgrade your Android Studio to 2.2 or higher, and set the buildToolsVersion parameter of build. gradle under the Module to 25 or above: buildToolsVersion '25. 100'

2. If you are creating a New Project, you can select the check box (Include C ++ support) under the Application name option when you are creating a New Project. When IDE is building a Project, it will help you configure the debug environment.

3. If you have migrated the package from an old project (that is, the previous Android. mk configuration), you can open the build. gradle file under the Module.
Add ndk to the defaultConfig node as follows:

Ndk {moduleName "imagetool" abiFilters "armeabi" // compile the supported platforms // according to your Application. mk configuration, configure according to gradle rules}

 

Then add the externalNativeBuild node to the Android node:

ExternalNativeBuild {ndkBuild {// relative path of Android. mk "src/main/jni/Android. mk "}}

 

Then sync the Project to synchronize the Project. You will see several more folders in the Project:


When you click the Attach debugger to Android process button on the right of the Run Debug button, if Native and Dual are selected:

 


When you click the Attach debugger to Android process button on the right of the Run Debug button, if Native and Dual are selected:

It indicates that you configured successfully, you can set the breakpoint in the native-lib.c to debug. The above dialog box is the debuger configuration. The default option is auto. You can set it to native to debug your natvie code.

When debugging to native is as follows:

 

You can use LLDB as your Debug auxiliary tool and how to use LLDB. This chapter is not detailed. You can refer to other materials.

References:
Https://developer.android.google.cn/studio/debug/index.html
Https://developer.android.google.cn/index.html (Android official website)

Tips: assume that you have configured the NDK development environment and the LLDB plug-in.

The following error occurs during debug:
Error: FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task': app: externalNativeBuildDebug '.
> Build command failed.
Error while executing process E: \ Android \ Sdk \ ndk-bundle \ ndk-build.cmd with arguments {NDK_PROJECT_PATH = null APP_BUILD_SCRIPT = G: \ project \ Android \ Anti-shake-arithmetic \ app \ src \ main \ jni \ Android. mk NDK_APPLICATION_MK = G: \ project \ Android \ Anti-shake-arithmetic \ app \ src \ main \ jni \ Application. mk APP_ABI = armeabi NDK_ALL_ABIS = armeabi NDK_DEBUG = 1 APP_PLATFORM = android-15 NDK_OUT = G: /project/Android/Anti-shake-arithmetic/app/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT = G: \ project \ Android \ Anti-shake-arithmetic \ app \ build \ intermediates \ ndkBuild \ debug \ lib G: /project/Android/Anti-shake-arithmetic/app/build/intermediates/ndkBuild/debug/obj/local/armeabi/libNDKUtils. so}
[Armeabi] StaticLibrary: libclapack1.a
Process_begin: CreateProcess (NULL, E:/Android/Sdk/ndk-bundle/build //.. /toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/arm-linux-androideabi-ar crsD ....

It is very likely that the configuration information of the ndk node in your defaultConfig node does not match Application. mk. You need to add it to the ndk node.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.