- Home /
Starting a activity in Android Library
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.
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");
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.
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).
Answer by nipundavid · Jun 20, 2017 at 10:12 AM
Any luck ? I am stuck with same sort of issue. I am trying to invoke native permission dialog box from unity3D.
So far I have found no way to do this. Link - http://www.techotopia.com/index.php/Making_Runtime_Permission_Requests_in_Android_6.0
Answer by mrttson · Nov 08, 2017 at 05:31 PM
I've same issue. When i changed base class of MainActivity from AppCompatActivity to Activity, it's seem resolve the issue, but on my layout of activity contains class from facebook SDK ("com.facebook.login.widget.LoginButton"), then ClassNotFoundException occurs again. So sad ...
Your answer
Follow this Question
Related Questions
Android Plugin OnCreate() function? (Unity project automatically open android peoject) 0 Answers
How do I override an Android Java class in Unity? 1 Answer
How do I make an Android plugin for unity? 0 Answers
How to pass accurate GPS location from android studio to unity? 0 Answers
Not rendering camera when using Unity as library on Android Studio 0 Answers