Porting, compiling, and testing libcurl in Android

Source: Internet
Author: User

Because the project needs to use network development in ndk,C LanguageFor network development, the libcurl library is a good choice, but the library is not included in the Android system, so you have to port it yourself.

The following is a migration procedure:

1. Download the curl source code
Here I download curl-7.22.0, source code: http://curl.haxx.se/download.html

2. Prepare the android Source Code compiling environment,

The android source code has been fully compiled. The details are not detailed here. Here I use the android2.2 froyo source code tree.

 

3. Compile curl in Android
In the latest curl source code, the android. mk compilation file is included, and the compilation method is described in detail in the comments section at the beginning of the file.

1) copy the curl source code to the external/curl under the android source code tree.

 

2) CD to the external/curl directory, enter (the red part changes accordingly based on your 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/androidconfig. h \

-I $ android_home/system/CORE/include/ARCH/Linux-arm /\

-D1_arm_arch_5 _-d1_arm_arch_5t _-d1_arm_arch_5e _-d1_arm_arch_5te _-dandroid-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-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. \

$ 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 contain an ndk Development Kit, you can download it from the Google official website.

 

3) CD to the source code root directory Mmm extern/libcurl:

After compilation, a static library is generated: Out/target/product/generic/obj/static_libraries/libcurl_intermediates/libcurl..

 

4) To generate a dynamic library, modify Android. mk in 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 source code tree of Android froyo, and create a curltest directory under this directory.

 

2) create a curl-test.cpp under the directory curtest:

# 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 In the curtest directory:

 
# Curl test executable # local_path: = $ (call my-DIR) include $ (clear_vars) local_c_includes + =\$ (local_path)/3rd/includelocal_src_files: = curl-test.cpp # No shared libraries. local_shared_libraries: = # No static libraries. local_static_libraries: = libcurllocal_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 code tree: Mmm/mytest/curltest/

Braincol @ Ubuntu :~ /Workspace/Android/froyo $ Mmm mytest/curltest/

========================================================== ====

Platform_version_codename = REL

Platform_versions = 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'



The curl-test is generated under out/target/product/generic/system/bin /.Program.


6) Copy curl-test to a real machine or simulator to run the command.

A. I have created a test directory under the root directory of the android real machine.

B. Then copy curl-test to the directory through ADB push, and change curl-test to execute: chmod 777 curl-test.

C. ADB shell enter the shell console, and then CD to the test directory, run./curl-test, you can see the printed web page source code, transplanted successfully.

 

In this way, in subsequent Android app development, if you need to use the libcurl library, the header file in out/target/product/generic/obj/include/libcurl/curl and out/target/product/generic/obj/static_libraries/libcurl_intermediates/libcurl can be directly used. A can be used in the app project.

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.