我正在尝试使用experimental gradle plugin将openSSL库添加到我的android studio 2.0 preview 7项目中。
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";
}发布于 2016-04-11 22:46:56
您需要将openssl库指定为依赖项。这里有一个here片段,展示了如何添加一个预建的库。
https://stackoverflow.com/questions/35034419
复制相似问题