繁体   English   中英

如何在Android Studio和NDK上使用额外的* .so库

[英]How to use extra *.so libraries on Android Studio and NDK

我正在尝试生成一个Android应用程序以使用一些额外的* .so库(特别是“ libinterface.so”)。 这些库是在外部生成的,并作为依赖项包含在从Java端调用的包装器类中。 该库存储在“ src / main / jniLibs / armeabi-v7a”中。 系统将所有.so文件包含到生成的应用程序中。

以前,我是为此目的而使用Eclipse的,并且能够使用此库,但是使用Android Studio却遇到问题。

生成的错误是:

/home/******/Libraries/android-sdk-linux/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/../lib/gcc/aarch64-linux-android/4.9/../../../../aarch64-linux-android/bin/ld: cannot find -linterface

由于链接器引发了错误,因此它看起来与库包含步骤有关。 在Eclipse上,我使用的是“ Android.mk”文件来包含新库,但是我找不到使用Gradle进行编码的方法。

#Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libinterface-prebuilt
LOCAL_SRC_FILES := prebuilt/libinterface.so
include $(PREBUILT_SHARED_LIBRARY)

我正在尝试将包含此gradle定义的库包括在内(注意:我已使用本教程包含了最新的JNI支持和gradle-experimental):

...
android.buildTypes {
    release {
        minifyEnabled = false
        proguardFiles.add(file('proguard-android.txt'))
    }
}

android.ndk {
    moduleName = "custom_wrapper_jni"
    cppFlags.add("-I" + file("src/main/jni").absolutePath)
    cppFlags.add("-I" + file("../Integration/include").absolutePath)  // <- New library header include path 
    cppFlags.add("-L" + file("src/main/jniLibs/armeabi-v7a").absolutePath)  // <- Path where the library is stored
    cppFlags.add("-std=c++11")
    stl = "stlport_static" // Which STL library to use: gnustl or stlport
    ldLibs.add("log")

    ldLibs.add("interface")    //<- Library to be included
}
...

该库使用CMake和makefile工具在外部进行编译,并且针对Android平台“正确”交叉编译(已通过Eclipse和ADT测试)。

我已经实现了这样的包装器:

// custom_wrapper_jni.h
#ifndef ANDROID_JNI_H
#define ANDROID_JNI_H

#include <jni.h>

extern "C"
{
    JNIEXPORT jint JNICALL
    Java_com_example_goe_android_JniInterface_testFunction(JNIEnv *env,
                                                           jobject instance);
}
#endif

// custom_wrapper_jni.cpp
#include <custom_wrapper_jni.h>

#include "Interface.h"   // Header of the included library

Interface* mInterface = Interface::create();  // Generate the library class instance

JNIEXPORT jint JNICALL
Java_com_example_goe_android_JniInterface_testFunction(JNIEnv *env,
                                                    jobject instance)
{
    LOGI("Test function called in wrapper!");
    return mInterface->test();    // Use the instance
}

库的标题如下所示:

#ifndef INTERFACE_H__
#define INTERFACE_H__

#include <string>

class Interface
{
public:
    static Interface* create();
    virtual ~Interface(){}


    // Testing function
    virtual int test() = 0;

protected:
    Interface();
};
#endif // INTERFACE_H__

提前致谢。

更新:

此示例之后 ,我在gradle脚本中包含了一些块:

def lib_path = file("src/main/jniLibs").absolutePath

model {

    repositories {
        libs(PrebuiltLibraries) {
            newlibs {
                headers.srcDir file("../Integration/include").absolutePath
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("${lib_path}/${targetPlatform.getName()}/libinterface.so")
                    println "Included libraries: " + file("${lib_path}/${targetPlatform.getName()}/libinterface.so")
                }
            }
        }
    }

    android {
     ...
    }

    android.sources {
        main {
            jni {
                dependencies {
                    library "newlibs" linkage "shared"
                }
            }
        }
    }
}

但不起作用:

Error: org.gradle.nativeplatform.toolchain.internal.CommandLineToolInvocationFailure: Linker failed while linking libcustom_wrapper_jni.so.

好的,可能有两个不同的问题。

首先,必须确保为正确的体系结构编译了库。 如果您使用的是armeabi-v7a库,但是编译器正在尝试加载armeabi库,则编译将失败。

其次,在第一个问题之后,您还必须根据所使用的体系结构包括库。 在模块build.gradle脚本中使用' flavors '配置。

例如,您可以尝试执行以下操作:

android.productFlavors {
    create("arm") {
        ndk.with{
            abiFilters.add("armeabi")

            File curDir = file('./')
            curDir = file(curDir.absolutePath)
            String libsDir = curDir.absolutePath + "/src/main/jniLibs/armeabi/"

            ldLibs.add(libsDir + "libinterface.so")
        }
    }
    create("armv7") {
        ndk.with {
            abiFilters.add("armeabi-v7a")

            File curDir = file('./')
            curDir = file(curDir.absolutePath)
            String libsDir = curDir.absolutePath + "/src/main/jniLibs/armeabi-v7a/"

            ldLibs.add(libsDir + "libinterface.so")
        }
    }
}

此外,我建议您使用“ jniLibs”存储库,因为这是它们的默认路径,但是每个拱门使用不同的文件夹。

您可以检查类似这样的其他示例。

希望这可以帮助。 问候。

暂无
暂无

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

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