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 The right way to extend UnityPlayerActivity in a jar plugin?

Discussion in 'Android' started by geniusz, Jan 5, 2022.

  1. geniusz

    geniusz

    Joined:
    Nov 21, 2014
    Posts:
    38
    In Unity 5 I just need add classes.jar in dependecies, then I can create an Activity class to extend UnityPlayerActivity.

    Now in Unity 2020 UnityPlayerActivity becomes a java file. If I copy this file to my plugin project, this class will be included in the output jar file. But there will be a class conflict when building apk.

    So I have to extract the jar, delete UnityPlayerActivity.class, and pack other files again. I don't think this is the right way to build a plugin. Is there a workaround?
     
    Last edited: Jan 5, 2022
    nightqnearth likes this.
  2. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,832
    Unity's classes.jar already have UnityPlayerActivity.class file.
    Just include classes.jar as dependency (under libs folder) and you don't need to do anything extra.

    Code (build.gradle):
    1. dependencies {
    2.     implementation fileTree(include: ['*.jar'], dir: 'libs')
    3.     ....
    4. }
     
    nightqnearth likes this.
  3. geniusz

    geniusz

    Joined:
    Nov 21, 2014
    Posts:
    38
    Sorry I forgot to mention that I'm using Unity 2020, Is UnityPlayerActivity.class added into classes.jar in Unity 2022?
     
    nightqnearth likes this.
  4. geniusz

    geniusz

    Joined:
    Nov 21, 2014
    Posts:
    38
    Could you tell me which version and where is the classes.jar that contains UnityPlayerActivity.class?

    I've just downloaded Unity 2021.2.7f1, and I've checked classes.jar in following path
    • Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Release\Classes
    • Editor\Data\PlaybackEngines\AndroidPlayer\Variations\il2cpp\Development\Classes
    • Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes
    • Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Development\Classes
    Unfortunately UnityPlayerActivity.class is in none of them.
     
    nightqnearth likes this.
  5. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,832
    Ok, I got the problem now.
    I'm currently using old unity version's jar(2018.4) so I don't see such problem. However, As its just for reference alone, it should be fine.

    However, if you like to do it, there are two options
    1. Create a new android library project from classes.jar and the UnityPlayerActivity.java file and use it as a dependency module.
    2. Include the UnityPlayerActivity.java and write a gradle task to exclude that class file in the final archive (as you mentioned earlier)
     
    Last edited: Jan 5, 2022
    nightqnearth likes this.