I had an existing Android Studio Project with one Activity Class and lots of other Java classes. I converted that to a library project by changing the apply plugin: 'com.android.application' to apply plugin: 'com.android.library' in the build.gradle(Module: app) file.
apply plugin: 'com.android.application'
apply plugin: 'com.android.library'
The generated .aar file was imported to Unity. I am able to call the functions in the normal java classes in the plugin but unable to start the activity in the plugin. This is how I am trying to access the MainActivity class AndroidJavaClass myAndroidClass = new AndroidJavaClass("com.companyName.appname.MainActivity");
AndroidJavaClass myAndroidClass = new AndroidJavaClass("com.companyName.appname.MainActivity");
I have changed the Manifest in the plugin to specify exact path for the class com.companyName.appname.MainActivity
This always fails with AndroidJavaException: java.lang.ClassNotFoundException: com.companyName.appname.MainActivity for the MainActivity class but works fine for the regular java classes in the plugin.
AndroidJavaException: java.lang.ClassNotFoundException: com.companyName.appname.MainActivity
I also found that there are ways to start the same by extending activity as UnityPlayerActivity in the Android studio code but wanted some direction before diving into that as I am unable to figure out why Unity can find the regular java classes but not the MainActivity?
Can anyone please suggest how to resolve this? My end goal is to convert the Android Studio project to a library and to start the activity from Unity.
You should make sure that:
1. The activity class does get included into the .aar (it's a simple zip file. unzip it and inspect the classes.jar file under it using some decompilation tool, like JD-GUI).
2. You have included other dependencies that this activity relies on (Android support library ? appcompat? other libraries)
3. The name is spelled correctly and exactly as in the manifest.
As a side note, in order to start an activity, you should call startActivity, you never call the activity's constructor like you would with other plain Java classes (Android will construct it for you).
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
4 People are following this question.
How to include assets in an android plugin (aar)?
IAP Plugins on Android Unity Not Working For Me
How to convert wav to flac on Android?
How to alter the Android Manifest to use my ad-displaying activity in Unity?
BroadcastReceiver onReceive does not work (custom Unity Android plugin)