Android NDK Development (iii)--Common error Brocade collection log using "Go"

Source: Internet
Author: User
Tags log log tag name

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 have an 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/android.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 error description not found in 4,java code: 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:

[CPP]View PlainCopyprint?
  1. #ifndef _android_log_h
  2. #define _android_log_h
  3. #include <stdarg.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /*
  8. * Android Log priority values, in ascending priority order.
  9. */
  10. typedef ENUM Android_logpriority {
  11. Android_log_unknown = 0,
  12. Android_log_default, /* only for setminpriority () */
  13. Android_log_verbose,
  14. Android_log_debug,
  15. Android_log_info,
  16. Android_log_warn,
  17. Android_log_error,
  18. Android_log_fatal,
  19. Android_log_silent, /* only for setminpriority (); must is last */
  20. } android_logpriority;
  21. /*
  22. * Send A simple string to the log.
  23. */
  24. int __android_log_write (int prio, const Char *tag, const char *text);
  25. /*
  26. * Send a formatted string to the log, used like printf (FMT,...)
  27. */
  28. int __android_log_print (int prio, const Char *tag, const char *fmt, ...)
  29. #if defined (__gnuc__)
  30. __ATTRIBUTE__ (Format (printf, 3, 4))
  31. #endif
  32. ;
  33. /*
  34. * A variant of __android_log_print () that takes A va_list to list
  35. * Additional parameters.
  36. */
  37. int __android_log_vprint (int prio, const char *tag,
  38. Const Char *fmt, va_list AP);
  39. /*
  40. * Log an assertion failure and SIGTRAP the process to a chance
  41. * To inspect it, if a debugger is attached. This uses the FATAL priority.
  42. */
  43. void __android_log_assert (const Char *cond, const char *tag,
  44. Const Char *fmt, ...)
  45. #if defined (__gnuc__)
  46. __attribute__ ((noreturn))
  47. __ATTRIBUTE__ (Format (printf, 3, 4))
  48. #endif
  49. ;
  50. #ifdef __cplusplus
  51. }
  52. #endif
  53. #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

[CPP]View PlainCopyprint?
  1. #include <stdio.h>
  2. #include <jni.h>
  3. #include "Com_example_ndk_mainactivity.h"
  4. #include <android/log.h>
  5. #define LOG_TAG "System.out.c"
  6. #define LOGD (...) __android_log_print (Android_log_debug, Log_tag, __va_args__)
  7. #define Logi (...) __android_log_print (Android_log_info, Log_tag, __va_args__)
  8. Jniexport jstring Jnicall Java_com_example_ndk_mainactivity_javafromjni (
  9. jnienv* env, Jobject obj) {
  10. return (*env)->newstringutf (env, "Hello jni!");
  11. }
  12. /*
  13. * Class:com_example_ndk_mainactivity
  14. * Method:java_from_jni
  15. * Signature: () ljava/lang/string;
  16. */
  17. Jniexport jstring Jnicall Java_com_example_ndk_mainactivity_java_1from_1jni (
  18. jnienv* env, Jobject obj) {
  19. Logi ("function called");
  20. Logi ("Array init");
  21. char c1[3] = { ' a ', ' B ', ' C '};
  22. char c2[2] = { ' d ', ' e '};
  23. Logi ("array init finish");
  24. Logi ("copy array");
  25. strcat (c1, C2); //Put the contents of the C2 behind the C1, requiring C1 length >c2 length
  26. Logi ("copy array Finish");
  27. return (*env)->newstringutf (env, "hello_jni__");
  28. }

In the code above

[CPP]View PlainCopyprint?
    1. #include <android/log.h>
    2. #define LOG_TAG "System.out.c"
    3. #define LOGD (...) __android_log_print (Android_log_debug, Log_tag, __va_args__)
    4. #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

[HTML]View PlainCopy print?
    1. Local_path : = $ (call My-dir)
    2. Include $ (clear_vars)
    3. Local_module : = Hello
    4. Local_src_files : = hello.c
    5. Local_ldlibs + =-llog
    6. 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!

Android NDK Development (iii)--Common error Brocade collection log using "Go"

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.