制作一个嵌入JNI的Android库:找不到任何实现

时间:2016-08-02 12:34:29

标签: android android-studio android-ndk java-native-interface

我的目标是构建一个嵌入 superpowered 库的Android库。该库提供Android静态库(.a文件)和C ++头文件。

我已经创建了一个模块(类型为Android Library),因此它使用 superpowered 库:

  • 我配置了graddle,因此它允许NDK / JNI
  • 我包含.a与 superpowered
  • 相关的文件和标题
  • 在我的Java类中,我设置了本地方法,该方法映射到cpp方法。

要创建此模块,我高度使用了 superpowered 提供的Cross Example示例。它是一个Android应用程序,现在作为第一步,我只想将它分为两​​部分:一个应用程序和一个Android库。

在我的Android应用程序中,我添加了依赖于此模块。但是当我调用一个调用JNI方法的方法时,我得到了这个错误:

E/art: No implementation found for void
  com.example.mylib.MyDSP.onPlayPause(boolean) 
  (tried Java_com_example_mylib_MyDSP_onPlayPause 
  and Java_com_example_mylib_MyDSP_onPlayPause__Z)

关于它为什么不起作用的任何想法?

来源:

在Android lib中:

我的java课程MyDSPMyDSP.java):

package com.example.mylib;

public class MyDSP {

    public  void CallJniCppMethod() {

        this.onPlayPause(true);
    }

    private native void onPlayPause(boolean play);

    static {
        System.loadLibrary("SuperpoweredExample");
    }

}

在cpp文件中:SuperpoweredExample.cpp(SuperpoweredExample.cpp

extern "C" JNIEXPORT void Java_com_example_mylib_MyDSP_onPlayPause(JNIEnv * __unused javaEnvironment, jobject __unused obj, jboolean play) {
    //example->onPlayPause(play);

    // We do nothing, but it's OK,
    // Just want to see if the JNI call does not throw exception
}

h文件:SuperpoweredExample.h(SuperpoweredExample.h

void onPlayPause(bool play);

lib(build.gradle)的graddle配置:

申请插件:'com.android.model.library'

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def superpowered_sdk_path = properties.getProperty('superpowered.dir')

model {
    repositories {
        libs(PrebuiltLibraries) {
            superpowered { // this is where you declare the "superpowered" static library
                headers.srcDir "${superpowered_sdk_path}"
                binaries.withType(StaticLibraryBinary) { // attaching library files to each platform
                    def platformName = targetPlatform.getName()
                    if (platformName == "armeabi-v7a") {
                        staticLibraryFile = file("${superpowered_sdk_path}/libSuperpoweredAndroidARM.a")
                    } else if (platformName == "arm64-v8a") {
                        staticLibraryFile = file("${superpowered_sdk_path}/libSuperpoweredAndroidARM64.a")
                    } else if (platformName == "x86") {
                        staticLibraryFile = file("${superpowered_sdk_path}/libSuperpoweredAndroidX86.a")
                    } else if (platformName == "x86_64") {
                        staticLibraryFile = file("${superpowered_sdk_path}/libSuperpoweredAndroidX86_64.a")
                    }
                }
            }
        }
    }

    android {
        compileSdkVersion 24
        buildToolsVersion "24.0.1"

        defaultConfig {
            minSdkVersion.apiLevel = 21 // more than 95% of all active Android devices
            targetSdkVersion.apiLevel = 21
            versionCode 1
            versionName "1.0"
        }

    }


    android.ndk { // your application's native layer parameters
        moduleName = "SuperpoweredExample"
        platformVersion = 21
        stl = "c++_static"
        CFlags.addAll(["-O3", "-fsigned-char"]) // full optimization, char data type is signed
        cppFlags.addAll(["-fsigned-char", "-I${superpowered_sdk_path}".toString()])
        ldLibs.addAll(["log", "android", "OpenSLES"]) // load these libraries: log, android, OpenSL ES (for audio)
        abiFilters.addAll(["armeabi-v7a", "arm64-v8a", "x86", "x86_64"]) // these platforms cover 99% percent of all Android devices
    }

    android.sources.main.jni {
        source {
            srcDir "jni"
            srcDir "${superpowered_sdk_path}/AndroidIO"
        }
        dependencies {
            library "superpowered" linkage "static" // this is where you attach the "superpowered" static library to your app
        }
    }

}

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

注意:

我的所有来源都可以在Github上找到:https://github.com/DelattreAdixon/Superpowered-Android-Lib

为了构建/运行它,您必须更改le local.properties内容,使其与您的ndk / sdk / superpowered路径匹配。

1 个答案:

答案 0 :(得分:0)

如果您使用experimental gradle,它会处理创建cpp文件,在此文件中创建函数并将其与您的java类链接。

尝试删除cpp文件,单击private native void onPlayPause(boolean play)将其标记为alt + intro,它会显示一个对话框,用于自动生成cpp文件及其中的函数。