István Pecznyik István Pecznyik - 6 months ago 387
Android Question

undefined reference error in ndk android project using static library

I'm creating a native android application in Android Studio. I downloaded GitHub eighthave/openssl-android project. I only added the row

APP_ABI := armeabi-v7a
to
jni/Application.mk
as its first row.

When I run "ndk-build" in command line it compiles
libcrypto.so
and
libssl.so
which I copied to my project's
jni
,
libs/armeabi-v7a
and
jniLibs/armeabi-v7a
folders. I also copied the
include
folder to my project's
jni
folder.

My project Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := libcrypto
LOCAL_SRC_FILES := libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libssl
LOCAL_SRC_FILES := libssl.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := sec
LOCAL_SRC_FILES := main.cpp application.cpp
LOCAL_CPPFLAGS += -fexceptions -frtti -std=c++14
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM
LOCAL_C_INCLUDES := ${ndkDir}/sources/android/native_app_glue
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include # openssl include folder

LOCAL_SHARED_LIBRARIES := android_native_app_glue libcrypto libssl
include $(BUILD_SHARED_LIBRARY)

$(call import-module,android/native_app_glue)


When I run "ndk-build" from command line on my project it creates a
sec.so
file in the
libs
folder and copies the OpenSSL shared libraries next to it.

If I try to create an apk with Android Studio it throws an error:


undefined reference to 'DH_new'



at this row:


DH dh = DH_new();



I can only compile the project in Android Studio if I do not use any functions from the openssl project. And if I do so then I can see all shared libraries in apk's "lib/armeabi-v7a" folder so it can find all the libraries.

So probably the problem could be in my
build.gradle
file so here it is:

apply plugin: 'com.android.model.application'

model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.3"

defaultConfig {
applicationId = "com.example.sec"
minSdkVersion.apiLevel = 16
targetSdkVersion.apiLevel = 23
}
ndk {
moduleName = 'sec'
toolchain = 'clang'
stl = 'c++_static'
cppFlags.addAll(['-Wall', '-std=c++14', '-fexceptions'])
cppFlags.add("-I" + file("src/main/jni/include").absolutePath)
ldLibs.addAll(['log', 'android'])
}
sources {
main {
jni {
dependencies {
project ':nativeactivity' linkage 'static'
}
}
}
}
buildTypes {
release {
minifyEnabled = false
proguardFiles.add(file('proguard-rules.txt'))
}
}
productFlavors{
create("arm7") {
ndk.abiFilters.add("armeabi-v7a")
}
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.4.0'
}


And here is my Application.mk but I do not think it helps much:

APP_ABI := armeabi-v7a
APP_CPPFLAGS += -fexceptions -frtti -std=c++14
APP_STL := gnustl_static


I also tried to create static libraries from OpenSSL project but still got errors. I use gradle-experimental:0.7.0 dependency.

Can please someone help me to figure it out what could be wrong with my project.
Thanks in advance!

Answer

I finally solved my problem. It seems that the only thing I missed is link against the shared libraries in the build.gradle file.

Like this:

ldFlags += "-L${file(path/to/libssl.so)}".toString()

ldLibs += ["ssl"]
Comments