Porting libiconv and libxml2 to Android NDK

Source: Internet
Author: User

Use Android NDK to compile libxml2 and libiconv libraries simultaneously
First, make preparations. follow the steps below to create a directory and prepare the libiconv and libxml2 compressed files.
(The version I use here is: libiconv-1.14.tar.gz libxml2-git-snapshot.tar.gz [libxml2-2.8.0 ])
Create a directory as follows:
Buildlibxml2 (root)
Jni-> Android. mk (root-child)
Jni-> main. c (root-child)
Libiconv (root-child)
Libxml2 (root-child)
The Android. mk file is as follows:
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_MODULE: = main
LOCAL_SRC_FILES: = main. c
LOCAL_C_INCLUDES: = \
$ (LOCAL_PATH)/libxml2/include \
$ (LOCAL_PATH)/libxml2/include/libxml \
$ (LOCAL_PATH)/libiconv/include \
$ (LOCAL_PATH)/libiconv \
$ (LOCAL_PATH)/libiconv/libcharset \
$ (LOCAL_PATH)/libiconv/lib \
$ (LOCAL_PATH)/libiconv/libcharset/include
LOCAL_SHARED_LIBRARIES: = libiconv libxml2
Include $ (BUILD_EXECUTABLE)
Include $ (call all-makefiles-under, $ (LOCAL_PATH ))
The main. c file is as follows:
# Include <stdio. h>
# Include <stdlib. h>
# Include "libxml2/include/libxml/parser. h"
# Include "libxml2/include/libxml/tree. h"

Int main (int argc, char ** argv)
{
XmlDocPtr pdoc = NULL;
XmlNodePtr proot_node = NULL, pnode = NULL, pnode1 = NULL;
 
Pdoc = xmlNewDoc (BAD_CAST "1.0 ");
Proot_node = xmlNewNode (NULL, BAD_CAST "root node ");
XmlNewProp (proot_node, BAD_CAST "version", BAD_CAST "1.0 ");
XmlDocSetRootElement (pdoc, proot_node );
 
Pnode = xmlNewNode (NULL, BAD_CAST "child node 1 ");
XmlNewChild (pnode, NULL, BAD_CAST "child node 1", BAD_CAST "info ");
XmlAddChild (proot_node, pnode );
 
Pnode1 = xmlNewNode (NULL, BAD_CAST "child node 1 ");
XmlAddChild (pnode, pnode1 );
XmlAddChild (pnode1, xmlNewText (BAD_CAST "this will lower node, child node 1 "));
 
XmlNewTextChild (proot_node, NULL, BAD_CAST "child node 2", BAD_CAST "child node 2 info ");
XmlNewTextChild (proot_node, NULL, BAD_CAST "child node 3", BAD_CAST "child node info 3 ");
 
XmlSaveFormatFileEnc (argc> 1? Argv [1]: "-", pdoc, "UTF-8", 1 );
 
XmlFreeDoc (pdoc );
XmlCleanupParser ();
XmlMemoryDump ();
Return 0;
}
 
Decompress the libiconv library and libxml2 with the tar xvvzf command
Enter the terminal to enter the libiconv directory
./Configure -- build = x86_64-pc-linux-gnu -- host = arm-linux-eabi
Go to the libxml2 directory
./Configure (use this because the above command is stuck to a step)
Add the Android. mk file in the libiconv and libxml2 folders
Libiconv Android. mk:
LOCAL_PATH: = $ (call my-dir)
# Libiconv. so
Include $ (CLEAR_VARS)
LOCAL_MODULE: = libiconv
LOCAL_CFLAGS: = \
-Wno-multichar \
-DANDROID \
-DLIBDIR = "c "\
-DBUILDING_LIBICONV \
-DIN_LIBRARY
LOCAL_SRC_FILES: = \
Libcharset/lib/localcharset. c \
Lib/iconv. c \
Lib/relocatable. c
LOCAL_C_INCLUDES + = \
$ (LOCAL_PATH)/include \
$ (LOCAL_PATH)/libcharset \
$ (LOCAL_PATH)/lib \
$ (LOCAL_PATH)/libcharset/include \
$ (LOCAL_PATH)/srclib
 
Include $ (BUILD_SHARED_LIBRARY)
Libxml2 Android. mk:
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_SRC_FILES: = \
C14n. c catalog. c chvalid. c debugXML. c dict. c DOCBparser. c \
Encoding. c entities. c error. c globals. c hash. c HTMLparser. c \
HTMLtree. c legacy. c list. c nanoftp. c nanohttp. c parser. c \
ParserInternals. c pattern. c relaxng. c SAX. c SAX2.c \
Threads. c tree. c trionan. c triostr. c uri. c valid. c \
Xinclude. c xlink. c xmlIO. c xmlmemory. c xmlmodule. c \
Xmlreader. c xmlregexp. c xmlsave. c xmlschemas. c xmlschemastypes. c xmlstring. c \
Xmlunicode. c xmlwriter. c xpath. c xpointer. c
LOCAL_C_INCLUDES: = \
$ (LOCAL_PATH)/include \
$ (LOCAL_PATH)/include/libxml \
$ (LOCAL_PATH)/../libiconv/include \
$ (LOCAL_PATH)/../libiconv \
$ (LOCAL_PATH)/../libiconv/libcharset \
$ (LOCAL_PATH)/../libiconv/lib \
$ (LOCAL_PATH)/../libiconv/libcharset/include
LOCAL_MODULE: = libxml2
LOCAL_SHARED_LIBRARIES: = libiconv
Include $ (BUILD_SHARED_LIBRARY)
Open the terminal and enter the buildlibxml2 directory for ndk-build
Langinfo. h No such file or directiory
View the file \ buildlibxml2 \ jni \ libiconv \ libcharset \ lib \ localcharset. c
# If HAVE_LANGINFO_CODESET
# Include <langinfo. h>
# Else
# If 0/* see comment below */
# Include <locale. h>
# Endif
# Endif
Solution: \ buildlibxml2 \ jni \ libiconv \ libcharset \ config. h
Change the HAVE_LANGINFO_CODESET definition to 0.
# Define HAVE_LANGINFO_CODESET 0
Error
'C' undeclared identitfier
View the file \ buildlibxml2 \ jni \ libiconv \ libcharset \ lib \ localcharset. c
Search for int c
Move the int c definition to the beginning of the get_charset_aliases Function

Error ansidecl. h No such file or directiory
Find the cause and view the file \ buildlibxml2 \ jni \ libxml2 \ include \ libxml \ xmlversion. h.
# Ifdef HAVE_ANSIDECL_H
# Include <ansidecl. h>
# Endif
Solution: open the file \ buildlibxml2 \ jni \ libxml2 \ config. h
Comment out define HAVE_ANSIDECL_H
// # Define HAVE_ANSIDECL_H 1
Report various issues such as undefined gzopen
Run \ buildlibxml2 \ jni \ libxml2 \ config. h
HAVE_LIBZ and HAVE_ZLIB_H
// # Define HAVE_LIBZ 1
// # Define HAVE_ZLIB_H 1
Note
Error rand_r undefined
Find the cause and view the file \ buildlibxml2 \ jni \ libxml2 \ dict. c.
# Ifdef HAVE_RAND_R
Rand_seed = time (NULL );
Rand_r (& rand_seed );
# Else
Srand (time (NULL ));
# Endif
# Ifdef HAVE_RAND_R
Ret = rand_r (& rand_seed );
# Else
Ret = rand ();
# Endif
Solution: open the file \ buildlibxml2 \ jni \ libxml2 \ config. h
Annotate HAVE_RAND_R Definition
// # Define HAVE_RAND_R 1

The ndk-build is successfully compiled.
I only encountered the above problems when compiling these two libraries. Fortunately. Compiled! If you are ready to use it for a while, it is not painful to see that cygwin cannot be used for compiled executable files in windows.-# Hey! Sorrow
Cannot execute binary file
And then I use it in linux... Binary files cannot be executed
Then I pushed it to the android simulator to see if I could use./main to run .. sorry, permission denied has no permission.
Why are you calling me a root simulator ....
I tried a command.
Adb remount
Adb shell
Mount-o remount, rw-t yaffs2/dev/block/mtdblock3/system
Cd/system/bin
Cat sh> su
Chmod 4755 su
Su
No.
We have modified the JNI connection test.
Haha !!
Main activity:
Package build. libxml2;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. Log;
Import android. widget. TextView;
Public class testActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// SetContentView (R. layout. main );

TextView TV = new TextView (this );
TV. setText ("the program is running! ");
Log. v ("1", "The program is started! ");
SetContentView (TV );
TV. setText ("starting to run xmltest! ");
Log. v ("1", "started to run xmltest ");
Natives. xmltest ();
}
}
Natives. class
Package build. libxml2;
Import android. util. Log;
Public class natives {
Static
{
Log. v ("loadlibrary", "start load iconv ");
System. loadLibrary ("iconv ");
Log. v ("loadlibrary", "succse load iconv ");
Log. v ("loadlibrary", "start load xml2 ");
System. loadLibrary ("xml2 ");
Log. v ("loadlibrary", "succse load xml2 ");
Log. v ("loadlibrary", "start load main ");
System. loadLibrary ("main ");
Log. v ("loadlibrary", "succse load main ");
}
 
Public static native void xmltest ();
}
Modify the main. c file
# Include <stdio. h>
# Include <stdlib. h>
# Include "libxml2/include/libxml/parser. h"
# Include "libxml2/include/libxml/tree. h"
# Include <jni. h>
# Include <android/log. h>
JNIEXPORT void JNICALL Java_build_libxml2_natives_xmltest
(JNIEnv * env, jclass jc)
{
Int I;
_ Android_log_print (ANDROID_LOG_INFO, "FILE_OPT", "run to testmain ");
I = testmain ();
_ Android_log_print (ANDROID_LOG_INFO, "FILE_OPT", "fun return % d", I );
}
// Int main (int argc, char ** argv)
Int testmain ()
{
Char * szDocName = "/mnt/sdcard/yy. xml ";
FILE * fp;
Fp = fopen (szDocName, "a + ");
If (fp = NULL)
{
Fclose (fp );
Return 0;
}
_ Android_log_print (ANDROID_LOG_INFO, "FILE_OPT", "file create ");
XmlDocPtr pdoc = NULL;
XmlNodePtr proot_node = NULL, pnode = NULL, pnode1 = NULL;
 
Pdoc = xmlNewDoc (BAD_CAST "1.0 ");
Proot_node = xmlNewNode (NULL, BAD_CAST "root node ");
XmlNewProp (proot_node, BAD_CAST "version", BAD_CAST "1.0 ");
XmlDocSetRootElement (pdoc, proot_node );
 
Pnode = xmlNewNode (NULL, BAD_CAST "child node 1 ");
XmlNewChild (pnode, NULL, BAD_CAST "child node 1", BAD_CAST "info ");
XmlAddChild (proot_node, pnode );
 
Pnode1 = xmlNewNode (NULL, BAD_CAST "child node 1 ");
XmlAddChild (pnode, pnode1 );
XmlAddChild (pnode1, xmlNewText (BAD_CAST "this will lower node, child node 1 "));
 
XmlNewTextChild (proot_node, NULL, BAD_CAST "child node 2", BAD_CAST "child node 2 info ");
XmlNewTextChild (proot_node, NULL, BAD_CAST "child node 3", BAD_CAST "child node info 3 ");
 
// XmlSaveFormatFileEnc (argc> 1? Argv [1]: "-", pdoc, "UTF-8", 1 );
XmlSaveFormatFileEnc (szDocName, pdoc, "UTF-8", 1 );
XmlFreeDoc (pdoc );
XmlCleanupParser ();
XmlMemoryDump ();
Return 0;
}

Modify the \ buildlibxml2 \ jni \ Android. mk File
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_MODULE: = main
LOCAL_SRC_FILES: = main. c
LOCAL_C_INCLUDES: = \
$ (LOCAL_PATH)/libxml2/include \
$ (LOCAL_PATH)/libxml2/include/libxml \
$ (LOCAL_PATH)/libiconv/include \
$ (LOCAL_PATH)/libiconv \
$ (LOCAL_PATH)/libiconv/libcharset \
$ (LOCAL_PATH)/libiconv/lib \
$ (LOCAL_PATH)/libiconv/libcharset/include
LOCAL_SHARED_LIBRARIES: = libiconv libxml2
LOCAL_LDLIBS: =-llog
Include $ (BUILD_SHARED_LIBRARY)
Include $ (call all-makefiles-under, $ (LOCAL_PATH ))

Enter the terminal to enter the directory buildlibxml2 ndk-build
Generated successfully
Add file access permissions in AndroidManifest. xml
Run the program.
Use the adb pull command to copy/mnt/sdcard/yy. xml
View with notepad
<? Xml version = "1.0" encoding = "UTF-8"?>
<Root node version = "1.0">
<Child node 1>
<Child node 1> info </child node 1>
<Child node 1> this will lower node, child node 1 </child node 1>
</Child node 1>
<Child node 2> child node 2 info </child node 2>
<Child node 3> child node info 3 </child node 3>
</Root node>
The example of the test was borrowed and rewritten on the Internet. The specific link cannot be remembered!
Porting this library references some other blog knowledge, but most of them are self-written and integrated.
Successful !!!! After the test is successful, libxml2 and libiconv are successfully transplanted to android.


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.