Native and JNI development on Android (1)

Source: Internet
Author: User

Recently developed on Android, it mainly refers to Porting some libraries on Win32 and Windows Mobile to andriod and encapsulating the secondary development interface at the Java layer.

Although Google is completely encapsulated, Android is a Linux system after all. Many Linux-related things cannot be bypassed in native development of Android. I have never been too lazy to touch Linux. How troublesome ...... It wasn't long before Vista came out. I heard how the Ubuntu interface was gorgeous and blabla, far from being comparable to the Red Hat Linux. So the first installation of ubuntu was 8.04, it does not support my laptop's hd2400 graphics card ...... Later I heard that it was supported, so I installed 11.04. Unfortunately, I changed a new version, but it does not support switching my NVIDIA/I5 graphics card ......

The two main technologies used in the past few years are ASP. NET platform and VC. In the hosting industry, speaking of. NET is about the same as speaking of 1 + 1. Even the Masters of European and American books say that the overall level of. Net programmers is lower than that of Java ...... How can they determine that the. NET course must be a Drag Control Stream ?...... So when someone asks me, I say that I write c ++ ...... Of course, I have been writing C ++ recently.

I found that there are few discussions about Native Development and encapsulation on Android, so I will summarize some things while doing so. Many people have already written many things about the installation and use of Android ndk and SDK, native development methods, and JNI on Android, this article mainly discusses the problems and solutions that will be encountered in the next step after writing Hello word.

1. Some changes from VC to Android native C ++

The function libraries and language-related things will not be mentioned. People who have read the C/C ++ open source code library all know the requirements of cross-platform code, various macro judgments, and platform-independent function encapsulation, byte alignment, large-end and small-end processing, I think the SQLite VFS is a beautiful cross-platform code ...... That is, writing is quite troublesome.

It mainly introduces the similarities and differences in compilation.

1. In Windows, DLL is used to export functions, write def files, or use ugly _ declspec (dllexport ). Similar mechanisms are not required in Linux. By default, all symbols are exported and not required. for Lib, you can directly link a so file and define the class or function in it through the header file.

2. in Android Application Development (regardless of the android source code), native compilation can be implemented in two ways: 1) ndk-build 2) install the android GCC tool chain using ndk, write makefile by yourself.

Under VC, it is estimated that there are not many people compiling C ++ with nmake. VC is always smart in compiling all. cpp in the project, silly IDE. The problems related to these two compilation methods provided by ndk are summarized a little later.

3. to ensure compatibility of later versions, Android provides only a small number of stable C ++ APIs in ndk, which is really small enough, however, the Linux Standard C functions and Standard C ++ libraries are basically complete. In addition to C functions and STL, do you have more requirements? Compile it by yourself...

4. debugging is really difficult. In Windows, the gdb connection debugging is only successful once, and the rest are inexplicably stuck. Use the log for the time being. It is also convenient, because it is a library that has been properly run on Win32 and WM, so there are not many debugging tasks.

2. Android. mk and application. mk

The help documents under the ndk are mandatory, and the entire ndk will not be able to read a few pages.

When I wrote Android. mk, I still encountered several small traps.

1. Path Problems. Do not write paths like/cygdrive/... under various MK types. Android is developed under Win. ndk requires cygwin. However, the ultimate compiler gcccategory is also the windows program of .exe. MK still needs to write the Windows path, the acceptable form such as/C:/ndk/XXXXX or direct C:/ndk/xxx. Pay attention to the slash direction.

2. local_path: = $ (call my-DIR), a trap. Therefore, you must carefully read the documents on those pages. $ (Call my-DIR) does not actually return [path of the current MK file], but [path of the last opened MK file]. The following error occurs:

Local_path: = $ (call my-DIR)

Include $ (project_path)/prebuilt. mk

.........

In this case, local_path may be rewritten in prebuilt. mk.

Include $ (project_path)/prebuilt. mk

Local_path: = $ (call my-DIR)

.........

In this case, local_path actually obtains the prebuilt. mk path.

MK is actually part of makefile. You can refer to Android. mk of some libraries in ndk. In fact, it is very flexible to write.

3. when using ndk-build, ndk-build directly finds JNI/android. MK, all MK contained in this MK will also be added, but this sentence does not occur when the $ (build_shared_library) Statement is executed, just remember to compile this library for a moment. Ndk-build starts to compile each database until all MK is executed. Therefore, the idea of adding commands such as copying the compiled database to the specified location in MK is just a dream, okay, I had this dream, but I finally wrote another compilation script.

3. Chinese Encoding Problems

The Chinese encoding problem on ndk is definitely a big tiger, at least better than paper tiger ......

In fact, there is no need to emphasize "Chinese" encoding, which is simply a problem of conversion of different character sets.

First of all, android2.2 previously did not support wchar_t. Well, we do support wchar_t now. However, in GCC, wchar_t is a 4-byte, not 2-byte! No wonder you can see that wchar_t is not used in various open source libraries, and people directly use void * or unsigned short. Jchar in JNI is also unsigned short, so wchar_t is purely used by hackers.

Although the compilation command-fshort-wchar can be used to force wchar_t to use 2 bytes, there will be countless warnings when connecting to other libraries ...... Unless you guarantee that all the libraries you encounter (including the C/C ++ standard library) are compiled using this command. I tried a very sharp wchar16 definition to automatically select the most appropriate type to ensure that wchar16 is always 2 byte characters:

template<int>struct tag_auto_WCHAR_CHOOSER;template<>struct tag_auto_WCHAR_CHOOSER<4> {typedef unsigned short WCHAR16;};template<>struct tag_auto_WCHAR_CHOOSER<2> {typedef unsigned short WCHAR16;};typedef tag_auto_WCHAR_CHOOSER<sizeof(wchar_t)>::WCHAR16 WCHAR16;

However, in GCC, it cannot be guaranteed that wchar16 and unsigned short are used for the same parameter signature overload function, and I just want to overload the function, so wchar_t will be useless to me. discard it.

Okay, it's a bit out of question...

The first is the encoding of source code files:The default encoding of C ++ char * under ndk is UTF-8, But if you write: const char * szchina = "China", then the day knows what encoding. By default, if your code file itself uses GBK, then szchina is GBK encoding, and if the code file is a UTF-8, szchina is also a UTF-8. You have to ask if the code file is UTF-16? I did not try, but if your code can be compiled, want to szchina will be a UTF-8, the reason, said here you know. If you do not understand, you can first find
MBCS/Unicode tutorial, find the GBK/UTF-8/UTF16 encoding rules and Its Relationship with Unicode tutorial, find UCS-2/UTF-16LE/UTF-16BE difference analysis, finally look at GCC for different encoding source file processing, then you will understand it.

Encoding conversion:Ndk does not provide any direct encoding conversion methods, but you can compile a very famous library, iconv, of course, to write Android. mk, but you can download it.Hgl868Modified ...... It is a little big after compilation, more than 900 K ...... But remove the UTF-8, UTF-16LE, GBK and Other encoding libraries, iconv can be reduced to 200 k ...... As for the use, the portal of a great family just clicked to open the link.

As for how to return the Java conversion method, save the trouble ...... Which conversion library inside Android is used? Are you sure you can use this to continue running normally on Android 4.0?

Related Article

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.