使用JNI无法将AssetManager从Java传递到C ++ [英] Failed to pass assetManager from java to c++ by using JNI

查看:141
本文介绍了使用JNI无法将AssetManager从Java传递到C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在本机c ++代码中使用应用程序的某些资产文件,因此我有一些测试代码,如下所示:

I'd like to using some asset files of my app in native c++ code, so I have some testing code like follows:

Java

package com.example.andy.textureviewtest;
import ...

public class MainActivity extends AppCompatActivity {

    private AssetManager assetManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        assetManager = getAssets();
        generateAssets(assetManager);
    }

    public native String stringFromJNI();

    public native int generateAssets(AssetManager assetManager);

    static {
        System.loadLibrary("native-lib");
    }
}

C ++

#include <jni.h>
#include <string>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <android/log.h>

JNIEXPORT jstring JNICALL Java_com_example_andy_textureviewtest_MainActivity_stringFromJNI(
    JNIEnv* env,
    jobject /* this */) {
  std::string hello = "Hello from C++";
  return env->NewStringUTF(hello.c_str());
}

JNIEXPORT jint JNICALL Java_com_example_andy_textureviewtest_MainActivity_generateAssets(
    JNIEnv* env,jobject thiz,jobject assetManager) {
  AAssetManager* mgr = AAssetManager_fromJava(env, assetManager);
  AAssetDir* assetDir = AAssetManager_openDir(mgr, "");
  const char* filename = (const char*)NULL;
  while ((filename = AAssetDir_getNextFileName(assetDir)) != NULL) {
    AAsset* asset = AAssetManager_open(mgr, filename, AASSET_MODE_UNKNOWN);
    off_t bufferSize = AAsset_getLength(asset);
}
  return 0;
}

但是当我的应用运行时,我仅收到此错误:

but when my app runs, I only got this error:

java.lang.UnsatisfiedLinkError: No implementation found for int com.example.andy.textureviewtest.MainActivity.generateAssets(android.content.res.AssetManager) (tried Java_com_example_andy_textureviewtest_MainActivity_generateAssets and Java_com_example_andy_textureviewtest_MainActivity_generateAssets__Landroid_content_res_AssetManager_2)

这里可能我错过了什么错事?

Anything wrong here that I might have missed?

推荐答案

您的C ++函数可能丢失了

Your C++ functions probably are missing

extern "C"

前缀.它们的导出名称被C ++编译器破坏,并且JNI无法解析它们.

prefix. Their exported names are mangled by the C++ compiler, and JNI fails to resolve them.

这篇关于使用JNI无法将AssetManager从Java传递到C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆