I have a Unity game which uses an Android Library packed in an AAR (library.aar). However, I need to access the Main Activity's lifecycle callbacks (onPause(), onResume(), to name a few).
So I created a new library project in Android Studio (let's call this plugin). I imported library.aar into plugin and added it to the Dependencies. I tested it by calling the functions inside library.aar inside MainActivity. It doesn't throw any errors at this point.
In MainActivity, I called Library.onActivityResult(), Library.onResume(), and Library.onPause() in the respective functions.
Next, I added the classes.jar from Unity/Editor/Data/PlaybackEngines/androidplayer/release/bin into my plugin project. I added the JAR into the dependencies. I tested it by trying to subclass UnityPlayerActivity. Again, no errors yet.
I compile plugin into plugin-debug.aar and put it in Assets/Plugins/Android. I also created an AndroidManifest.xml which I got from the Unity Manual and put it inside Assets/Plugins.
I call the functions from a button in a Unity scene like so:
AndroidJavaClass jc = new AndroidJavaClass("com.alxcyl.plugin.MainActivity");
jc.CallStatic("connect");
// "connect" is a static function in the Java class MainActivity containing
// a short message like so:
// Log.d("My Plugin", "connect() was called");
I build the game and run it on my Android device. If I click the button, MyPlugin.connect() is properly called since it logs the message it's supposed to when clicked.
I think the lifecycle callbacks are properly called but the problem is that it logs an error saying:
I/dalvikvm( 5466): Could not find method com.somecompany.library.Library.onActivityResult, referenced from method com.alxcyl.plugin.MainActivity.onActivityResult
W/dalvikvm( 5466): VFY: unable to resolve virtual method 276: Lcom/somecompany/library/Library;.onActivityResult (IILandroid/content/Intent;)V
I/dalvikvm( 5466): Could not find method com.somecompany.library.Library.onPause, referenced from method com.alxcyl.plugin.MainActivity.onPause
W/dalvikvm( 5466): VFY: unable to resolve virtual method 276: Lcom/somecompany/library/Library;.onPause()V
I/dalvikvm( 5466): Could not find method com.somecompany.library.Library.onResume, referenced from method com.alxcyl.plugin.MainActivity.onResume
W/dalvikvm( 5466): VFY: unable to resolve virtual method 276: Lcom/somecompany/library/Library;.onResume()V
D/MyPlugin( 5466): connect() was called
I'm not sure what causes it. When you build the plugin project, it builds fine without any errors but when you access it through Unity, it doesn't work. I tried to use the same library.aar into a new Android project and Library.onActivityResult(), Library.onPause(), and Library.onResume() definitely exists and are properly called.
Could it be that Unity can't handle AARs that include AARs? Is there a fix for this?