1

I have unity app with android studio plugin, that load android camera app on my phone when I press the button... Here java class:

 import android.content.Intent;
 import android.os.Bundle;
 import android.provider.MediaStore;
 import androidx.appcompat.app.AppCompatActivity;


 public class LoadAndroidApp extends AppCompatActivity 
 {
     @Override
     protected void onCreate(Bundle savedInstanceState) 
   {

     super.onCreate(savedInstanceState);
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
     startActivity(intent);
   }
 }

here my build gradle:

    apply plugin: 'com.android.library'

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
    minSdkVersion 24
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  }
}

dependencies 
{
  implementation fileTree(dir: "libs", include: ["*.jar"])
  implementation  'androidx.appcompat:appcompat:1.1.0-alpha04'
  testImplementation 'junit:junit:4.12'
  androidTestImplementation 'androidx.test.ext:junit:1.1.2'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

When I launch my unity app, it doesn't crush, but I have this error in logcat:

 2021-04-27 18:45:26.327 27678-27700/? E/Unity: AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/appcompat/app/AppCompatActivity;
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/appcompat/app/AppCompatActivity;
    at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    at com.unity3d.player.UnityPlayer.access$300(Unknown Source:0)
    at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:95)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:241)
    at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)
 Caused by: java.lang.ClassNotFoundException: androidx.appcompat.app.AppCompatActivity
    at com.unity3d.player.UnityPlayer.nativeRender(Native Method) 
    at com.unity3d.player.UnityPlayer.access$300(Unknown Source:0) 
    at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:95) 
    at android.os.Handler.dispatchMessage(Handler.java:103) 
    at android.os.Looper.loop(Looper.java:241) 
    at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20) 
  at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.AndroidJNISafe.FindClass (System.String name) [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.AndroidJavaClass._AndroidJavaClass (System.String className) [0x00000] in <00000000000000000000000000000000>:0 

please, help, I need some feedback... I read that This is happening because the aar file doesn't contain external dependencies that we add in gradle file, but I didn't have error when I created my plugin... Please, help... Thank you in advance.

Hope1_
  • 25
  • 7

1 Answers1

1

When you compile your plugin it does not include "androidx.appcompat.app.AppCompatActivity" You have to add that package to your Plugin/Android folder. There is two way to fix this;

1-External Dependency Manager for Unity! This plugin helps to add external dependencies.

2-You can find dependency .jar or .aar on Android Developer website.

I recommend first option. Because it also handles cocoapods dependencies.

OR

If you don't want any dependencies, extend your class with just "Activity". If your functions included in Activity class you do not have to add any dependency.

4litasci
  • 26
  • 2
  • Hello! Thank you a lot for your answer! may I get a little clarification(about "Activity"): I must import class in my java code like this?: import android.app.Activity; and in my class name change something like this?:public class LoadAndroidApp extends Activity{ } Thank You again!=) – Hope1_ Apr 28 '21 at 13:40
  • @Hope1_ Yes, You can do like that. if you extend your class with Activity, You just don't need extra dependency. Do not forget to send "unity mainactivity" to your class. Your plugin mostly work on unity's android activity. You can make plugins in 2 ways. Extend UnityPlayerActivity and use your "Extended new activity" as main activity [Extending](https://docs.unity3d.com/Manual/AndroidUnityPlayerActivity.html). Other method is making aar library [AAR Library](https://docs.unity3d.com/Manual/AndroidAARPlugins.html). – 4litasci Apr 28 '21 at 18:57
  • Thank you for your answers! I'm right understand about plugin(from github): I must import external-dependency-manager-*.unitypackage in my Unity project, and add external-dependency-manager-*.unitypackage and SampleDependencies.xml to my android studio project, for everything to work?(my english very bad, I am from Ukraine, sorry) – Hope1_ Apr 29 '21 at 14:59
  • or all I must do in android studio project? – Hope1_ Apr 30 '21 at 08:05