简体   繁体   中英

Unity Native C++ Plug-in for Android

I am making a native plug-in for Android written in C++ and packaged in a shared library for the Unity Game Engine. However, I'm getting an EntryPointNotFoundException with even the default native-lib.cpp generated by Android Studio.

#include <jni.h>
#include <string>

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

In Android Studio I built the project. Then in Unity I made the folder structure Assets/Plugins/Android/libs and copied over the shared libnative-lib.so libraries armeabi-v7a and x86 .

I attached this script to an empty game object:

using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;

public class LibLoadTest : MonoBehaviour
{
    public Text debugText;

    [DllImport("native-lib")]
    private static extern string stringFromJNI();

    private void Start()
    {
        try
        {
             debugText.text = stringFromJNI();
        }
        catch(Exception exception)
        {
            debugText.text = exception.ToString();
        }
    }
}

My debugText shows "System.EntryPointNotFoundException: stringFromJNI at (wrapper managed-to-native) LibLoadTest::stringFromJNI() at LibLoadTest.Start() [0x00000] in :0" instead of "Hello from C++".

Does anyone know how to successfully use a native plug-in shared library for Android in Unity and where I'm going wrong in my process? Thanks.

I figured out the issue thanks to Bonfire-Boy on the Unity Answers forums: the name of the function I should have been trying to import was Java_com_example_willgauthier_stublibrary_MainActivity_stringFromJNI , not just stringFromJNI . I incorrectly assumed that because the long name was auto-generated and included the package (not sure that's the correct term) structure, that it could be ignored. Deleting everything before stringFromJNI in the library function definition worked, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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