解析libcurl在android下的移植、编译与测试

时间:2023-01-24 01:19:44 

由于项目需要在NDK中使用网络开发,对于c语言网络开发来说,libcurl库是个很不错的选择,但android系统中并没有自带该库,所以就得自己移植了。
下面是移植步骤:
1.  下载curl源码
我这里下载的是curl-7.22.0,源码下载地址为:http://curl.haxx.se/download.html

2. 准备android源码编译环境,
android源码应已全部编译过,具体细节这里不详述,我这里使用的是android2.2 froyo源码树。

3.  在android中编译curl
在最新的curl源码里其实已经带有Android.mk这个编译文件了,而且在这文件的开头注释部分比较详细地介绍编译方法。
1)拷贝curl源码至android源码树下的external/curl
2)cd 到 external/curl目录下,输入(红色字部分根据自己的环境做相应的更改):


ANDROID_HOME=/home/braincol/workspace/android/froyo && \
NDK_HOME=/home/braincol/workspace/android/froyo/ndk && \
PATH="$ANDROID_HOME/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/platforms/android-8/arch-arm/usr/include \
-I $ANDROID_HOME/external/curl/include/  \
-I $ANDROID_HOME/external/curl/3rd/include   \
-I $ANDROID_HOME/external/curl   \
-I $ANDROID_HOME/out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates   \
-I $ANDROID_HOME/dalvik/libnativehelper/include/nativehelper   \
-I $ANDROID_HOME/system/core/include   \
-I $ANDROID_HOME/hardware/libhardware/include   \
-I $ANDROID_HOME/hardware/libhardware_legacy/include   \
-I $ANDROID_HOME/hardware/ril/include   \
-I $ANDROID_HOME/dalvik/libnativehelper/include   \
-I $ANDROID_HOME/frameworks/base/include   \
-I $ANDROID_HOME/frameworks/base/opengl/include   \
-I $ANDROID_HOME/frameworks/base/native/include   \
-I $ANDROID_HOME/external/skia/include   \
-I $ANDROID_HOME/out/target/product/generic/obj/include   \
-I $ANDROID_HOME/bionic/libc/arch-arm/include   \
-I $ANDROID_HOME/bionic/libc/include   \
-I $ANDROID_HOME/bionic/libstdc++/include   \
-I $ANDROID_HOME/bionic/libc/kernel/common   \
-I $ANDROID_HOME/bionic/libc/kernel/arch-arm   \
-I $ANDROID_HOME/bionic/libm/include   \
-I $ANDROID_HOME/bionic/libm/include/arch/arm   \
-I $ANDROID_HOME/bionic/libthread_db/include \
-include $ANDROID_HOME/system/core/include/arch/linux-arm/AndroidConfig.h \
-I $ANDROID_HOME/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/build/core/armelf.x \
-Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc \
-L$ANDROID_HOME/out/target/product/generic/obj/lib -Wl,-z,noexecstack \
-Wl,-rpath-link=$ANDROID_HOME/out/target/product/generic/obj/lib \
-lc -llog -lcutils -lstdc++ \
-Wl,--no-undefined $ANDROID_HOME/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0/libgcc.a  \
$ANDROID_HOME/out/target/product/generic/obj/lib/crtend_android.o \
-lm $ANDROID_HOME/out/target/product/generic/obj/lib/crtbegin_dynamic.o \
-L$ANDROID_HOME/external/curl/3rd/libs"


如果$ANDROID_HOME目录下没有ndk的开发包,那么到google的官网上下载一个放进去就行了。

3)cd 到源码根目录 mmm extern/libcurl:
编译完成之后,会生成静态库:out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates/libcurl.a  。

4)如果要生成动态库需要修改curl下的Android.mk :


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 $(BUILD_SHARED_LIBRARY)


4.  在android中测试curl
1)在android froyo源码树中下建立一个mytest目录,该目录下再建立一个curltest目录。
2)在目录curtest下创建curl-test.cpp:


#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.cnblogs.com/hibraincol/");
        res
 = curl_easy_perform(curl);         if
(0!=res) {                  printf("curl
 error: %d\n",
 res);                 }                 curl_easy_cleanup(curl);   
    } 
     curl_global_cleanup();    return
0;}


3)在目录curtest下创建Android.mk:


#
 curl test executable#LOCAL_PATH
 := $(call my-dir)include
 $(CLEAR_VARS) LOCAL_C_INCLUDES
 += \    $(LOCAL_PATH)/3rd/include LOCAL_SRC_FILES:=
 curl-test.cpp #
 No shared libraries.LOCAL_SHARED_LIBRARIES
 := #
 No static libraries.LOCAL_STATIC_LIBRARIES
 := libcurl LOCAL_MODULE
 := curl-testinclude
 $(BUILD_EXECUTABLE)


4)把libcurl的头文件拷贝到curtest目录下的3rd/include目录下:
cp -rf out/target/product/generic/obj/include/libcurl/curl  mytest/curltest/3rd/include
5)到android源码树的根目录下:mmm /mytest/curltest/


braincol@ubuntu:~/workspace/android/froyo$ mmm mytest/curltest/
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=2.2
TARGET_PRODUCT=generic
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=MASTER
============================================
make: Entering directory `/home/braincol/workspace/android/froyo'
target thumb C++: curl-test <= mytest/curltest/curl-test.cpp
mytest/curltest/curl-test.cpp:2:19: warning: extra tokens at end of #include directive
target Executable: curl-test (out/target/product/generic/obj/EXECUTABLES/curl-test_intermediates/LINKED/curl-test)
target Non-prelinked: curl-test (out/target/product/generic/symbols/system/bin/curl-test)
target Strip: curl-test (out/target/product/generic/obj/EXECUTABLES/curl-test_intermediates/curl-test)
Install: out/target/product/generic/system/bin/curl-test
make: Leaving directory `/home/braincol/workspace/android/froyo'


可以看到在out/target/product/generic/system/bin/下生成了curl-test这个测试程序。
6)将curl-test拷贝到真机或者模拟器中运行。
  a.我这里是在android真机的根目录下建立了一个test目录.
  b.然后通过adb push将curl-test拷贝到该目录下,并更改curl-test为可执行权限:chmod 777 curl-test.
  c. adb shell 进入shell控制台,然后cd到test目录下, ./curl-test  执行,可以看到打印出的网页源码,移植成功。
 
这样在之后的android app开发中,如果需要用到libcurl的库,就可以直接out/target/product/generic/obj/include/libcurl/curl里的头文件和out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates/libcurl.a拿到app工程中去用就行了。

标签:移植,编译,测试
0
投稿

猜你喜欢

  • javaWeb 四大域对象详细介绍

    2021-08-16 14:04:01
  • Java 超详细讲解IO操作字节流与字符流

    2022-04-28 20:09:57
  • 基于ReentrantLock的实现原理讲解

    2023-11-23 22:43:23
  • springboot v2.0.3版本多数据源配置方法

    2023-07-27 06:37:57
  • 浅谈Java中Collections.sort对List排序的两种方法

    2021-11-18 11:20:22
  • Java源码解析之详解ImmutableMap

    2023-11-23 08:06:07
  • fastjson生成json时Null属性不显示的解决方法

    2022-08-17 15:56:59
  • Android Tabhost使用方法详解

    2023-10-02 12:46:46
  • 详解Android开发数据持久化之文件存储(附源码)

    2022-06-19 20:30:33
  • Java 8 中 Function 接口使用方法介绍

    2022-12-08 23:54:39
  • Android item长按删除功能

    2022-11-13 03:45:24
  • Android详解之NoHttp最基本使用(无封装)

    2022-09-28 17:11:51
  • Springboot WebFlux集成Spring Security实现JWT认证的示例

    2021-06-02 03:24:50
  • c#扫描图片去黑边(扫描仪去黑边)

    2022-11-06 11:45:55
  • Android开发导入项目报错Ignoring InnerClasses attribute for an anonymous inner class的解决办法

    2023-05-16 13:02:45
  • Java数据结构学习之二叉树

    2023-04-25 12:08:27
  • Android解决所有双击优化的问题

    2023-07-27 09:07:03
  • Dubbo实现分布式日志链路追踪

    2023-08-23 21:00:54
  • 聊一聊jdk1.8中的ArrayList 底层数组是如何扩容的

    2023-11-16 08:55:50
  • spring boot metrics监控指标使用教程

    2022-01-10 08:42:16
  • asp之家 软件编程 m.aspxhome.com