本章主要讲述 curl 编译支持https
1:准备
curl7.85.0 https://curl.se/download.html
openssl-3.0.5.tar.gz https://www.openssl.org
zlib1.2.12 https://zlib.net/
ubuntu18 (ubuntu16都可以) 这里是用的是虚拟机里安装的
android-ndk-r21e https://developer.android.google.cn/ndk/downloads/
目录结构如下
curl curl安装目录
out openssl安装目录
zlib zlib 安装目录
2:编译openssl
1> 写编译脚本
./Configure --help 查看说明
写了个shell脚本,直接上脚本(部分参考别人写的),
NOTES-ANDROID.md 画红框这段,官方编译android说明
func_build(){
cd $HOME/ndk/openssl-3.0.5
rm -rf $HOME/ndk/out/output-$ARCH
mkdir $HOME/ndk/out/output-$ARCH
export ANDROID_NDK_ROOT=$HOME/ndk/android-ndk-r21e
#export PATH=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH
#export PATH=$ANDROID_NDK_ROOT/toolchains/$TOOL_CHAIN/prebuilt/linux-x86_64/bin:$PATH
export PATH=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin:$ANDROID_NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH
#./Configure android-$ARCH_NAME -D__ANDROID_API__=23 --prefix=$HOME/ndk/out/output-$ARCH -fPIC
./Configure $ARCH_NAME -D__ANDROID_API__=23 --prefix=$HOME/ndk/out/output-$ARCH -fPIC
make clean
make && make install
}
#export ANDROID_NDK_ROOT=$HOME/ndk/android-ndk-r21e
#export PATH=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin:$ANDROID_NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH
#`android-arm`, `android-arm64`, `android-x86` and `android-x86_64` , `android-mips`,`android-mip64`,
#armeabi-v7a armeabi arm64-v8a x86 x86_64 mips mips64
for ARCH in armeabi-v7a arm64-v8a x86 x86_64
do
echo $ARCH
if [ "$ARCH" = "armeabi-v7a" ]; then
ARCH_NAME=android-arm
TOOL_CHAIN=arm-linux-androideabi-4.9
fi
if [ "$ARCH" = "arm64-v8a" ]; then
ARCH_NAME=android-arm64
TOOL_CHAIN=aarch64-linux-android-4.9
fi
if [ "$ARCH" = "x86" ]; then
ARCH_NAME=android-x86
TOOL_CHAIN=x86-4.9
fi
if [ "$ARCH" = "x86_64" ]; then
ARCH_NAME=android-x86_64
TOOL_CHAIN=x86_64-4.9
fi
func_build
done
2>make &&make install
生成so a 文件 eg:arm64-v8a
3:编译zlib
感觉这个用处不大
.全脚本编译没完成,先手动编译了(这里以x86为例)
1>
./configure --prefix=$HOME/ndk/zlib/out-x86
2> 修改生成的 Makefile 文件
3> make && make install
4:编译curl
参考官方编译说明
上编译脚本 android_curl_build.sh 可以间化的(有时间再优化下,看起冗长的)
export ANDROID_NDK_ROOT=$HOME/ndk/android-ndk-r21e
export NDK=$HOME/ndk/android-ndk-r21e
export HOST_TAG=linux-x86_64
export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/$HOST_TAG
#armeabi-v7a arm64-v8a x86 x86_64
for ARCH in armeabi-v7a arm64-v8a x86 x86_64
do
if [ "$ARCH" = "armeabi-v7a" ]; then
ARCH_NAME=android-arm
rm -rf $HOME/ndk/curl/output-$ARCH
mkdir $HOME/ndk/curl/output-$ARCH
#TOOL_CHAIN=arm-linux-androideabi-4.9
export AR=$TOOLCHAIN/bin/arm-linux-androideabi-ar
export AS=$TOOLCHAIN/bin/arm-linux-androideabi-as
export CC=$TOOLCHAIN/bin/armv7a-linux-androideabi23-clang
export CXX=$TOOLCHAIN/bin/armv7a-linux-androideabi23-clang++
export LD=$TOOLCHAIN/bin/arm-linux-androideabi-ld
export RANLIB=$TOOLCHAIN/bin/arm-linux-androideabi-ranlib
export STRIP=$TOOLCHAIN/bin/arm-linux-androideabi-strip
#--disable-static --enable-shared #share only
#--enable-static --enable-shared # static share
./configure --host arm-linux-androideabi --enable-static --enable-shared --without-libidn --without-librtmp --without-gnutls --without-nss --without-libssh2 --without-winidn --disable-rtsp --disable-ldap --disable-ldaps --disable-ipv6 --with-openssl=$HOME/ndk/out/output-$ARCH --with-zlib=$HOME/ndk/zlib/out-$ARCH --prefix=$HOME/ndk/curl/output-$ARCH
fi
if [ "$ARCH" = "arm64-v8a" ]; then
ARCH_NAME=android-arm64
#TOOL_CHAIN=aarch64-linux-android-4.9
rm -rf $HOME/ndk/curl/output-$ARCH
mkdir $HOME/ndk/curl/output-$ARCH
export AR=$TOOLCHAIN/bin/aarch64-linux-android-ar
export AS=$TOOLCHAIN/bin/aarch64-linux-android-as
export CC=$TOOLCHAIN/bin/aarch64-linux-android23-clang
export CXX=$TOOLCHAIN/bin/aarch64-linux-android23-clang++
export LD=$TOOLCHAIN/bin/aarch64-linux-android-ld
export RANLIB=$TOOLCHAIN/bin/aarch64-linux-android-ranlib
export STRIP=$TOOLCHAIN/bin/aarch64-linux-android-strip
./configure --host aarch64-linux-android --enable-static --enable-shared --without-libidn --without-librtmp --without-gnutls --without-nss --without-libssh2 --without-winidn --disable-rtsp --disable-ldap --disable-ldaps --disable-ipv6 --with-openssl=$HOME/ndk/out/output-$ARCH --with-zlib=$HOME/ndk/zlib/out-$ARCH --prefix=$HOME/ndk/curl/output-$ARCH
fi
if [ "$ARCH" = "x86" ]; then
ARCH_NAME=android-x86
#TOOL_CHAIN=x86-4.9
rm -rf $HOME/ndk/curl/output-$ARCH
mkdir $HOME/ndk/curl/output-$ARCH
export AR=$TOOLCHAIN/bin/i686-linux-android-ar
export AS=$TOOLCHAIN/bin/i686-linux-android-as
export CC=$TOOLCHAIN/bin/i686-linux-android23-clang
export CXX=$TOOLCHAIN/bin/i686-linux-android23-clang++
export LD=$TOOLCHAIN/bin/i686-linux-android-ld
export RANLIB=$TOOLCHAIN/bin/i686-linux-android-ranlib
export STRIP=$TOOLCHAIN/bin/i686-linux-android-strip
./configure --host i686-linux-android --enable-static --enable-shared --without-libidn --without-librtmp --without-gnutls --without-nss --without-libssh2 --without-winidn --disable-rtsp --disable-ldap --disable-ldaps --disable-ipv6 --with-openssl=$HOME/ndk/out/output-$ARCH --with-zlib=$HOME/ndk/zlib/out-$ARCH --prefix=$HOME/ndk/curl/output-$ARCH
fi
if [ "$ARCH" = "x86_64" ]; then
ARCH_NAME=android-x86_64
#TOOL_CHAIN=x86_64-4.9
rm -rf $HOME/ndk/curl/output-$ARCH
mkdir $HOME/ndk/curl/output-$ARCH
export AR=$TOOLCHAIN/bin/x86_64-linux-android-ar
export AS=$TOOLCHAIN/bin/x86_64-linux-android-as
export CC=$TOOLCHAIN/bin/x86_64-linux-android23-clang
export CXX=$TOOLCHAIN/bin/x86_64-linux-android23-clang++
export LD=$TOOLCHAIN/bin/x86_64-linux-android-ld
export RANLIB=$TOOLCHAIN/bin/x86_64-linux-android-ranlib
export STRIP=$TOOLCHAIN/bin/x86_64-linux-android-strip
./configure --host x86_64-linux-android --enable-static --enable-shared --without-libidn --without-librtmp --without-gnutls --without-nss --without-libssh2 --without-winidn --disable-rtsp --disable-ldap --disable-ldaps --disable-ipv6 --with-openssl=$HOME/ndk/out/output-$ARCH --with-zlib=$HOME/ndk/zlib/out-$ARCH --prefix=$HOME/ndk/curl/output-$ARCH
fi
make clean
make && make install
done
5:写android 工程测试
参考android curl.so使用(二)
com_example_testcurl_JNIUtils.cpp 内容如下
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "curl/curl.h"
#include <string>
using namespace std ;
#include "com_example_testcurl_JNIUtils.h"
#include <android/log.h>
#define TAG "CURLLIB"
#define LOGI(FORMAT,...) __android_log_print(ANDROID_LOG_INFO, TAG, FORMAT, ##__VA_ARGS__);
#define LOGE(FORMAT,...) __android_log_print(ANDROID_LOG_ERROR, TAG, FORMAT, ##__VA_ARGS__);
size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
((std::string*)userdata)->append(ptr, nmemb);
return nmemb;
}
JNIEXPORT jstring JNICALL Java_com_example_testcurl_JNIUtils_curlhttpget
(JNIEnv *env, jclass jc, jstring js){
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL* curl = curl_easy_init();
std::string response_data;
if (curl)
{
jboolean b = JNI_TRUE ;
const char *nativeString = (const char *)env->GetStringUTFChars(js, &b);
curl_easy_setopt(curl, CURLOPT_URL, nativeString);
// no certificate, not verify server certificate
// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, true);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_data);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); //是从请求开始到响应总共等待的时间
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2); 是响应等待的时间,后面的数字是等待的秒数
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
// long nRes = curl_easy_strerror(res) ;
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
// char szbuf[1024]={0,};
// sprintf(szbuf,"%s",curl_easy_strerror(res));
// response_data = szbuf ;
}
else
{
// response_data ="[0]";
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
printf("response code %d \n", response_code);
printf("response data : \n ");
printf("%s",response_data.c_str());
LOGI("[res]%ld-%s",response_code,response_data.c_str())
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return env->NewStringUTF(response_data.c_str());
}
Android.mk 如下
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := crypto
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := ssl
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libssl.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := z
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libz.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := curl
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libcurl.so
include $(PREBUILT_SHARED_LIBRARY)
#LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libcurl.a
#include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := TestCallCurl
#LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := \
com_example_testcurl_JNIUtils.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
#LOCAL_LDLIBS := -llog
LOCAL_LDLIBS := -L$(TARGET_ARCH_ABI) -lcurl -llog -ldl
#LOCAL_STATIC_LIBRARIES := $(LOCAL_PATH)/$(TARGET_ARCH_ABI)/libcurl.a
#LOCAL_SHARED_LIBRARIES := Test
include $(BUILD_SHARED_LIBRARY)
#include $(BUILD_STATIC_LIBRARY)
工程目录结构如下(x86 x86_64测试工程就没包含了)
6:lib库下载地址
https://download.csdn.net/download/yunteng521/86517151