I'm trying to add openSSL library to my project in android studio 2.0 preview 7
with experimental gradle plugin
.
dependencies {
classpath 'com.android.tools.build:gradle-experimental:0.4.0'
}
What I did is that I downloaded the openSSL library
and put it into jni
folder. And I have another .c file that uses this library. I have included the files that I need and there are no errors in code. My .c
file name is hello-jni.c
and I declared it into my build.gradle (module: app)
like this:
android.ndk {
moduleName = "hello-jni"
}
and also I loaded my library in my MainActivity like this:
static {
System.loadLibrary("hello-jni");
}
but when I try to build my project, an error shows up like this:
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.
My hello-jni.c source code:
#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";
}
You need to specify the openssl library as a dependency. There's a snippet here showing how to add a prebuilt library.