簡體   English   中英

如何使用實驗性Gradle插件(NDK)在Android Studio中添加openssl lib?

[英]How to add openssl lib in android studio with experimental gradle plugin (NDK)?

我正在嘗試使用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";
}

您需要將openssl庫指定為依賴項。 這里有一個片段顯示了如何添加預構建的庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM