如何解决java.lang.NoClassDefFoundError:解析失败:Landroidx/appcompat/app/AppCompatActivity;
我有一个带有 android studio 插件的统一应用程序,当我按下按钮时,它会在我的手机上加载 android 相机应用程序......这里是 java 类:
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);
}
}
这里是我的构建 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'
}
当我启动我的统一应用程序时,它没有崩溃,但我在 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
请帮忙,我需要一些反馈...我读到这是因为 aar 文件不包含我们在 gradle 文件中添加的外部依赖项,但是我在创建插件时没有错误。 .. 请帮忙...提前谢谢你。
解决方法
当您编译您的插件时,它不包含“androidx.appcompat.app.AppCompatActivity”您必须将该包添加到您的 Plugin/Android 文件夹中。有两种方法可以解决这个问题;
1-External Dependency Manager for Unity!此插件有助于添加外部依赖项。
2-您可以在 Android 开发者网站上找到依赖项 .jar 或 .aar。
我推荐第一个选项。因为它还处理 cocoapods 依赖项。
或
如果您不需要任何依赖项,请仅使用“Activity”扩展您的类。如果您的函数包含在 Activity 类中,则不必添加任何依赖项。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。