简体   繁体   English

使用静态库的ndk android项目中未定义的引用错误

[英]undefined reference error in ndk android project using static library

I'm creating a native android application in Android Studio. 我正在Android Studio中创建本机android应用程序。 I downloaded GitHub eighthave/openssl-android project. 我下载了GitHub sevenhave / openssl-android项目。 I only added the row APP_ABI := armeabi-v7a to jni/Application.mk as its first row. 我仅将APP_ABI := armeabi-v7a行添加到jni/Application.mk作为其第一行。

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. 当我在命令行中运行“ ndk-build”时,它将编译libcrypto.solibssl.so ,并将它们复制到项目的jnilibs/armeabi-v7ajniLibs/armeabi-v7a文件夹中。 I also copied the include folder to my project's jni folder. 我也将include文件夹复制到了项目的jni文件夹中。

My project Android.mk: 我的项目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. 当我从命令行在项目上运行“ ndk-build”时,它将在libs文件夹中创建一个sec.so文件,并在其旁边复制OpenSSL共享库。

If I try to create an apk with Android Studio it throws an error: 如果我尝试使用Android Studio创建apk,则会引发错误:

 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. 如果我不使用openssl项目中的任何功能,则只能在Android Studio中编译该项目。 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. 如果这样做,我可以在apk的“ lib / armeabi-v7a”文件夹中看到所有共享库,以便可以找到所有库。

So probably the problem could be in my build.gradle file so here it is: 所以问题可能出在我的build.gradle文件中,所以在这里是:

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: 这是我的Application.mk,但我认为它没有太大帮助:

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. 我还尝试从OpenSSL项目创建静态库,但仍然出现错误。 I use gradle-experimental:0.7.0 dependency. 我使用gradle-experimental:0.7.0依赖项。

Can please someone help me to figure it out what could be wrong with my project. 能否请别人帮助我弄清楚我的项目可能出什么问题。 Thanks in advance! 提前致谢!

I finally solved my problem. 我终于解决了我的问题。 It seems that the only thing I missed is link against the shared libraries in the build.gradle file. 看来,我唯一想念的就是在build.gradle文件中指向共享库的链接。

Like this: 像这样:

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

ldLibs += ["ssl"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM