首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建一个嵌入JNI的Android库:没有找到实现

创建一个嵌入JNI的Android库:没有找到实现
EN

Stack Overflow用户
提问于 2016-08-02 12:34:28
回答 1查看 1.4K关注 0票数 0

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

我已经创建了一个模块(使用Android类型),因此它使用了超级电源库:

  • 我配置了等级,所以它允许NDK / JNI
  • 我包括了与超级电源相关的.a文件和头。
  • 在我的Java类中,我设置了本机方法,该方法映射到cpp方法。

为了创建这个模块,我高度使用了超级电源提供的交叉例子示例。这是一个Android应用程序,现在作为第一步,我只想把它分成两个部分:一个应用程序和一个Android库。

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

代码语言:javascript
复制
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中:

我的java类MyDSP (MyDSP.java):

代码语言:javascript
复制
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)

代码语言:javascript
复制
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文件:SuperpoweredExprese.h (SuperpoweredExample.h)

代码语言:javascript
复制
void onPlayPause(bool play);

库(build.gradle)的分级配置:

应用插件:'com.android.model.library‘

代码语言:javascript
复制
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/超能力路径相匹配。

EN

回答 1

Stack Overflow用户

发布于 2016-08-02 12:59:30

如果您正在使用experimental gradle,它负责创建cpp文件,在该文件中创建函数,并将其与您的java类链接。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38720574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档