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?
- #ifndef _android_log_h
- #define _android_log_h
- #include <stdarg.h>
- #ifdef __cplusplus
- extern "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 takes 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 a chance
- * To inspect 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
[CPP]View PlainCopyprint?
- #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 behind the C1, requiring C1 length >c2 length
- Logi ("copy array Finish");
- return (*env)->newstringutf (env, "hello_jni__");
- }
In the code above
[CPP]View PlainCopyprint?
- #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
[HTML]View PlainCopy print?
- 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!
Android NDK Development (iii)--Common error Brocade collection log using "Go"