一.移植Curl工具到Android环境步骤
1.修改cURL源码下的mk文件。源码下面的Android.mk文件最后生成的是静态库libcurl.a,做如下修改(编译成动态库)。
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE:= libcurl
LOCAL_MODULE_TAGS := optional
ALL_PREBUILT += $(LOCAL_PATH)/NOTICE
$(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP)
$(copy-file-to-target)
include $(BUILD_SHARED_LIBRARY)
2.配置编译环境(cd 到Android.mk同一目录,直接在控制台输入下列代码或者把下面代码弄成sh脚本执行)红色部分根据自己源码情况
ANDROID_HOME=/home/zhoulc/android/ && \
NDK_HOME=/home/zhoulc/android/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"
3.编译libcurl.so库
cd进入android/external/curl源码目录
mm-》编译生成libcurl.so库
4.编写测试case 以及Android.mk文件并生成可执行文件
新建一个测试案例curl_test.cpp
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文件生成curl_test可执行文件
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)
生成可执行文件:curl_test
4.运行查看测试结果
运行测试case:curl_test
5.(补充)移植libcurl到android4.0,修改两个地方
1)把生成的路径改一下,一般默认为out/target/product/generic下面,我们根据系统不同(根据lunch选择不同,最终生成的路径不一样)改为系统的全局变量,
把$ANDROID_HOME/out/target/product/generic替换成$ANDROID_PRODUCT_OUT。
ANDROID_HOME_CURL=../../ && \
NDK_HOME_CURL=../../prebuilt/ndk && \
PATH="$ANDROID_HOME_CURL/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin:$PATH" \
./configure --host=arm-linux CC=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_PRODUCT_OUT/obj/lib -Wl,-z,noexecstack \
-Wl,-rpath-link=$ANDROID_PRODUCT_OUT/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_PRODUCT_OUT/obj/lib/crtend_android.o \
-lm $ANDROID_PRODUCT_OUT/obj/lib/crtbegin_dynamic.o \
-L$ANDROID_HOME_CURL/external/curl/3rd/libs"
2)修改Android.mk
把关于ALL_PREBUILT模块全部注释调
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。