3

At the beginning, I have to say I already searched around as many possible answers as I can to my answer. Some of them are really helpful, but not in Android Studio way. Also, I can get my JNI worked with the well-known example "getStringFromNative()", so please don't give me this kind of links. What I have tried so far :

1. structure of my directory

app |- src |- main |- java |- jni |- libs |- libcrypto.a |- libssl.a |- openssl |- xxxxx.h |...... -> those .h file of openssl |- com_ais_ndksample_MainActivity.h |- CipherModule.cpp |- CipherModule.h 

2. build.gradle

apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "23.0.0 rc3" defaultConfig { applicationId "com.ais.ndksample" minSdkVersion 16 targetSdkVersion 22 versionCode 1 versionName "1.0" ndk { moduleName "JniDemo" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard- android.txt'), 'proguard-rules.pro' } } sourceSets { main { jni.srcDirs = ['src/main/jni'] jniLibs.srcDirs = ['src/main/jni/libs'] } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.2.1' } 
  1. The errors occurred in my CipherModule.cpp, I have several statement on the top as below:

    #include "CipherModule.h" #include <openssl/aes.h> #include <stdio.h> #include <iostream> 

and the #include shows warning that Unused import statement, but I did use that. When I built, I got the error message indicated as followed:

undefined reference to `AES_set_encrypt_key` undefined reference to `AES_cbc_encrypt` 

As a result, I doubted that I didn't make my openssl library right, but I did specify it in my build.gradle. I had made so much efforts on this and got no luck. Any clue or thought would be appreciated!!!!

3
  • I am not aware that there is support for depending upon static libraries as you are here. There definitely is no such support in the upcoming Gradle plugin overhaul. Even if depending upon static libraries is supported, I would expect them to have to be organized by ABI -- your directory structure does not indicate the CPU architecture for those .a files. Commented Aug 8, 2015 at 13:54
  • 1
    This guy claims to have gotten static library support working, but that was 18 months ago and required a makefile. Commented Aug 8, 2015 at 13:54
  • Thanks for your information! I will check out the link you provided! Commented Aug 9, 2015 at 9:55

1 Answer 1

1

Finally I found out the root cause of this. The error messages I got as below:

undefined reference to `AES_set_encrypt_key` undefined reference to `AES_cbc_encrypt` 

are due to the fact that my libcrypto.a and libssl.a library are not built for android-armv7 based devices. I solved this issue by "cross-compile" my openssl for armv7. The script is shown as follow:

export CC=/your_path_to_sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc export AR=/your_path_to_sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ar export RANLIB=/your_path_to_sdk/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc-ranlib ./Configure android-armv7 export ANDROID_DEV=/your_path_to_sdk/ndk-bundle/platforms/android-15/arch-arm/usr make 

You can run this script inside your openssl folder downloaded from official website and your will get your libcrypto.a and libssl.a inside openssl folder. Those are compiled for android this time. If you guys still encounter similar issues, feel free to ask me!

Sign up to request clarification or add additional context in comments.

1 Comment

Hello @pptang. I have this problem. I want add OpenSSl into Android Studio. I just use cpp support by android studio. But, I don't know how to add openssl into android studio when use cpp. Can you help me?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.