編輯:關於Android編程
使用Android NDK同時編譯libxml2和libiconv庫
首先做好准備工作按照如下創建好目錄並准備好libiconv和libxml2的壓縮文件
(我這裡使用的版本為:libiconv-1.14.tar.gz libxml2-git-snapshot.tar.gz【libxml2-2.8.0】)
創建目錄如下
buildlibxml2(root)
jni->Android.mk (root-child)
jni->main.c (root-child)
libiconv (root-child)
libxml2 (root-child)
Android.mk文件如下:
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))
main.c 文件如下
#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 child node 1", BAD_CAST "info");
xmlAddChild (proot_node, pnode);
pnode1 = xmlNewNode (NULL, BAD_CAST "child child node 1");
xmlAddChild (pnode, pnode1);
xmlAddChild (pnode1,xmlNewText (BAD_CAST "this will lower node, child child 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;
}
將libiconv庫和libxml2用tar xvvzf 命令解壓
進入終端 進入libiconv目錄
./configure --build=x86_64-pc-linux-gnu --host=arm-linux-eabi
進入libxml2目錄
./configure (用上面的命令卡到某個步驟因此用這個)
在libiconv和libxml2文件夾裡面添加Android.mk文件文件分別為
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)
打開終端進入buildlibxml2目錄 進行ndk-build
報錯 langinfo.h No such file or directiory
查找原因查看文件 \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
解決方法將\buildlibxml2\jni\libiconv\libcharset\config.h裡面
將HAVE_LANGINFO_CODESET定義改成 0
#define HAVE_LANGINFO_CODESET 0
報錯
'c'undeclared identitfier
查找原因查看文件 \buildlibxml2\jni\libiconv\libcharset\lib\localcharset.c
搜索int c
將int c的定義移到get_charset_aliases函數的開頭
報錯 ansidecl.h No such file or directiory
查找原因查看文件\buildlibxml2\jni\libxml2\include\libxml\xmlversion.h
#ifdef HAVE_ANSIDECL_H
#include <ansidecl.h>
#endif
解決方法打開文件\buildlibxml2\jni\libxml2\config.h
將define HAVE_ANSIDECL_H注釋
//#define HAVE_ANSIDECL_H 1
報各類的如gzdopen gzopen 未定義的問題
將\buildlibxml2\jni\libxml2\config.h
HAVE_LIBZ 和HAVE_ZLIB_H
//#define HAVE_LIBZ 1
//#define HAVE_ZLIB_H 1
注釋
報錯 rand_r未定義
查找原因查看文件\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
解決方法打開文件\buildlibxml2\jni\libxml2\config.h
將HAVE_RAND_R 定義注釋
//#define HAVE_RAND_R 1
重新ndk-build完成成功編譯
編譯這2個庫我只遇到以上的一些問題。還是很幸運。編譯好了!准備去用一下看可以用不痛苦的是我編譯的可執行文件用cygwin在windows不能直接用- -#哎!悲哀
直接是說cannot execute binary file
然後我就在linux下用也是一樣。。。不能執行二進制文件
然後我push到android模擬器上看能不能用./main運行..悲催permission denied 沒有權限
你叫我情何以堪....是不是要root模擬器....
試了一下一個命令
adb remount
adb shell
mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
cd /system/bin
cat sh > su
chmod 4755 su
su
發現不行 算了
還是好好的改JNI連接測試了。
哈哈哈!!
主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("開始程序運行了!");
Log.v("1", "程序開始了!");
setContentView(tv);
tv.setText("開始運行xmltest()了!");
Log.v("1", "開始運行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();
}
將main.c文件修改
#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 child node 1", BAD_CAST "info");
xmlAddChild (proot_node, pnode);
pnode1 = xmlNewNode (NULL, BAD_CAST "child child node 1");
xmlAddChild (pnode, pnode1);
xmlAddChild (pnode1,xmlNewText (BAD_CAST "this will lower node, child child 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;
}
將\buildlibxml2\jni\Android.mk文件修改
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))
進入終端進入目錄buildlibxml2 ndk-build
成功生成
在AndroidManifest.xml添加文件訪問權限
運行程序。
用adb pull 命令 將/mnt/sdcard/yy.xml拷出來
用notepad查看
<?xml version="1.0" encoding="UTF-8"?>
<root node version="1.0">
<child node 1>
<child child node 1>info</child child node 1>
<child child node 1>this will lower node, child child child node 1</child 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>
測試的例子是在網上廣為流傳的借用然後改寫了。具體鏈接記不起了!
移植這個庫參考了一些其他的博客知識但大多是自己撰寫和整編。
成功!!!!測試成功至此libxml2和libiconv成功移植到android
簡介在使用百度地圖SDK為您提供的各種LBS能力之前,您需要獲取百度地圖移動版的開發密鑰,該密鑰與您的百度賬戶相關聯。因此,您必須先有百度帳戶,才能獲得開發密鑰。並且,該
一、前言關於Android中的分包技術,已經不是什麼新的技術了,網上也有很多解析了,但是他們都是給了理論上的知道和原理解析,並沒有詳細的案例說明,所以這裡我們就來詳細講解
轉眼間近一年沒更新博客了,工作一忙起來,很難有時間來寫博客了,由於現在也在從事Android開發相關的工作,因此以後的博文也會更多地專注於這一塊。這篇文章准備從源碼層面為
手機卡未實名會停機、手機實名登記怎麼辦理呢?下文將為大家介紹針對即將於9月1日大規模推行的手機卡實名制認證,怎麼辦理呢?總的來說,辦理實名制可分為線下和線上