Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android -- libcurl移植、編譯與測試

android -- libcurl移植、編譯與測試

編輯:關於Android編程

      1、移植Curl工具到Android 2.3 環境步驟      1、直接到網站上下載 curl 源碼      2、利用tar在android編譯環境下,一般放在 external 目錄下      3、由於Curl依賴一些一些頭文件及定義需要先執行配置文件先,在external/curl 下面編譯curl.sh         需要 cd external/curl/.curl.sh進行執行,或者放在android根目錄下的*.sh腳本中執行:source external/curl/curl.sh         其中修改了相應的CURL路徑及編譯工具鏈路徑等變量   [cpp]   #!/bin/bash       ANDROID_HOME_CURL=../../ && \   NDK_HOME_CURL=../../ndk && \   PATH="$ANDROID_HOME_CURL/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin:$PATH" \   ./configure --host=arm-linux CC=arm-eabi-gcc --with-random=/dev/urandom \   CPPFLAGS="-I$NDK_HOME_CURL/platforms/android-8/arch-arm/usr/include \   -I $ANDROID_HOME_CURL/external/curl/include/  \   -I $ANDROID_HOME_CURL/external/curl/3rd/include   \   -I $ANDROID_HOME_CURL/external/curl   \   -I $ANDROID_HOME_CURL/out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates   \   -I $ANDROID_HOME_CURL/dalvik/libnativehelper/include/nativehelper   \   -I $ANDROID_HOME_CURL/system/core/include   \   -I $ANDROID_HOME_CURL/hardware/libhardware/include   \   -I $ANDROID_HOME_CURL/hardware/libhardware_legacy/include   \   -I $ANDROID_HOME_CURL/hardware/ril/include   \   -I $ANDROID_HOME_CURL/dalvik/libnativehelper/include   \   -I $ANDROID_HOME_CURL/frameworks/base/include   \   -I $ANDROID_HOME_CURL/frameworks/base/opengl/include   \   -I $ANDROID_HOME_CURL/frameworks/base/native/include   \   -I $ANDROID_HOME_CURL/external/skia/include   \   -I $ANDROID_HOME_CURL/out/target/product/generic/obj/include   \   -I $ANDROID_HOME_CURL/bionic/libc/arch-arm/include   \   -I $ANDROID_HOME_CURL/bionic/libc/include   \   -I $ANDROID_HOME_CURL/bionic/libstdc++/include   \   -I $ANDROID_HOME_CURL/bionic/libc/kernel/common   \   -I $ANDROID_HOME_CURL/bionic/libc/kernel/arch-arm   \   -I $ANDROID_HOME_CURL/bionic/libm/include   \   -I $ANDROID_HOME_CURL/bionic/libm/include/arch/arm   \   -I $ANDROID_HOME_CURL/bionic/libthread_db/include \   -include $ANDROID_HOME_CURL/system/core/include/arch/linux-arm/AndroidConfig.h \   -I $ANDROID_HOME_CURL/system/core/include/arch/linux-arm/ \   -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -DANDROID -DNDEBUG -DNDEBUG -DHAVE_CONFIG_H" \   CFLAGS="-fno-exceptions -Wno-multichar -msoft-float -fpic -ffunction-sections \   -funwind-tables -fstack-protector -Wa,--noexecstack -Werror=format-security \   -fno-short-enums -march=armv5te -mtune=xscale  -Wno-psabi -mthumb-interwork  \   -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith \   -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point  \   -g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once \   -fgcse-after-reload -frerun-cse-after-loop -frename-registers  -UDEBUG \   -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64   \   -Wpointer-arith -Wwrite-strings -Wunused -Winline -Wnested-externs \   -Wmissing-declarations -Wmissing-prototypes -Wno-long-long -Wfloat-equal \   -Wno-multichar -Wsign-compare -Wno-format-nonliteral -Wendif-labels \   -Wstrict-prototypes -Wdeclaration-after-statement -Wno-system-headers"  \   LIBS="-nostdlib -Bdynamic -Wl,-T,$ANDROID_HOME_CURL/build/core/armelf.x \   -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc \   -L$ANDROID_HOME_CURL/out/target/product/generic/obj/lib -Wl,-z,noexecstack \   -Wl,-rpath-link=$ANDROID_HOME_CURL/out/target/product/generic/obj/lib \   -lc -llog -lcutils -lstdc++ \   -Wl,--no-undefined $ANDROID_HOME_CURL/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0/libgcc.a  \   $ANDROID_HOME_CURL/out/target/product/generic/obj/lib/crtend_android.o \   -lm $ANDROID_HOME_CURL/out/target/product/generic/obj/lib/crtbegin_dynamic.o \   -L$ANDROID_HOME_CURL/external/curl/3rd/libs"     #!/bin/bash   ANDROID_HOME_CURL=../../ && \ NDK_HOME_CURL=../../ndk && \ PATH="$ANDROID_HOME_CURL/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin:$PATH" \ ./configure --host=arm-linux CC=arm-eabi-gcc --with-random=/dev/urandom \ CPPFLAGS="-I$NDK_HOME_CURL/platforms/android-8/arch-arm/usr/include \ -I $ANDROID_HOME_CURL/external/curl/include/  \ -I $ANDROID_HOME_CURL/external/curl/3rd/include   \ -I $ANDROID_HOME_CURL/external/curl   \ -I $ANDROID_HOME_CURL/out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates   \ -I $ANDROID_HOME_CURL/dalvik/libnativehelper/include/nativehelper   \ -I $ANDROID_HOME_CURL/system/core/include   \ -I $ANDROID_HOME_CURL/hardware/libhardware/include   \ -I $ANDROID_HOME_CURL/hardware/libhardware_legacy/include   \ -I $ANDROID_HOME_CURL/hardware/ril/include   \ -I $ANDROID_HOME_CURL/dalvik/libnativehelper/include   \ -I $ANDROID_HOME_CURL/frameworks/base/include   \ -I $ANDROID_HOME_CURL/frameworks/base/opengl/include   \ -I $ANDROID_HOME_CURL/frameworks/base/native/include   \ -I $ANDROID_HOME_CURL/external/skia/include   \ -I $ANDROID_HOME_CURL/out/target/product/generic/obj/include   \ -I $ANDROID_HOME_CURL/bionic/libc/arch-arm/include   \ -I $ANDROID_HOME_CURL/bionic/libc/include   \ -I $ANDROID_HOME_CURL/bionic/libstdc++/include   \ -I $ANDROID_HOME_CURL/bionic/libc/kernel/common   \ -I $ANDROID_HOME_CURL/bionic/libc/kernel/arch-arm   \ -I $ANDROID_HOME_CURL/bionic/libm/include   \ -I $ANDROID_HOME_CURL/bionic/libm/include/arch/arm   \ -I $ANDROID_HOME_CURL/bionic/libthread_db/include \ -include $ANDROID_HOME_CURL/system/core/include/arch/linux-arm/AndroidConfig.h \ -I $ANDROID_HOME_CURL/system/core/include/arch/linux-arm/ \ -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -DANDROID -DNDEBUG -DNDEBUG -DHAVE_CONFIG_H" \ CFLAGS="-fno-exceptions -Wno-multichar -msoft-float -fpic -ffunction-sections \ -funwind-tables -fstack-protector -Wa,--noexecstack -Werror=format-security \ -fno-short-enums -march=armv5te -mtune=xscale  -Wno-psabi -mthumb-interwork  \ -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith \ -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point  \ -g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once \ -fgcse-after-reload -frerun-cse-after-loop -frename-registers  -UDEBUG \ -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64   \ -Wpointer-arith -Wwrite-strings -Wunused -Winline -Wnested-externs \ -Wmissing-declarations -Wmissing-prototypes -Wno-long-long -Wfloat-equal \ -Wno-multichar -Wsign-compare -Wno-format-nonliteral -Wendif-labels \ -Wstrict-prototypes -Wdeclaration-after-statement -Wno-system-headers"  \ LIBS="-nostdlib -Bdynamic -Wl,-T,$ANDROID_HOME_CURL/build/core/armelf.x \ -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc \ -L$ANDROID_HOME_CURL/out/target/product/generic/obj/lib -Wl,-z,noexecstack \ -Wl,-rpath-link=$ANDROID_HOME_CURL/out/target/product/generic/obj/lib \ -lc -llog -lcutils -lstdc++ \ -Wl,--no-undefined $ANDROID_HOME_CURL/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0/libgcc.a  \ $ANDROID_HOME_CURL/out/target/product/generic/obj/lib/crtend_android.o \ -lm $ANDROID_HOME_CURL/out/target/product/generic/obj/lib/crtbegin_dynamic.o \ -L$ANDROID_HOME_CURL/external/curl/3rd/libs"               4、編寫android.mk編譯腳本         增加編譯生成libcurl.so動態庫                 [cpp] view plaincopyprint? LOCAL_PATH:= $(call my-dir)         common_CFLAGS := -Wpointer-arith -Wwrite-strings -Wunused -Winline -Wnested-externs -Wmissing-declarations -Wmissing-prototypes -Wno-long-long -Wfloat-equal -Wno-multichar -Wsign-compare -Wno-format-nonliteral -Wendif-labels -Wstrict-prototypes -Wdeclaration-after-statement -Wno-system-headers -DHAVE_CONFIG_H      #########################    # Build the libcurl library       include $(CLEAR_VARS)   include $(LOCAL_PATH)/lib/Makefile.inc   CURL_HEADERS := \       curlbuild.h \       curl.h \       curlrules.h \       curlver.h \       easy.h \       mprintf.h \       multi.h \       stdcheaders.h \       typecheck-gcc.h      LOCAL_SRC_FILES := $(addprefix lib/,$(CSOURCES))   LOCAL_C_INCLUDES += $(LOCAL_PATH)/include/   LOCAL_CFLAGS += $(common_CFLAGS)      LOCAL_COPY_HEADERS_TO := libcurl/curl   LOCAL_COPY_HEADERS := $(addprefix include/curl/,$(CURL_HEADERS))   LOCAL_PRELINK_MODULE := false   LOCAL_MODULE:= libcurl   LOCAL_MODULE_TAGS := optional      # Copy the licence to a place where Android will find it.    # Actually, this doesn't quite work because the build system searches    # for NOTICE files before it gets to this point, so it will only be seen    # on subsequent builds.    ALL_PREBUILT += $(LOCAL_PATH)/NOTICE   $(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP)       $(copy-file-to-target)      include $(BUILD_STATIC_LIBRARY)      include $(CLEAR_VARS)   include $(LOCAL_PATH)/lib/Makefile.inc   CURL_HEADERS := \       curlbuild.h \       curl.h \       curlrules.h \       curlver.h \       easy.h \       mprintf.h \       multi.h \       stdcheaders.h \       typecheck-gcc.h      LOCAL_SRC_FILES := $(addprefix lib/,$(CSOURCES))   LOCAL_C_INCLUDES += $(LOCAL_PATH)/include/   LOCAL_CFLAGS += $(common_CFLAGS)      LOCAL_COPY_HEADERS_TO := libcurl/curl   LOCAL_COPY_HEADERS := $(addprefix include/curl/,$(CURL_HEADERS))   LOCAL_PRELINK_MODULE := false   LOCAL_MODULE:= libcurl   LOCAL_MODULE_TAGS := optional      # Copy the licence to a place where Android will find it.    # Actually, this doesn't quite work because the build system searches    # for NOTICE files before it gets to this point, so it will only be seen    # on subsequent builds.    ALL_PREBUILT += $(LOCAL_PATH)/NOTICE   $(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP)       $(copy-file-to-target)      include $(BUILD_SHARED_LIBRARY)      #########################    # Build the curl binary       include $(CLEAR_VARS)   include $(LOCAL_PATH)/src/Makefile.inc   LOCAL_SRC_FILES := $(addprefix src/,$(CURL_CFILES))      LOCAL_MODULE := curl   LOCAL_MODULE_TAGS := optional   LOCAL_SHARED_LIBRARIES := libcurl   LOCAL_SYSTEM_SHARED_LIBRARIES := libc      LOCAL_C_INCLUDES += $(LOCAL_PATH)/include $(LOCAL_PATH)/lib      # This may also need to include $(CURLX_ONES) in order to correctly link    # if libcurl is changed to be built as a dynamic library    LOCAL_CFLAGS += $(common_CFLAGS)      include $(BUILD_EXECUTABLE)     LOCAL_PATH:= $(call my-dir)     common_CFLAGS := -Wpointer-arith -Wwrite-strings -Wunused -Winline -Wnested-externs -Wmissing-declarations -Wmissing-prototypes -Wno-long-long -Wfloat-equal -Wno-multichar -Wsign-compare -Wno-format-nonliteral -Wendif-labels -Wstrict-prototypes -Wdeclaration-after-statement -Wno-system-headers -DHAVE_CONFIG_H   ######################### # Build the libcurl library   include $(CLEAR_VARS) include $(LOCAL_PATH)/lib/Makefile.inc CURL_HEADERS := \ curlbuild.h \ curl.h \ curlrules.h \ curlver.h \ easy.h \ mprintf.h \ multi.h \ stdcheaders.h \ typecheck-gcc.h   LOCAL_SRC_FILES := $(addprefix lib/,$(CSOURCES)) LOCAL_C_INCLUDES += $(LOCAL_PATH)/include/ LOCAL_CFLAGS += $(common_CFLAGS)   LOCAL_COPY_HEADERS_TO := libcurl/curl LOCAL_COPY_HEADERS := $(addprefix include/curl/,$(CURL_HEADERS)) LOCAL_PRELINK_MODULE := false LOCAL_MODULE:= libcurl LOCAL_MODULE_TAGS := optional   # Copy the licence to a place where Android will find it. # Actually, this doesn't quite work because the build system searches # for NOTICE files before it gets to this point, so it will only be seen # on subsequent builds. ALL_PREBUILT += $(LOCAL_PATH)/NOTICE $(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP) $(copy-file-to-target)   include $(BUILD_STATIC_LIBRARY)   include $(CLEAR_VARS) include $(LOCAL_PATH)/lib/Makefile.inc CURL_HEADERS := \ curlbuild.h \ curl.h \ curlrules.h \ curlver.h \ easy.h \ mprintf.h \ multi.h \ stdcheaders.h \ typecheck-gcc.h   LOCAL_SRC_FILES := $(addprefix lib/,$(CSOURCES)) LOCAL_C_INCLUDES += $(LOCAL_PATH)/include/ LOCAL_CFLAGS += $(common_CFLAGS)   LOCAL_COPY_HEADERS_TO := libcurl/curl LOCAL_COPY_HEADERS := $(addprefix include/curl/,$(CURL_HEADERS)) LOCAL_PRELINK_MODULE := false LOCAL_MODULE:= libcurl LOCAL_MODULE_TAGS := optional   # Copy the licence to a place where Android will find it. # Actually, this doesn't quite work because the build system searches # for NOTICE files before it gets to this point, so it will only be seen # on subsequent builds. ALL_PREBUILT += $(LOCAL_PATH)/NOTICE $(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP) $(copy-file-to-target)   include $(BUILD_SHARED_LIBRARY)   ######################### # Build the curl binary   include $(CLEAR_VARS) include $(LOCAL_PATH)/src/Makefile.inc LOCAL_SRC_FILES := $(addprefix src/,$(CURL_CFILES))   LOCAL_MODULE := curl LOCAL_MODULE_TAGS := optional LOCAL_SHARED_LIBRARIES := libcurl LOCAL_SYSTEM_SHARED_LIBRARIES := libc   LOCAL_C_INCLUDES += $(LOCAL_PATH)/include $(LOCAL_PATH)/lib   # This may also need to include $(CURLX_ONES) in order to correctly link # if libcurl is changed to be built as a dynamic library LOCAL_CFLAGS += $(common_CFLAGS)   include $(BUILD_EXECUTABLE)編譯過程如果沒有任何錯誤的話將會在/system/lib下生成 libcurl.so 及 /system/bin下面 curl 可執行文件         2、測試接口功能     #include "curl/curl.h"   #include <stdio.h>;   int main() {        CURL *curl;        CURLcode res;           curl_global_init(CURL_GLOBAL_ALL);           curl = curl_easy_init();       if (curl) {                curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com/");             res = curl_easy_perform(curl);            if (0!=res) {                     printf("curl error: %d\n", res);                    }                       curl_easy_cleanup(curl);            }              curl_global_cleanup();       return 0;   }       編譯一個相應的android.mk生成可執行文件:     LOCAL_PATH := $(call my-dir)   include $(CLEAR_VARS)    LOCAL_C_INCLUDES += \       $(TOP)/external/curl/include/ \    LOCAL_SRC_FILES:= curl_test.cpp   LOCAL_SHARED_LIBRARIES := libcurl   LOCAL_MODULE_TAGS := optional   LOCAL_MODULE := curl_test   include $(BUILD_EXECUTABLE)   即可進行測試的,輸出的內容就是打開 http://www.baidu.com/ 頁面的源代碼         3、編譯問題         請根據具體android版本及路徑修改 curl.sh 文件並重新執行即可,暫時沒碰到其它的問題。  
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved