我试图在android ndk中使用openssl。
我使用了来自guardianproject github page的openssl源代码,它能够生成libssl.so和libcrypto.so文件。
目前我仍然坚持将这两个文件与我的jni接口c文件链接。
下面是我的Android.mk文件,我的jni接口c文件名为" security.c"
LOCAL_PATH := $(call my-dir)
# Prebuilt libssl
include $(CLEAR_VARS)
LOCAL_MODULE := ssl
LOCAL_SRC_FILES := libprebuilt/libssl.so
include $(PREBUILT_SHARED_LIBRARY)
# Prebuilt libcrypto
include $(CLEAR_VARS)
LOCAL_MODULE := crypto
LOCAL_SRC_FILES := libprebuilt/libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := security
LOCAL_SRC_FILES := security.c
LOCAL_SHARED_LIBRARIES= ssl crypto
include $(BUILD_SHARED_LIBRARY)
这是我的security.c文件
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <jni.h>
#include "openssl/bio.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
jstring Java_com_test_stringFromJNI(JNIEnv* env, jobject thiz) {
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
return (*env)->NewStringUTF(env, "Hello from JNI");
}
使用ndk-build构建时,发生此错误
Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 8 in /Users/huydo/cur-project/company/kimisaki_native/android_os/ensemble_girls/AndroidManifest.xml
[armeabi-v7a] Install : libcrypto.so => libs/armeabi-v7a/libcrypto.so
[armeabi-v7a] Compile thumb : security <= security.c
[armeabi-v7a] SharedLibrary : libsecurity.so
[armeabi-v7a] Install : libsecurity.so => libs/armeabi-v7a/libsecurity.so
[armeabi-v7a] Install : libssl.so => libs/armeabi-v7a/libssl.so
[armeabi] Install : libcrypto.so => libs/armeabi/libcrypto.so
[armeabi] Compile thumb : security <= security.c
[armeabi] SharedLibrary : libsecurity.so
[armeabi] Install : libsecurity.so => libs/armeabi/libsecurity.so
[armeabi] Install : libssl.so => libs/armeabi/libssl.so
[x86] Install : libcrypto.so => libs/x86/libcrypto.so
/Users/huydo/android-ndk-r9d/toolchains/x86-4.6/prebuilt/darwin-x86_64/bin/i686-linux-android-strip: Unable to recognise the format of the input file `/Users/huydo/cur-project/XXX/android_os/YYY/libs/x86/libcrypto.so'
make: *** [/Users/huydo/cur-project/XXX/android_os/YYY/libs/x86/libcrypto.so] Error 1
make: *** Deleting file `/Users/huydo/cur-project/XXX/android_os/YYY/libs/x86/libcrypto.so'
注意到libssl.so和libcrypto.so已被正确复制到armeabi和armeabi-v7a中。它在为mips和x86架构构建时陷入困境。 如果您之前遇到过这个错误,可以给我一个胶水发生的事情。
答案 0 :(得分:1)
在编译每个无法工作的架构时,您使用相同的ARM libssl.so和libcrypto.so。
您需要使用针对正确架构的预构建。为此,您可以在针对每个体系结构命名的 libprebuilt 中创建子文件夹( armeabi , armeabi-v7a , x86 ,< em> mips ),在每个文件下添加prebuilts .so文件,并在 Android.mk 中使用 $(TARGET_ARCH_ABI)来引用你的.so文件:
LOCAL_PATH := $(call my-dir)
# Prebuilt libssl
include $(CLEAR_VARS)
LOCAL_MODULE := ssl
LOCAL_SRC_FILES := libprebuilt/$(TARGET_ARCH_ABI)/libssl.so
include $(PREBUILT_SHARED_LIBRARY)
# Prebuilt libcrypto
include $(CLEAR_VARS)
LOCAL_MODULE := crypto
LOCAL_SRC_FILES := libprebuilt/$(TARGET_ARCH_ABI)/libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := security
LOCAL_SRC_FILES := security.c
LOCAL_SHARED_LIBRARIES= ssl crypto
include $(BUILD_SHARED_LIBRARY)
如果你找不到x86版本的libssl.so和libcrypto.so,你可以从x86模拟器中取出它们。
要指定要编译projet的架构,请在 Application.mk 中使用 APP_ABI 变量,例如:
APP_ABI := armeabi-v7a x86 mips