- Home /
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. 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.
nope, but reading the documentation is what I would do:
https://docs.unity3d.com/$$anonymous$$anual/AndroidNativePlugins.html
That's the thing: I downloaded the zip archive example linked at the bottom of that documentation page, and I got the same EntryPointNotFoundException from their code. Would you $$anonymous$$d seeing if the sample works for you?
The function names don't match, is that intentional?
I would expect it not to find the function if the names don't match, but I'm not sure what all the other stuff in the C++ declaration is for. i.e not sure why it doesn't just look like this...
extern "C" stringFromJNI(... ){...}
That was it - thanks Bonfire-Boy! I didn't really know what all the Android/Java package name stuff in the C++ declaration was for either. I thought because it was auto-generated it had to be there and didn't count as part of the function name. Taking it out worked like a charm, though.
Your answer
Follow this Question
Related Questions
How to write an AndroidManifest.xml combining different plugins? 3 Answers
Android Java Plugin For Unity Object with null ptr! 0 Answers
Xamarin Android Plugins for Unity 0 Answers
Using AndroidJavaClass in a java Unity3d plugin 0 Answers
Launching an Android application from within unity without extending UnityPlayerActivity? 1 Answer