I'm making a jni Function in android to generate file checkSum using BoringSSL.
So I built boringssl to shared Library for use Prebuilt Library in Android studio.
I copied the *.so file to my project src/main/jniLibs/lib and BoringSSL include folder to src/main/jni
I referenced this project and apply to my projects.
https://github.com/googlesamples/android-ndk/tree/master/hello-libs
My build.gradle file like this. I'm using gradle-experimental:0.7.0( and Window10, Android Studio 2.1.2 )
apply plugin: 'com.android.model.application'
model {
repositories {
libs(PrebuiltLibraries) {
boringssl {
headers.srcDir "src/main/jni/include/openssl"
binaries.withType(SharedLibraryBinary) {
sharedLibraryFile = file("src/main/jniLibs/lib/libcrypto.so")
sharedLibraryFile = file("src/main/jniLibs/lib/libssl.so")
}
}
}
}
android {
compileSdkVersion = 24
buildToolsVersion = '24.0.1'
defaultConfig {
applicationId = 'yein.a'
minSdkVersion.apiLevel = 19
targetSdkVersion.apiLevel = 24
versionCode = 1
versionName = '1.0'
}
ndk {
moduleName = 'hello-libs'
ldLibs.addAll(['android', 'log'])
}
sources {
main {
jni {
dependencies {
library 'boringssl' linkage 'shared'
}
}
jniLibs{
source{
srcDir "src/main/jniLibs/lib"
}
}
}
}
productFlavors{
create("arm"){
ndk.abiFilters.add("armeabi-v7a")
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles.add(file('proguard-android.txt'))
}
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.1.1'
}
This is my project structure.
I think I properly apply the example projects, because it is not occurred gradle sync error.
And when I use MD5 function Android Studio auto complete function name like this.
But I click run it occurred error like this...
I searched a lot project in google,github, and of course stackoverflow previous answers but I still not solve my problem.
I tried change gradle version but other gradle version occurred error in auto generated jni function except gradle-experimental:0.7.0.
Could anyone point me in the right direction or even a solution?
Thanks for read my Question.
you can try to call the java API from JNI, there is a nice tutorial here, Google recommends on this site you should call the java API or you can link the libraries as you did.
If you can't or don't want to call the java API you can always use CRC32 instead of MD5, CRC32 uses the libz library and that library is already on the NDK files. CRC32 in C.
#include <zlib.h>
int checkSumCRC32(char *text){
long n = crc32(0, (const void*)text, strlen(text));
printf("CRC32 %d", n);
return n;
}
Remember to add ldLibs.addAll(['z'])
to your Gradle file
In case you need MD5 and BoringSSL this is my gradle.build file for BoringSSL.
Note: I am using the 3 libraries you compile on ubuntu, libssl.a libcrypto.a and libdecrepit.a
Note2: I am using com.android.tools.build:gradle-experimental:0.7.0-alpha4
apply plugin: 'com.android.model.application'
model {
repositories {
libs(PrebuiltLibraries) {
libcrypto {
headers.srcDir "./main/jni/include/openssl"
binaries.withType(StaticLibraryBinary) {
staticLibraryFile = file("src/main/jni/${targetPlatform.getName()}/crypto/libcrypto.a")
}
}
libssl {
headers.srcDir "./main/jni/include/openssl"
binaries.withType(StaticLibraryBinary) {
staticLibraryFile = file("src/main/jni/${targetPlatform.getName()}/ssl/libssl.a")
}
}
libdecrepit {
headers.srcDir "src/main/jni/include/openssl"
binaries.withType(StaticLibraryBinary) {
staticLibraryFile = file("src/main/jni/${targetPlatform.getName()}/decrepit/libdecrepit.a")
}
}
}
}
android {
compileSdkVersion = 23
buildToolsVersion = "22.0.1"
defaultConfig {
applicationId = "net.app"
minSdkVersion.apiLevel = 9
targetSdkVersion.apiLevel = 23
versionCode = 1
}
ndk {
platformVersion = 21
moduleName = "modulename"
toolchain = "clang"
abiFilters.addAll([ 'armeabi-v7a'])
CFlags.addAll(["-fvisibility=hidden", "-fpic"])
ldLibs.addAll(['log', 'z', 'android']) //Libreria llog, lz y landroid
}
sources {
main {
jni{
dependencies {
library "libcrypto" linkage "static"
library "libssl" linkage "static"
library "libdecrepit" linkage "static"
}
}
}
}
}
}
I haven't used MD5 but I have used SHA256 using boringSSL and it works fine. You can use it too as a checksum method. This is my method for SHA256.
char *sha256(char *str){
unsigned char hash[SHA256_DIGEST_LENGTH];
char *output = (char *) malloc(sizeof(char)*((SHA256_DIGEST_LENGTH*2)+1));
if (output == NULL) {
return NULL;
}
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, str, strlen(str));
SHA256_Final(hash, &sha256);
int i;
for(i = 0; i < SHA256_DIGEST_LENGTH; i++){
sprintf(&output[i*2], "%02x", (unsigned int)hash[i]);
}
return output;
}