Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do you add an Activity file to an Android plugin?

Discussion in 'Documentation' started by AveSeca, Mar 7, 2023.

  1. AveSeca

    AveSeca

    Joined:
    Dec 6, 2022
    Posts:
    2
    I'm using Unity 2021.3.15f1 version, and trying to build an Android game which uses native Android functionality.

    The documentation on Create a custom activity for Android builds says:
    Then it says that the Activity file should be created on the Assets folder, inside the Unity project. But later it also says that you should "Create a new plug-in and add the Activity file to it." But how I do this?

    The docs say the plugin should be either an Android Library Project, an Android Archive (an already built AAR file), or a JAR. How can I add the activity file to an AAR/JAR file, if they are already built when inside the Unity project?

    The Android Library Project option doesn't tell me much about how to do, only to copy the android library project into the Assets folder and add an extension to its name (although it recommends to use the Android Archives instead). In this case, should I copy the entire Android Studio project into the Unity project's Assets folder? If not, which files should be copied?

    I also tried to create the activity inside the library on the Android Studio project and build the AAR file to import it at Unity later. However, it gives errors from not finding com.unity3d.player.UnityPlayerActivity (since it indeed only exists on Android Studio projects exported by Unity).

    So what exactly should I be doing to create the Android plugin and extend the UnityPlayerActivity? What file/folder goes where?
     
    hamza_unity995 likes this.
  2. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    835
    Hi,
    thanks for pointing out the lack of clarity in our documentation. If you just want to do this in your project, you don't necessarily have to create a plugin. You can just leave java file in the assets directory and allow Unity to pick it up as a java source plugin https://docs.unity3d.com/Manual/android-java-and-kotlin-plugins-create.html However note that in this case you will have to overwrite the manifest file inside of the Unity editor - follow the instructions to enable manifest template file and do modifications in there.
    It's also possible to do this as a plugin. AAR or Android library are recommended options, because there you can include the manifest file together with the code. For library plugin, you should be able to copy the whole Android Studio project as described in the manual page you've linked yourself. To allow Android Studio to find UnityPlayerActivity, you can reference Editor\Data\PlaybackEngines\AndroidPlayer\Source\com\unity3d\player\UnityPlayerActivity.java file from Unity install directory.
     
    AveSeca likes this.
  3. AveSeca

    AveSeca

    Joined:
    Dec 6, 2022
    Posts:
    2
    Thanks, the approach of leaving the Java files in the Assets directory and overwriting the manifest file on Unity worked here.

    When I first read about the Java source plugins, I wrongly assumed that with every file being an individual plugin, they wouldn't be able to call functions on each other (since my Android code used multiple classes), but this is not the case, when using the option to export an Android Studio project from the Unity one, I could see them all on their respective packages and they had access to each other. So I could have both my custom Activity and some other Java classes at the Assets folder.

    Anyway, Java source files are not on the list of plugin options for containing the custom Activity, so could you include them on it? Right now, it looks like it can't be done. I also don't know why only Android Library Projects and Android Archive plugins are suggested for reuse and distribution, since you can theoretically export your Java files as Asset packages. Because the Java/Kotlin source plugins seem easier to maintain, since they are only source code, and the only extra job you get is customizing the Gradle files for adding eventual dependencies.
     
    hamza_unity995 likes this.