Since the project needs to use network development in the NDK, the libcurl library is a good choice for C language network development, but the library does not come with it in the Android system, so you have to port it yourself.
Here are the transplant steps:
1. Download the curl source code
What I downloaded here is curl-7.22.0, and the source code download address is:/
2. Prepare the Android source code compilation environment,
The Android source code should have been fully compiled. The specific details are not explained here. I am using the Android2.2 froyo source code tree here.
3. Compile curl in android
The latest curl source code actually contains this compilation file, and the compilation method is introduced in more detail at the comment section at the beginning of this file.
1) Copy the curl source code to external/curl under the Android source code tree
2) cd to external/curl directory and enter (The red character section makes corresponding changes according to its own environment):
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/ \
-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/ \
-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/ \
$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"
If there is no ndk development package in the $ANDROID_HOME directory, then download one on Google's official website and put it in.
3) cd to the source code root directory mmmm extern/libcurl:
After compilation is completed, a static library will be generated: out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates/ .
4) If you want to generate a dynamic library, you need to modify the curl:
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. Test curl in android
1) Create a mytest directory in the Android froyo source tree, and then create a curlttest directory under this directory.
2) Create in the directory curtest:
#include
"curl/"#include
<>;int
main() { CURL
*curl; CURLcode
res; curl_global_init(CURL_GLOBAL_ALL); curl
= curl_easy_init(); if
(curl) { curl_easy_setopt(curl,
CURLOPT_URL, "/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) Create in the directory curtest:
#
curl test executable#LOCAL_PATH
:= $(call my-dir)include
$(CLEAR_VARS) LOCAL_C_INCLUDES
+= \ $(LOCAL_PATH)/3rd/include LOCAL_SRC_FILES:=
#
No shared libraries.LOCAL_SHARED_LIBRARIES
:= #
No static libraries.LOCAL_STATIC_LIBRARIES
:= libcurl LOCAL_MODULE
:= curl-testinclude
$(BUILD_EXECUTABLE)
4) Copy the header file of libcurl to the 3rd/include directory under the curtest directory:
cp -rf out/target/product/generic/obj/include/libcurl/curl mytest/curltest/3rd/include
5) Go to the root directory of the Android source tree: mmmm /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/
mytest/curltest/: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'
You can see that the curl-test test program is generated under out/target/product/generic/system/bin/.
6) Copy curl-test to a real machine or emulator and run it.
a. I have created a test directory under the root directory of the Android real machine.
b. Then copy curl-test to this directory through adb push and change curl-test to executable permission: chmod 777 curl-test.
c. adb shell enters the shell console, then cd to the test directory, execute ./curl-test , and you can see the printed web page source code and the porting is successful.
In this way, in the subsequent android app development, if you need to use the libcurl library, you can directly use the header files in out/target/product/generic/obj/include/libcurl/curl and out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates/ into the app project to use it.