javaandroidandroid-studiologcat

java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/appcompat/app/AppCompatActivity;


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.


Solution

  • 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.