我正在尝试将 openSSL 库添加到我的项目android studio 2.0 preview 7
中experimental gradle plugin
。
dependencies {
classpath 'com.android.tools.build:gradle-experimental:0.4.0'
}
我所做的是我下载openSSL library
并将其放入jni
文件夹中。我还有另一个使用这个库的 .c 文件。我已经包含了我需要的文件并且代码中没有错误。我的.c
文件名是hello-jni.c
,我将其声明为build.gradle (module: app)
:
android.ndk {
moduleName = "hello-jni"
}
我也在我的 MainActivity 中加载了我的库,如下所示:
static {
System.loadLibrary("hello-jni");
}
但是当我尝试构建我的项目时,会出现如下错误:
Error:(51) undefined reference to `RSA_generate_key'
Error:error: ld returned 1 exit status
Error:Execution failed for task ':app:linkArm64-v8aDebugHello- jniSharedLibrary'.
> A build operation failed.
Linker failed while linking libhello-jni.so.
我的 hello-jni.c 源代码:
#include <jni.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include "boringssl/crypto/openssl/base.h"
#include "boringssl/crypto/openssl/rsa.h"
#include "boringssl/crypto/openssl/pem.h"
#define KEY_LENGTH 2048
#define PUB_EXP 3
#define PRINT_KEYS
#define WRITE_TO_FILE 0
size_t pri_len; // Length of private key
size_t pub_len; // Length of public key
char *pri_key; // Private key
char *pub_key; // Public key
char msg[KEY_LENGTH/8]; // Message to encrypt
char *encrypt = NULL; // Encrypted message
char *decrypt = NULL; // Decrypted message
char *err; // Buffer for any error messages
JNIEXPORT jstring JNICALL
Java_com_example_ndktest_SignUpActivity_testString(JNIEnv *env, jobject activity) {
// In this line, the error happens!(in build time)
RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);
//continue to work with key pair ...
return "some jstring";
}