Analysis of Libcurl porting, compiling and testing in Android _android

Source: Internet
Author: User
Tags curl

Because the project needs to use the network development in the NDK, for C language network development, the Libcurl library is a very good choice, but the Android system does not own this library, therefore has to transplant itself.
The following is a migration step:
1. Download Curl Source
I download here is curl-7.22.0, the source code download address: http://curl.haxx.se/download.html

2. Prepare the Android source compilation environment,
Android source should have been all compiled, the details are not detailed here, I use here is the android2.2 Froyo source tree.

3. Compile Curl in Android
In the latest Curl source code in fact already has android.mk this compile file, and in this document the beginning Annotation section describes the compilation method in more detail.
1 Copy Curl Source to the External/curl under the Android source tree
2 CD to the External/curl directory, input ( red Word part according to their own environment to make the corresponding changes ):

Copy Code code as follows:

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 "

If the $android_home directory does not have a NDK development package, then go to Google's official web download one put it on the line.

3 CD to source root directory mmm Extern/libcurl:
After the compilation completes, a static library is generated: OUT/TARGET/PRODUCT/GENERIC/OBJ/STATIC_LIBRARIES/LIBCURL_INTERMEDIATES/LIBCURL.A.

4 If you want to generate a dynamic library you need to modify the android.mk under Curl:
Copy Code code as follows:

Local_prelink_module: = False
local_module:= Libcurl
Local_module_tags: = Optional
# Copy the licence to a place where Android would find it.
# Actually, this doesn ' t quite work because of the build system searches
# for NOTICE files before it gets to this point and so it'll 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 in the Android Froyo source tree to create a mytest directory, the directory to build a curltest directory.
2 Create curl-test.cpp under directory Curtest:
Copy Code code as follows:

#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 Create android.mk under directory Curtest:
Copy Code code as follows:

#
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 Copy the Libcurl header file to the 3rd/include directory under the Curtest directory:
CP-RF Out/target/product/generic/obj/include/libcurl/curl Mytest/curltest/3rd/include
5 to the root directory of the Android source tree: mmm/mytest/curltest/
Copy Code code as follows:

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 the 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 was generated under out/target/product/generic/system/bin/.
6 The Curl-test copy to the real machine or simulator to run.
A. I set up a test directory in the root directory of the android real machine.
B. Then copy the curl-test to the directory via ADB push and change curl-test to executable permissions: chmod 777 curl-test.
C. The ADB shell enters the shell console, and then the CD to the test directory,./curl-test execution, you can see the printed page source code, transplant successfully.

So in the later Android app development, if you need to use the Libcurl library, you can directly out/target/product/generic/obj/include/libcurl/curl the header files and out/target/ PRODUCT/GENERIC/OBJ/STATIC_LIBRARIES/LIBCURL_INTERMEDIATES/LIBCURL.A get the app project and use it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.