Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Android java source plugin import com.unity3d.player.UnityPlayer?

Discussion in 'Android' started by hansadler, Jan 31, 2023.

  1. hansadler

    hansadler

    Joined:
    May 12, 2014
    Posts:
    50
    I'm trying to write a java source only plugin as described here:
    https://docs.unity3d.com/Manual/AndroidJavaSourcePlugins.html

    import com.unity3d.player.UnityPlayer;

    I want to import and use the UnityPlayer class in my plugin code, but I can't figure out how to get it imported successfully. Any idea how this can be done?

    I know I can do this with a aar plugin, but I want the simplicity of compiling the java source in Unity instead of making a library project with Android Studio and dragging it into the project.
     
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    Simply create a java source inside Unity project, it will then get copied over to gradle project.
     
  3. hansadler

    hansadler

    Joined:
    May 12, 2014
    Posts:
    50
    I don't think you read my question. I already tried what you said.

    The problem is that I need to use classes from com.unity3d.player.UnityPlayer. I imported that package in my java file, and compiled for Android, and got this error.

    package com.unity3d.player does not exist
    import com.unity3d.player.UnityPlayer;
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,638
    The easiest way is to export Android Studio project from Unity and write the Java file there. Later copy that file to Unity project.
     
  5. hansadler

    hansadler

    Joined:
    May 12, 2014
    Posts:
    50
    Are you saying there's no way to import com.unity3d.player.UnityPlayer in a java source only plugin?
     
  6. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    Few clarifications:
    • The package is called com.unity3d.player, UnityPlayer is a class located in package with the name 'com.unity3d.player'.
    • If you use java source plugin, you don't need to import anything, since that java file is already part of com.unity3d.player package.
    • You can edit that java file in any text editor you want, but it's more convient to do it in Android Studio, since after making changes you can quickly check if it compiles and you can actually test the code quickly by running it on device
    • If you'll decide to edit the java file in Android Studio, select Export Project in Unity build settings window, export a gradle project and open it in Android Studio
    • Additionally you might want to select Symlink Sources, this will tell gradle to reference java files from within Unity project, this useful since you won't need to copy java file back from gradle project to Unity project after doing any modifications. It might be difficult to find the java in Android Studio solution explorer, thus find it by using Ctrl + Shift + N and enter Test.java after gradle project is imported.

    Here's a simply Test.java file which you can simply throw in Unity project, as you see it can reference UnityPlayer class just fine and it's inside com.unity3d.player package.
    Code (CSharp):
    1. package com.unity3d.player;
    2.  
    3. public class Test
    4. {
    5.     public void Foo(UnityPlayer test)
    6.     {
    7.    
    8.     }
    9. }
     
    Last edited: Jan 31, 2023
  7. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,638
    I'd advise against putting your files to com.unity3d.player package, that's a Unity package.
    I guess I misunderstood what the exact problem is. For sure your .java file should be able to import UnityPlayer class. Could you share more details, such as how your java source looks like and what error you get?
     
  8. hansadler

    hansadler

    Joined:
    May 12, 2014
    Posts:
    50
    I thought it was a little strange that you were saying I should put my class into the com.unity3d.player package. It seemed like you were saying that's what I should do.

    Anyway, I was able to get it working by just placing the java file into my project -- as you said. I also tried using my own package name, and that worked. I think the problem was that I thought I was supposed to create the package directory structure in the unity project java/com/domain/package/etc. When I did that, the unity android compile wasn't able to import the com.unity3d.player package classes that I needed to use in my java file. It would be really good if Unity could more clearly document creating a java source only plugin. I wasn't able to figure it out from the existing documentation and I was left trying to piece it together from random blog posts -- some of which gave me incorrect or outdated information.

    Thanks for your help.
     
  9. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,638
    Unity copies java files into gradle project to the unityLibrary project. Normally the should just work. We do not require java file to be in package directory tree, we scan the file and try to parse the package from it.

    One thing that catche my eye is you mentioning "java" in directory tree. I'm not sure you can put stuff to such package and that may cause problem as it is also a predefine directory in the gradle project. A completely lose .java file should work just fine. The one in correct directory tree should work too.

    Typical placement is either of these two:
    Plugins/Android/MyFile.java
    Plugins/Android/com/domain/package/subpack/MyFile.java

    It is not requires to put things to Plugins/Android, but for good for project structure.
     
  10. MonkeyYao

    MonkeyYao

    Joined:
    Jul 3, 2012
    Posts:
    18
    Can you help me modify the code? TestJavaSource in c# cannot execute java function successfully.
    c# code:
    Code (CSharp):
    1.  
    2. public void TestToast(string t)
    3. {
    4.     Debug.LogWarning("start test toast:" + t);
    5.     AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    6.     AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    7.     AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast");
    8.     unityActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
    9.     {
    10.         AndroidJavaObject toastObject = toastClass.CallStatic<AndroidJavaObject>("makeText", unityActivity, t, 3);
    11.         toastObject.Call("show");
    12.         Debug.LogWarning("start test toast  show on ui thread");
    13.     }));
    14. }
    15.  
    16. public void TestJavaSource()
    17. {
    18.     AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    19.     AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    20.     // unityActivity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
    21.     // {
    22.         AndroidJavaClass testClass = new AndroidJavaClass("com.test.MyJavaTestClass");
    23.         testClass.Call("showToast", unityActivity, "test java call toast====================");
    24.         // testClass.Call("testLog", "test java call log====================");
    25.         // testClass.Call("testCallUnity");
    26.         Debug.LogWarning("test java call");
    27.     // }));
    28. }
    29.  
    30. void JavaMessage(string message) {
    31.     Debug.Log("message from java: " + message);
    32. }
    33.  
    The java source "MyJavaTestClass.java" in the "Assets/Plugins/Android" folder
    Code (JavaScript):
    1.  
    2. package com.test;
    3.  
    4. import android.content.Context;
    5. import android.widget.Toast;
    6. import android.util.Log;
    7. import com.unity3d.player.UnityPlayer;
    8. public class MyJavaTestClass{
    9.     public void showToast(Context act, String msg){
    10.         Toast.makeText(act, msg, 3).show();
    11.         Log.w("java MyJavaTestClass", "showToast");
    12.     }
    13.     public void testLog(String str){
    14.         Log.w("java MyJavaTestClass", "testLog:"+str);
    15.     }
    16.  
    17.     public void testCallUnity(){
    18.         UnityPlayer.UnitySendMessage("main", "JavaMessage", "testCallUnity");
    19.     }
    20. }
    21.  
    player setting:
    2023-02-08_192736.jpg 2023-02-08_192802.jpg
     
    Last edited: Feb 8, 2023
  11. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,638
    Two ways of fixing this:
    1. Make methods in your MyJavaTestClass static and use CallStatic instead of Call in C#
    2. Replace AndroidJavaClass with AndroidJavaObject on this line:
    Code (CSharp):
    1. AndroidJavaClass testClass = new AndroidJavaClass("com.test.MyJavaTestClass");
    The second change will create a new Java object of this class and call methods on the instance.
     
  12. MonkeyYao

    MonkeyYao

    Joined:
    Jul 3, 2012
    Posts:
    18
    Thank you for your reply, it solved the problem that troubled me for two days.