Compile libcurl 7.37.0 with Android 4.4 or other versions

Kanglai Qian 8月 31, 2014
• 在其它设备中阅读本文章

Recently I need compile libcurl for different platforms, since cocos-2dx 3.0 ships with libcurl 7.26.0 and there are several fixes in newer versions according to changelog. There are several blogs describing how to build libcurl with android source, such as porting-of-libcurl-to-android-os-using, How_to_compile_libcurl and Android ndk下编译libcurl. However, they are all out dated, especially android source has changed a lot!

After struggling a whole weekend, I finally compiled libcurl 7.37.0 with android 4.4 source. The steps described in packages/Android/Android.mk skip many details. I’ll explain every steps and hope you can make it. If you use the similar version, you could just copy & paste mine.

Build Environment (Android 4.4)

Follow instructions in Download and Building and build android first. If you encounter connecting problems in China, you may refer to 如何在国内下载android源代码. My android source folder is put at /home/anthony/android.

Generate LIBS

In order to determine which libs are needed, we first make an execuable library to see arguments passed in. Run make dhcpcd showcommands at /home/anthony/android, and we will get this in terminal output:

This indicates the link step and we can find libraries like -lc -lcutils -lm -lnetutils -lstdc++ and libatomic.a libgcc.a crtbegin_dynamic.o crtend_android.o.

Put libcurl into android source

Extract libcurl into android/external and rename it from curl-7.37.0 to curl. Now we need to fix two places in packages/Android/Android.mk:

• Add in line 73: LOCAL_C_INCLUDES += $(LOCAL_PATH)/include/$(LOCAL_PATH)/lib/
• Comment line 86: #ALL_PREBUILT += \$(LOCAL_PATH)/NOTICE

Generate CPPFLAGS and CFLAGS

Similar to the previous step, run make libcurl showcommands at /home/anthony/android and we will fail with the following output (Remember we haven’t ./configure yet):

We should copy -I -isystem and flags like -W -D -M into the following step.

Build.sh

Now we have enough parameters needed to pass in, so we can write a script called build.sh. Follow Dan Fandrich’s instructions: putting the -I, -isystem and -D options into CPPFLAGS, putting the -W, -m, -f, -O and -nostdlib options into CFLAGS, and putting the -Wl, -L and -l options into LIBS, along with the path to the files libgcc.a, crtbegin_dynamic.o, and ccrtend_android.o. Remember that the paths must be absolute since you will not be running configure from the same directory as the Android make. The normal cross-compiler options must also be set. Note that the -c, -o, -MD and similar flags must not be set.

Here is my script, with parameters from the above steps.

Configure and Make

Run the script to configure by ./build.sh and make libcurl from android source root. You will find what you want in out/target/product/generic/obj:

Downgrade to lower Android version

If you wish to compile with lower version, the first thing you need is repo init -b xxx and repo sync to get a different android source.

Attention: you should use the corresponding APP_PLATFORM in NDK when building apps, and don’t forget android:minSdkVersion!

Android 2.3.3

You may need downgrade your gcc and do as the above.

bionic ERROR

This is not specified to libcurl itself. This happens for builds with higher version Android like the following errors:

You should disable _FORTIFY_SOURCE in the build.sh! These checks are implemented in Android source rather than NDK so you will get these errors.

Solution First of all, check your build.sh(or other scrips) and ensure D_FORTIFY_SOURCE=0. Second, add -D_FORTIFY_SOURCE=0 into LOCAL_CFLAGS in Android.mk.

You could check your libraries with nm:

The new library with the macro does not have these chk any more!