- Home /
Getting native Android String constants via JNI broken in Unity 2019.2.0?
Hello there.
Code snippet which is in usage throughout Unity releases as of Unity 4 until today and which used to work fine looks like this:
AndroidJavaObject ajoConstant = new AndroidJavaClass("com.android.package.MyClass").GetStatic<AndroidJavaObject>("CONSTANT_NAME");
Natively in Android, I have this:
public class MyClass {
public static final String CONSTANT_NAME = "constant";
}
Well, it used to work until Unity 2019.2.0 release. Until this release, this string constant was without any issues read and I had its value in C#. However, seems like something has changed in JNI handling in Unity 2019.2.0 which causes return value always to be null. AndroidJavaClass is not null, it's being recognised, but static field not anymore.
Exactly same code snippet works with Unity 2019.1.9.
Does anyone have explanation for this behaviour?
Answer by Bunny83 · Aug 07, 2019 at 09:29 AM
Why do you return a "AndroidJavaObject"? If it's a string, use a string as type parameter. Also it's not clear what you originally did with your "AndroidJavaObject" instance. It's also not clear what Unity version you actually talk about since you did not specify the whole version string. Do you actually use a final / stable build (with an "f" in the version)? Or do you use an alpha ("a") or beta ("b") version? If it's just an issue with an alpha or beta version, this question does not belong here. If you participate in the beta program and you found something strange in an alpha or beta version, post it in the beta forum.
Finally make sure you really use the correct package and classname (if you really use "com.android.package.MyClass" you should change the package to your own namespace).
As I said your code should look like this:
string constant;
using(AndroidJavaClass MyClass = new AndroidJavaClass("com.android.package.MyClass"))
{
constant = MyClass.GetStatic<string>("CONSTANT_NAME");
}
Answer by nonelse · Aug 07, 2019 at 09:54 AM
Hi @Bunny83
And thanks for your answer. Also, sorry if I wasn't clear enough in my original post, so let me answer to all your questions.
AndroidJavaObject was state of code I encountered and honestly not sure why was that data type chosen. But the fact is that it used to work. I have tried with string parameter, it also returns null.
I am using Unity 2019.2.0f1, so the latest official one which is downloadable from Unity Hub and this is the first official Unity version where I have encountered this behaviour.
I have also tried 2019.3.0a11 version to see if this behaviour is present in upcoming version alpha and it is.
I am absolutely positive that I use correct package and class name, code from above is me obscuring the actual package and class name for simplicity sake.
I have tried exact same code snippet which you provided above and it works with official Unity versions prior to 2019.2.0f1 and with Unity 2019.2.0f1 it doesn't.
Hi, did you manage to make it work? I am experiencing the exact same problem. $$anonymous$$y code is similar to the code from @Bunny83 and it was working without problems in 2019.1.14f1. However, GetStatic returns null in 2019.2.2f1
Answer by gimoneto · Sep 15, 2019 at 01:54 PM
It seems to be a bug in Unity 2019.2 and later. It is registered in the Issue Tracker (ID 1179268): https://issuetracker.unity3d.com/issues/android-androidjavaclass-dot-getstatic-returns-null-when-being-built-to-android-device
Your answer
Follow this Question
Related Questions
AndroidJavaObject.Call fails with 'method not found' 1 Answer
What is the difference between AndroidJavaClass.Call and AndroidJavaObject.Call 2 Answers
Returning byte array from Java/Android 3 Answers
How to translate ArrayList to C# using AndroidJavaClass? 1 Answer
Can't access android mediastore 1 Answer