Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

编译libcurl到Android平台 #22

Open
chenpengcong opened this issue Sep 16, 2018 · 0 comments
Open

编译libcurl到Android平台 #22

chenpengcong opened this issue Sep 16, 2018 · 0 comments

Comments

@chenpengcong
Copy link
Owner

chenpengcong commented Sep 16, 2018

本文介绍如何编译libcurl到Android平台并提供测试Demo工程

首先,使用NDK工具中的make_standalone_toolchain.py生成一套指定处理器架构的编译工具链

$ /opt/android-ndk-r14b/build/tools/make_standalone_toolchain.py --arch arm --api 21 --install-dir=/tmp/my-android-toolchain

--arch选项指定目标机器的处理器架构,--api选项指定android api版本,不同android api版本对native api的支持有所差异,具体android api版本提供了哪些native api参考这里Native APIs,同时也需要看我们使用的指定版本的ndk工具支持哪些api,具体在ndk工具的platforms目录下查看

这里我指定32位arm架构,android api 21

然后获取curl源码,这里我使用的curl是curl7.61.0

设置环境变量

# Add the standalone toolchain to the search path.
export PATH=$PATH:/tmp/my-android-toolchain/bin

# Tell configure what tools to use.
target_host=arm-linux-androideabi
export AR=$target_host-ar
export AS=$target_host-clang
export CC=$target_host-clang
export CXX=$target_host-clang++
export LD=$target_host-ld
export STRIP=$target_host-strip

# Tell configure what flags Android requires.
export CFLAGS="-fPIE -fPIC"
export LDFLAGS="-pie"

进入libcurl源码目录,执行

$ ./configure --prefix=/home/cike/Desktop/test_curl --host=arm-linux-androideabi
$ make
$ make install

Tip: libcurl默认是enable zlib的,但是我们并没有先交叉编译出zlib,原因是android api 21支持zlib库,所以我们生成的工具链已经包含了zlib了,可以在my-android-toolchain/sysroot下找到zlib的库和头文件

最后将libcurl.so作为prebuilt library集成到我们的Android项目中,如何集成参考Using Prebuilt Libraries,或者参考我的curl_demo

注意:使用libcurl记得在androidmanifest.xml配置网络权限<uses-permission android:name="android.permission.INTERNET"/>,否则会报错,而且报错信息并不提示没权限,一开始我也是找不到问题所在,折腾了会才想起是否是没有申请权限问题,验证了下果真是没申请网络权限导致。

References:
Standalone Toolchains
Android NDK Native APIs
Using Prebuilt Libraries

@chenpengcong chenpengcong changed the title 编译libcurl到Android平台.md 编译libcurl到Android平台 Feb 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant