我正在爲Android Studio中的Unity應用程序開發Android插件。該插件旨在訪問Google Fit API,並提供可在Unity應用程序中顯示的信息。我在下面包含了我的Java文件,以及本文末尾的我的Gradle文件和Manifest文件。Unity插件的NoClassDefFoundError和ClassNotFoundException
UnityPlayerActivity.java文件
package com.kasperiekqvist.player;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import com.google.android.gms.common.api.GoogleApiClient;
import com.unity3d.player.UnityPlayer;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UnityPlayerActivity extends com.unity3d.player.UnityPlayerActivity {
private GoogleApiClient mClient = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
logInUnity("onCreate", null);
super.onCreate(savedInstanceState);
checkPermissions();
}
/* SNIP */
private boolean checkPermissions() {
int permissionState = ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION);
boolean hasPermission = permissionState == PackageManager.PERMISSION_GRANTED;
logInUnity("checkPermissions", new String[] { "ACCESS_FINE_LOCATION:" + hasPermission });
return hasPermission;
}
private void logInUnity(String methodName, String[] linesToLog) {
if(methodName != null) {
String message = "UnityPlayerActivity:" + methodName + " was called at ";
message += new SimpleDateFormat("MM-dd HH:mm:ss").format(new Date());
if(linesToLog != null) {
for(int i = 0; i < linesToLog.length; i++) {
message += ";" + linesToLog[i];
}
}
UnityPlayer.UnitySendMessage("JavaCallback", "LogInUnity", message);
}
}
}
的logInUnity()
方法才能正確調用。我正在使用我的C#腳本中的LogInUnity()
方法中的Debug.Log()
方法記錄事物。使用命令adb logcat -s Unity delvikvm DEBUG
時,我可以在命令提示符中看到所有內容。
問題是訪問Android支持庫。當我在checkPermissions
方法中調用ActivityCompat.checkSelfPermission()
時,我的活動立即強制結束。我在下面列出了有關崩潰的信息。
崩潰信息
10-15 19:57:53.843 18197 18197 E AndroidRuntime: FATAL EXCEPTION: main
10-15 19:57:53.843 18197 18197 E AndroidRuntime: Process: com.kasperiekqvist.androidpluginproject, PID: 18197
10-15 19:57:53.843 18197 18197 E AndroidRuntime: java.lang.Error: FATAL EXCEPTION [main]
10-15 19:57:53.843 18197 18197 E AndroidRuntime: Unity version : 2017.2.0f3
10-15 19:57:53.843 18197 18197 E AndroidRuntime: Device model : OnePlus ONEPLUS A5000
10-15 19:57:53.843 18197 18197 E AndroidRuntime: Device fingerprint: OnePlus/OnePlus5/OnePlus5:7.1.1/NMF26X/09131759:user/release-keys
10-15 19:57:53.843 18197 18197 E AndroidRuntime:
10-15 19:57:53.843 18197 18197 E AndroidRuntime: Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/app/ActivityCompat;
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at com.kasperiekqvist.player.UnityPlayerActivity.checkPermissions(UnityPlayerActivity.java:74)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at com.kasperiekqvist.player.UnityPlayerActivity.onCreate(UnityPlayerActivity.java:27)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6743)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2848)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at android.app.ActivityThread.-wrap12(ActivityThread.java)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at android.os.Looper.loop(Looper.java:154)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6334)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.app.ActivityCompat" on path: DexPathList[[zip file "/data/app/com.kasperiekqvist.androidpluginproject-2/base.apk"],nativeLibraryDirectories=[/data/app/com.kasperiekqvist.androidpluginproject-2/lib/arm, /data/app/com.kasperiekqvist.androidpluginproject-2/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
10-15 19:57:53.843 18197 18197 E AndroidRuntime: ... 14 more
10-15 19:57:53.843 18197 18197 D AppTracker: App Event: crash
10-15 19:57:53.846 1804 16770 W ActivityManager: Force finishing activity com.kasperiekqvist.androidpluginproject/com.kasperiekqvist.player.UnityPlayerActivity
10-15 19:57:53.848 1804 16770 D ActivityTrigger: ActivityTrigger activityPauseTrigger
10-15 19:57:53.849 18197 18197 I Process : Sending signal. PID: 18197 SIG: 9
10-15 19:57:53.851 4741 6303 D OPReportService: addMonitorFolder onEvent [email protected]_57_53_851.txt, event:128
10-15 19:57:53.860 1804 4733 D EmbryoManager: prepare com.kasperiekqvist.androidpluginproject
10-15 19:57:53.860 1804 4733 I ActivityManager: Process com.kasperiekqvist.androidpluginproject (pid 18197) has died
10-15 19:57:53.860 1804 4733 D ActivityManager: get process top duration : com.kasperiekqvist.androidpluginproject, duration : 45
10-15 19:57:53.860 1804 4733 D ActivityManager: cleanUpApplicationRecord -- 18197
我已經包含在我的gradle這個文件的相關compile 'com.android.support:support-v4:25.3.1'
。根據我所做的研究,我認爲問題在於Android支持庫不包含在我的jar文件中,因爲它應該是。不過,我可能是錯的。
儘管如此,我沒有辦法嘗試。我發現堆棧溢出時出現了許多類似的問題,我試圖用這些問題的答案中提出的許多事情來解決我的問題。但是,到目前爲止,他們都沒有工作。
只是爲了澄清,在添加ActivityCompat.checkSelfPermission()
方法調用之前,一切正常。所以,問題必須在支持庫中。
的build.gradle文件
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "26.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services-fitness:11.0.4'
compile 'com.android.support:support-v4:25.3.1'
}
AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.kasperiekqvist.androidpluginproject" xmlns:tools="http://schemas.android.com/tools" android:versionName="1.0" android:versionCode="1" android:installLocation="preferExternal">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false" android:isGame="true" android:banner="@drawable/app_banner">
<activity android:name="com.kasperiekqvist.player.UnityPlayerActivity" android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
<meta-data android:name="unity.build-id" android:value="c9792a78-0123-4f4b-be86-1b85de56f689" />
<meta-data android:name="unity.splash-mode" android:value="0" />
<meta-data android:name="unity.splash-enable" android:value="True" />
<meta-data android:name="android.max_aspect" android:value="2.1" />
</application>
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="25" />
<uses-feature android:glEsVersion="0x00020000" />
<uses-feature android:name="android.hardware.vulkan" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
[爲什麼我得到一個可能的重複NoClassDefFoundError in Java?](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java) –
我無法解決我對這個問題的答案的問題。但是,我不認爲這是不可能的。我找到了自己的解決方案並將其包含在下面。 – Kasperi