如何正确地将 http C 库包含到 Android 项目中?
How to properly include an http C library into an Android Project?
我正在制作一个 Android 项目,我正在尝试使用 HTTPS 向服务器发出 post 请求。要发出 post 请求,我需要使用 JNI,所以我需要在 C
.
中实现它
我的想法是使用一个可以包含在我的项目中的库,就像我对 minizip library here 所做的那样。
我发现这个库 here 在我看来足够轻便并且可以满足我的目的。我包含在文件夹 c
中,您还可以在下方看到文件 ca_cert.h
、https.c
和 https.h
以及文件夹 mbedtls
,与在github 项目。
├── app.iml
├── build.gradle
├── CMakeLists.txt
└── src
├── androidTest
│ └── java
│ └── etc
├── main
│ ├── AndroidManifest.xml
│ ├── c
│ │ ├── ca_cert.h
│ │ ├── https.c
│ │ ├── https.h
│ │ ├── mbedtls
│ │ │ ├── etc
│ │ ├── native-lib.c
│ │ ├── pathfinder.c
│ │ ├── pathfinder.h
│ │ ├── post_data.c
│ │ ├── post_data.h
│ │ ├── third
│ ├── java
│ │ └── etc
│ └── res
│ ├── etc
└── test
└── java
└── etc
正如您在上面的树结构中看到的,我在根目录中有一个 CMakeLists.txt
,在 src/main/c/
中,您将看到我从提到的 https 库中获取的文件和文件夹。
CMakeLists.txt
内容如下
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/c/native-lib.c
src/main/c/ca_cert.h
src/main/c/https.h
)
include_directories(native-lib SHARED src/main/c/mbedtls/include)
#add_definitions(-DHAVE_ZLIB)
find_library( PRE_BUILD_ANDROID_LIB android)
find_library( log-lib log)
find_library(z-lib z)
target_link_libraries( native-lib
${PRE_BUILD_ANDROID_LIB}
${log-lib}
${z-lib}
)
我确实遗漏了一些东西,因为当我尝试 运行 一个像下面这样的简单示例时
JNIEXPORT jint JNICALL
Java_example_example_do_post(
JNIEnv *env,
jobject this ) {
NSV_LOGE("post_data starts\n");
char *url;
char data[1024], response[4096];
int i, ret, size;
HTTP_INFO hi1, hi2;
// Init http session. verify: check the server CA cert.
http_init(&hi1, FALSE);
http_init(&hi2, TRUE);
url = "http://httpbin.org/get?message=https_client";
ret = http_get(&hi1, url, response, sizeof(response));
return 0;
}
我明白了
../../../../src/main/c/native-lib.c:56: error: undefined reference to 'http_init'
../../../../src/main/c/native-lib.c:57: error: undefined reference to 'http_init'
../../../../src/main/c/native-lib.c:61: error: undefined reference to 'http_get'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
替换
src/main/c/ca_cert.h
src/main/c/https.h
与
src/main/c/https.c
在CMakeLists.txt
.
我正在制作一个 Android 项目,我正在尝试使用 HTTPS 向服务器发出 post 请求。要发出 post 请求,我需要使用 JNI,所以我需要在 C
.
中实现它
我的想法是使用一个可以包含在我的项目中的库,就像我对 minizip library here 所做的那样。
我发现这个库 here 在我看来足够轻便并且可以满足我的目的。我包含在文件夹 c
中,您还可以在下方看到文件 ca_cert.h
、https.c
和 https.h
以及文件夹 mbedtls
,与在github 项目。
├── app.iml
├── build.gradle
├── CMakeLists.txt
└── src
├── androidTest
│ └── java
│ └── etc
├── main
│ ├── AndroidManifest.xml
│ ├── c
│ │ ├── ca_cert.h
│ │ ├── https.c
│ │ ├── https.h
│ │ ├── mbedtls
│ │ │ ├── etc
│ │ ├── native-lib.c
│ │ ├── pathfinder.c
│ │ ├── pathfinder.h
│ │ ├── post_data.c
│ │ ├── post_data.h
│ │ ├── third
│ ├── java
│ │ └── etc
│ └── res
│ ├── etc
└── test
└── java
└── etc
正如您在上面的树结构中看到的,我在根目录中有一个 CMakeLists.txt
,在 src/main/c/
中,您将看到我从提到的 https 库中获取的文件和文件夹。
CMakeLists.txt
内容如下
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/c/native-lib.c
src/main/c/ca_cert.h
src/main/c/https.h
)
include_directories(native-lib SHARED src/main/c/mbedtls/include)
#add_definitions(-DHAVE_ZLIB)
find_library( PRE_BUILD_ANDROID_LIB android)
find_library( log-lib log)
find_library(z-lib z)
target_link_libraries( native-lib
${PRE_BUILD_ANDROID_LIB}
${log-lib}
${z-lib}
)
我确实遗漏了一些东西,因为当我尝试 运行 一个像下面这样的简单示例时
JNIEXPORT jint JNICALL
Java_example_example_do_post(
JNIEnv *env,
jobject this ) {
NSV_LOGE("post_data starts\n");
char *url;
char data[1024], response[4096];
int i, ret, size;
HTTP_INFO hi1, hi2;
// Init http session. verify: check the server CA cert.
http_init(&hi1, FALSE);
http_init(&hi2, TRUE);
url = "http://httpbin.org/get?message=https_client";
ret = http_get(&hi1, url, response, sizeof(response));
return 0;
}
我明白了
../../../../src/main/c/native-lib.c:56: error: undefined reference to 'http_init'
../../../../src/main/c/native-lib.c:57: error: undefined reference to 'http_init'
../../../../src/main/c/native-lib.c:61: error: undefined reference to 'http_get'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
替换
src/main/c/ca_cert.h
src/main/c/https.h
与
src/main/c/https.c
在CMakeLists.txt
.