Android NDK Development (iii)--common error collection and log usage

Source: Internet
Author: User

Reprint Please specify source:http://blog.csdn.net/allen315410/article/details/41826511

Android NDK Development often due to some of the causes of some unexpected errors, many times debugging these errors, it is more complicated than debugging Java code, on the one hand, the cause of the error is a lot of miscellaneous, on the other hand, the NDK development involves the writing of C + + code, many programmers are unfamiliar with it. So this blog summarizes some of the problems that often arise in the development of NDK, and attempts to provide the right solution to quickly locate errors, change errors, and of course, errors are varied, and it is difficult to sum up all the errors here, just as a note, Later in the development of the NDK to find a mistake or solve a mistake, I am here to record, over the years, it is good!

Common Errors and workarounds
1,ANDROID.MK file does not exist

Error Description:Android Ndk:your App_build_script points to an unknown file:./jni/android.mk
/cygdrive/e/ndk/android-ndk-r10d/build/core/add-application.mk:199: * * * Android ndk:aborting .... Stop it.

Workaround: To report this error, you need to look at the JNI directory under the project directory, whether there is a android.mk file, or if the Android.mk file name has been entered incorrectly.


2,ANDROID.MK File Configuration Error


Error description:/cygdrive/e/ndk/android-ndk-r10d/build/core/build-shared-library.mk:23: * * * Android ndk:missing LOCAL_ MODULE before including build_shared_library in Jni/and Roid.mk. Stop it.

Workaround: Check the android.mk file configuration information. Missing Local_module indicates an error in Local_module configuration and is reviewed and corrected.


3, C language code has errors


Error description:[Armeabi] Compile Thumb:hello <= hello.c
jni/hello.c:in function ' Java_com_example_ndk_mainactivity_java_1from_1jni ':
Jni/hello.c:17:9: Warning:division by zero [-wdiv-by-zero]
int i=5/0;
^
[Armeabi] SharedLibrary:libHello.so
[Armeabi] Install:libHello.so = libs/armeabi/libhello.so

Solution: C language error is usually a lot of wrong information, we can be based on the log on the Cygwin to the error.


C code library not found in 4,java code
Error Description:Androidruntime (1171): Java.lang.UnsatisfiedLinkError:Couldn ' t load Hello1 from Loader Dalvik.system.pathclassloader[dexpathlist[[zip file "/data/app/com.example.ndk-1.apk"],nativeLibraryDirectories= [/data/app-lib/com.example.ndk-1,/system/lib]]: Findlibrary returned null

WORKAROUND: The Java code will be loaded in the C code library when the name of the C code base is coarse, please correct in the Java layer.


5,c Code function Signature Error


Error Description:java.lang.UnsatisfiedLinkError:Native Method not Found:com.example.ndk.MainActivity.java_From_JNI: () ljava/lang/string;

Workaround: Native method not found. This should not be difficult, one can see that the C language function signature error.


6,NDK version Issue


Error description:Android NDK:WARNING:APP_PLATFORM android-19 is larger than Android:minsdkversion 8 in./androidmanifest.xml
[Armeabi] Compile Thumb:hello <= hello.c
[Armeabi] SharedLibrary:libHello.so
[Armeabi] Install:libHello.so = libs/armeabi/libhello.so

Workaround: This is just a warning, the program does not handle the same operation. The reason for this warning is that the current NDK version > Project minsdkversion, to remove this warning will change the minimum supported version of Minsdkversion to the NDK version number, of course it is foolish, but we can use the NDK's low version of the compiler, It's not very good. Here's a good workaround:

1, create a new file under the project directory JNI, the file name is Application.mk

2, add such a sentence to the Application.mk file:

APP_PLATFORM := android-8

3. Save the project, compile it, and see the warning is gone.


7, the class file cannot be found when generating a function signature using the Javah command


Error Description: Error: Unable to access android.app.Activity
The class file for android.app.Activity could not be found

Workaround: The specific reason for this error should not find the corresponding native method is located in the Java bytecode file, but I do the path is correct, or error, very strange, do not know is not a bug on eclipse. Encountered this problem, can be resolved, since the switch to the \bin\classes directory, then switch to the project directory \SRC directory, and then Javah, this time incredibly generated. h header file, do not know why this, anyway I test the time feasible.

Tip: Get the local method header file
jdk6.0: In the bin\classes Directory of Android Project: Javah Package name + class name
jdk7.0: Executed in the SRC directory of Android project: Javah Package name + class name


8, Chinese garbled problem

Error description:NDK development often in the C code in the Java code to return a Chinese string, occasionally called in Java, the program results will appear in Chinese garbled situation, or even more causes the program to crash directly, view log log is also a description of garbled situation.

Workaround:

The reason for the Chinese garbled is that the English C language file is saved in a format other than the UTF-8 format, or the entire project is not a UTF-8 format, because the C language JNI passes the string UTF-8 encoding, which can be in (*env)->newstringutf (env, " Hello jni! ") See, Newstringutf (env,char*) This method indicates that the string returned is a UTF-8 encoded form. So when we build the project, or create a new C language code file, we need to specify that the project encoding for UTF-8 or C language code file Save format is UTF-8.


9, encoding GBK characters that are not mapped


Error description: encoded GBK characters.

Workaround: This error is caused by not specifying the Java encoding set when using Javah, in which case the compiler automatically compiles according to the Windows default encoding (GBK), while Java supports the UTF-8 encoding set. The solution to this problem is to specify an encoding set for the compiler when the Javah command executes,-encoding the encoding set with the parameters of the Javah command,



Use of log logs

The above list some of the NDK development often encountered problems and solutions, but the only one not listed, but also the most common error, is the C language code errors, this is not a good solution, and the problem is a variety of specific situations specific treatment. It is known that when using Java to develop Android programs, Google in order to facilitate the programmer debugging, in the SDK provides the log output function, the programmer used to output the log in the program. Fortunately, Google also provides a similar log mechanism in the NDK, which helps to locate the native layer code incorrectly. Let's try using this log mechanism.

In the NDK decompression directory platforms\android-8\arch-arm\usr\include\android has a log.h header file, this log.h header file is used to manage the C language code in the log output, the code is as follows:

#ifndef _android_log_h#define _android_log_h#include <stdarg.h> #ifdef __cplusplusextern "C" {#endif * * * ANDROID Log priority values, in ascending priority order. */typedef enum android_logpriority {android_log_unknown = 0, Android_log_default,/* Only for setminpriority () * /Android_log_verbose, Android_log_debug, Android_log_info, Android_log_warn, Android_log_error, ANDROID _log_fatal, Android_log_silent,/* Only for setminpriority (); Must is last */} android_logpriority;/* * Send A simple string to the log. */int __android_log_write (int prio, const char *tag, const char *text);/* Send a formatted string to the log, used like printf (fmt,...) */int __android_log_print (int prio, const char *tag, const char *FMT, ...) #if defined (__gnuc__) __attribute__ ((Format (printf, 3, 4)) #endif;/* A variant of __android_log_print () that Tak Es a va_list to list * additional parameters.          */int __android_log_vprint (int prio, const char *tag,               const CHAR *FMT, va_list AP);/* Log an assertion failure and SIGTRAP the process to has a chance * to SPECT it, if a debugger is attached. This uses the FATAL priority.    */void __android_log_assert (const char *cond, const char *tag, const char *FMT, ...) #if defined (__gnuc__) __attribute__ ((noreturn)) __attribute__ ((Format (printf, 3, 4)) #endif; #ifdef __cplusplus } #endif #endif/* _android_log_h */
The above code does not understand it is OK, we just need to know how to use the line.

1, the log.h header file is referenced in the C language code, and the predefined log_tag tag tag name, predefined output rule:

2, define the contents of the log output with the predefined name above

#include <stdio.h> #include <jni.h> #include "com_example_ndk_mainactivity.h" #include <android/log.h > #define LOG_TAG "system.out.c" #define LOGD (...) __android_log_print (Android_log_debug, Log_tag, __va_args__) # Define Logi (...) __android_log_print (Android_log_info, Log_tag, __va_args__) jniexport jstring jnicall Java_com_ Example_ndk_mainactivity_javafromjni (jnienv* env, Jobject obj) {return (*env)->newstringutf (env, "Hello jni!");} /* Class:     com_example_ndk_mainactivity * Method:    java_from_jni * Signature: () ljava/lang/string; */jniexport Jstring Jnicall Java_com_example_ndk_mainactivity_java_1from_1jni (jnienv* env, Jobject obj) {LOGI ("function called"); Logi ("array init"); char c1[3] = {' A ', ' B ', ' C '};char c2[2] = {' d ', ' e '}; Logi ("Array init finish"); Logi ("copy array"); Strcat (c1, C2); Put the contents of the C2 in the back of the C1, requiring C1 length >C2 length logi ("Copy array Finish"), Return (*ENV)->newstringutf (env, "hello_jni__");}
In the code above
#include <android/log.h> #define LOG_TAG "system.out.c" #define LOGD (...) __android_log_print (android_log_debug , Log_tag, __va_args__) #define Logi (...) __android_log_print (Android_log_info, Log_tag, __va_args__)
is the tag tag that must be added to tell the compiler to output log,log here is "system.out.c", and the predefined Logd (...) Represents the debug output, Logi (...) Represents the info output. Then in the C language theme code can use LOGD and Logi, pass the string on it, it should be noted that the string to be passed in English characters, does not support Chinese.

3. Configure the log output in the Android.mk file

    Local_path: = $ (call My-dir)    include $ (clear_vars)    local_module    : = Hello    local_src_files: = hello.c        Local_ldlibs + =-llog    include $ (build_shared_library)
Note: Just one sentence local_ldlibs + =-llog is OK, but must be added before include $ (build_shared_library).

4, recompile the code, run to see the effect


OK, we filter out the logcat inside the system.out.c tag, you can see the output copy array after the program stopped, the description Logi ("copy array"); the strcat below (c1, C2); An error occurred, This need to modify the code in the error, the program can be executed correctly, is not very convenient ah? Give it a try!


Please download the source code here


Android NDK Development (iii)--common error collection and log usage

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.