在一个Android项目中使用libcryto.so和libssl.so? [英] use libcryto.so and libssl.so in an android project?

查看:656
本文介绍了在一个Android项目中使用libcryto.so和libssl.so?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android NDK的新手.我想基于openssl libary构建一个RSA示例. 首先,我在guardianproject中使用ndk-build构建了libssl.so和libcrypto.so库. 接下来,我创建一个新的示例项目来集成libary(libss.so& lybcryto.so).我在此 post

I'm beginer to Android NDK. I want to build a RSA example base on openssl libary. First, I built libssl.so and libcrypto.so librairies with ndk-build in the guardianproject. Next, I create a new sample project to integrate libary (libss.so & lybcryto.so). I follow the same in this post

我的应用目录

TrialApp
|
|-->Activity.java (includes System.LoadLibrary calls)
|
|-->jni
    |-->TestJNI2.cpp
    |
    |-->Android.mk
    |
    |-->includes
    |   |
    |   |-->openssl (dir containing *.h files)
    |
    |-->precompiled
       |-->libssl.so
       |-->libcrypto.so

我的android.mk:

My android.mk:

LOCAL_PATH := $(call my-dir)

# Prebuilt libssl
include $(CLEAR_VARS)
LOCAL_MODULE := ssl
LOCAL_SRC_FILES := precompiled/libssl.so
include $(PREBUILT_SHARED_LIBRARY)

# Prebuilt libcrypto
include $(CLEAR_VARS)
LOCAL_MODULE := crypto
LOCAL_SRC_FILES := precompiled/libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)

c_includes := $(LOCAL_PATH)
cf_includes := includes/openssl

cf_includes := $(addprefix -Ijni/,$(cf_includes))

export_c_includes := $(c_includes)

LOCAL_MODULE := security
LOCAL_SRC_FILES := TestJNI2.cpp
LOCAL_CFLAGS += $(cf_includes)
LOCAL_EXPORT_C_INCLUDES := $(export_c_includes)
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS += $(LOCAL_PATH)/precompiled/libssl.so
LOCAL_LDLIBS += $(LOCAL_PATH)/precompiled/libcrypto.so

include $(BUILD_SHARED_LIBRARY)

TestJNI2.cpp

TestJNI2.cpp

/* OpenSSL headers */

#include "openssl/bio.h"
#include "openssl/ssl.h"
#include "openssl/err.h"

/* Initializing OpenSSL */

void init_openssl(void){
    SSL_load_error_strings();
    ERR_load_BIO_strings();
    OpenSSL_add_all_algorithms();
}

然后,当我使用ndk-build进行构建时,总是会遇到这样的问题

Then when i build with ndk-build always have problems like this

/data/workspace/TestJNI2/jni/TestJNI2.cpp:3:25: error: openssl/bio.h: No such file or directory
/data/workspace/TestJNI2/jni/TestJNI2.cpp:4:25: error: openssl/ssl.h: No such file or directory
/data/workspace/TestJNI2/jni/TestJNI2.cpp:5:25: error: openssl/err.h: No such file or directory
/data/workspace/TestJNI2/jni/TestJNI2.cpp: In function 'void init_openssl()':
/data/workspace/TestJNI2/jni/TestJNI2.cpp:10: error: 'SSL_load_error_strings' was not declared in this scope
/data/workspace/TestJNI2/jni/TestJNI2.cpp:11: error: 'ERR_load_BIO_strings' was not declared in this scope
/data/workspace/TestJNI2/jni/TestJNI2.cpp:12: error: 'OpenSSL_add_all_algorithms' was not declared in this scope
make: *** [/data/workspace/TestJNI2/obj/local/armeabi/objs/security/TestJNI2.o] Error 1

有人可以帮助我吗?还是如何在openssl lib上构建示例RSA算法?

Can anyone help me? Or how to build an example RSA algorthim base on openssl lib?

推荐答案

您应该在脚本中为libssl和libcrypto构建静态库.如果不能,请重命名这些库(可以在构建后执行此操作,同时将其复制到precompiled目录中).原因是系统带有这些共享库的自身(可能不同)版本,并且加载程序将使用/system/lib/libssl.so/system/lib/libcrypto.so而不是您的私有副本.

You should build static libraries for libssl and libcrypto in your script. If you can't, rename these libraries (you can do this after build, while copying to your precompiled directory). The reason is that the system comes with its own (probably different) version of these shared libraries, and the loader will use /system/lib/libssl.so and /system/lib/libcrypto.so instead of your private copies.

关于 Android.mk 文件,我为您做了一些清理(请注意,我没有更改预构建的 LOCAL_MODULE 的名称,但更改了您最终构建了 LOCAL_MODULE ,因为security太通用了,也有可能与某些设备上的系统库匹配):

Regarding the Android.mk file, I slightly cleaned it up for you (note that I did not change the names of prebuilt LOCAL_MODULEs, but changed the name of LOCAL_MODULE you finally build, because security is, well, too generic and could also happen to match a system library on some device):

LOCAL_PATH := $(call my-dir)

# Prebuilt libssl
include $(CLEAR_VARS)
LOCAL_MODULE := ssl
LOCAL_SRC_FILES := precompiled/libPrivateSsl.so
include $(PREBUILT_SHARED_LIBRARY)

# Prebuilt libcrypto
include $(CLEAR_VARS)
LOCAL_MODULE := crypto
LOCAL_SRC_FILES := precompiled/libPrivateCrypto.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := PrivateSecurity
LOCAL_C_INCLUDES := includes
LOCAL_SRC_FILES := TestJNI2.cpp
LOCAL_LDLIBS := -llog
LOCAL_SHARED_LIBRARIES := ssl crypto

include $(BUILD_SHARED_LIBRARY)

别忘了您的Java应该首先加载依赖项:

Don't forget that your Java should load the dependencies first:

{
    System.loadLibrary("PrivateSsl");
    System.loadLibrary("PrivateCrypto");
    System.loadLibrary("PrivateSecurity");
}

这篇关于在一个Android项目中使用libcryto.so和libssl.so?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆