Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nonelse · Aug 05, 2019 at 12:02 PM · androidandroidjavaobjectjni

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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");
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gimoneto · Aug 24, 2019 at 06:49 AM 0
Share

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

avatar image nonelse gimoneto · Sep 04, 2019 at 12:55 PM 0
Share

Hey @gimoneto - nope I didn't. Wasn't checking it in subsequent versions, rather updated the library I am making to work around this limitation. Still curious to hear if @Bunny83 has any advice what might be happening.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

239 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges