我在Unity 2017.3 Beta 1中开发了一个Android应用程序(让我们称之为 AppName )。我需要一个自定义Activity来在Startup上运行一些Java代码。我在一个单独的项目中有AppNameActivity
的代码,该项目使用gradle
将Activity打包到AppName.jar
文件中,然后将其放入Unity的AppName/Assets/Plugins/Android
文件夹中,以及引用它的自定义AndroidManifest.xml
。我使用Unity的构建和运行功能(构建系统:Gradle,开发构建)。
当我在Android设备上启动应用时,它会立即崩溃。似乎缺少Activity类,但仔细观察后,实际上就是那里。
通过adb logcat
我可以获得此错误:
V/ActivityThread(24831): Handling launch of ActivityRecord{2609126c token=android.os.BinderProxy@2101f035 {de.companyname.appname.rebuild/de.companyname.appname.rebuild.AppNameActivity}}
D/OpenGLRenderer(23986): Flushing caches (mode 0)
D/OpenGLRenderer(23986): Flushing caches (mode 0)
I/art (24831): Rejecting re-init on previously-failed class java.lang.Class<de.companyname.appname.rebuild.AppNameActivity>
D/AndroidRuntime(24831): Shutting down VM
E/AndroidRuntime(24831): FATAL EXCEPTION: main
E/AndroidRuntime(24831): Process: de.companyname.appname.rebuild, PID: 24831
E/AndroidRuntime(24831): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{de.companyname.appname.rebuild/de.companyname.appname.rebuild.AppNameActivity}: java.lang.ClassNotFoundException: Didn't find class "de.companyname.appname.rebuild.AppNameActivity" on path: DexPathList[[zip file "/data/app/de.companyname.appname.rebuild-2/base.apk"],nativeLibraryDirectories=[/data/app/de.companyname.appname.rebuild-2/lib/arm, /vendor/lib, /system/lib]]
E/AndroidRuntime(24831): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2381)
E/AndroidRuntime(24831): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2532)
E/AndroidRuntime(24831): at android.app.ActivityThread.access$900(ActivityThread.java:169)
E/AndroidRuntime(24831): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
E/AndroidRuntime(24831): at android.os.Handler.dispatchMessage(Handler.java:111)
E/AndroidRuntime(24831): at android.os.Looper.loop(Looper.java:194)
E/AndroidRuntime(24831): at android.app.ActivityThread.main(ActivityThread.java:5562)
E/AndroidRuntime(24831): at java{de.companyname.appname.rebuild/de.companyname.appname.rebuild.AppNameActivity}.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(24831): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(24831): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:968)
E/AndroidRuntime(24831): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763)
E/AndroidRuntime(24831): Caused by: java.lang.ClassNotFoundException: Didn't find class "de.companyname.appname.rebuild.AppNameActivity" on path: DexPathList[[zip file "/data/app/de.companyname.appname.rebuild-2/base.apk"],nativeLibraryDirectories=[/data/app/de.companyname.appname.rebuild-2/lib/arm, /vendor/lib, /system/lib]]
E/AndroidRuntime(24831): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E/AndroidRuntime(24831): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/AndroidRuntime(24831): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
E/AndroidRuntime(24831): at android.app.Instrumentation.newActivity(Instrumentation.java:1070)
E/AndroidRuntime(24831): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2371)
E/AndroidRuntime(24831): ... 10 more
E/AndroidRuntime(24831): Suppressed: java.lang.NoClassDefFoundError: de.companyname.appname.rebuild.AppNameActivity
E/AndroidRuntime(24831): at dalvik.system.DexFile.defineClassNative(Native Method)
E/AndroidRuntime(24831): at dalvik.system.DexFile.defineClass(DexFile.java:226)
E/AndroidRuntime(24831): at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
E/AndroidRuntime(24831): at dalvik.system.DexPathList.findClass(DexPathList.java:321)
E/AndroidRuntime(24831): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
E/AndroidRuntime(24831): ... 14 more
E/AndroidRuntime(24831): Suppressed: java.lang.ClassNotFoundException: de.companyname.appname.rebuild.AppNameActivity
E/AndroidRuntime(24831): at java.lang.Class.classForName(Native Method)
E/AndroidRuntime(24831): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
E/AndroidRuntime(24831): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
E/AndroidRuntime(24831): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
E/AndroidRuntime(24831): ... 13 more
E/AndroidRuntime(24831): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
AndroidManifest.xml
的相关部分:
<application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:isGame="false" android:banner="@drawable/app_banner">
<activity 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" android:name="de.companyname.appname.rebuild.AppNameActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
(...)
</application>
这是dexdump
的输出,在Android设备上通过adb shell
使用参数/data/app/de.companyname.appname.rebuild-2/base.apk
执行。
Processing './base.apk'...
Opened './base.apk', DEX version '035'
(...)
Class #69 -
Class descriptor : 'Lde/companyname/appname/rebuild/AppNameActivity;'
Access flags : 0x0001 (PUBLIC)
Superclass : 'Lde/companyname/appname/rebuild/UnityPlayerActivity;'
Interfaces -
Static fields -
#0 : (in Lde/companyname/appname/rebuild/AppNameActivity;)
name : 'ACTION_USB_PERMISSION'
type : 'Ljava/lang/String;'
access : 0x001a (PRIVATE STATIC FINAL)
#1 : (in Lde/companyname/appname/rebuild/AppNameActivity;)
name : 'TAG'
type : 'Ljava/lang/String;'
access : 0x001a (PRIVATE STATIC FINAL)
Instance fields -
#0 : (in Lde/companyname/appname/rebuild/AppNameActivity;)
name : 'mUsbManager'
type : 'Landroid/hardware/usb/UsbManager;'
access : 0x0002 (PRIVATE)
#1 : (in Lde/companyname/appname/rebuild/AppNameActivity;)
name : 'mUsbReceiver'
type : 'Landroid/content/BroadcastReceiver;'
access : 0x0012 (PRIVATE FINAL)
#2 : (in Lde/companyname/appname/rebuild/AppNameActivity;)
name : 'validProductNames'
type : 'Ljava/util/Collection;'
access : 0x0002 (PRIVATE)
Direct methods -
#0 : (in Lde/companyname/appname/rebuild/AppNameActivity;)
name : '<init>'
type : '()V'
access : 0x10001 (PUBLIC CONSTRUCTOR)
code -
registers : 4
ins : 1
outs : 2
insns size : 32 16-bit code units
catches : (none)
positions :
0x0000 line=27
0x0003 line=22
0x000a line=25
0x0018 line=28
0x001f line=29
locals :
0x0000 - 0x0020 reg=3 this Lde/companyname/appname/rebuild/AppNameActivity;
(...)
我还使用工具AndroidSdk/tools/bin/apkanalyzer dex packages ~/src/appname/UnityExport.apk
在我的开发机器上进行反汇编,产生:
sh: 0: getcwd() failed: No such file or directory
P d 465 944 66105 <TOTAL>
P d 420 454 49362 com
(...)
C d 8 10 1067 de.companyname.appname.rebuild.AppNameActivity
M d 1 1 119 de.companyname.appname.rebuild.AppNameActivity <init>()
(...)
所以我很确定设备上存在班级de.companyname.appname.rebuild.AppNameActivity
。
更新
我担心错误日志中连续两次提到的包:{de.companyname.appname.rebuild/de.companyname.appname.rebuild.AppNameActivity}
所以我将清单中的值从android:name="de.companyname.appname.rebuild.AppNameActivity"
更改为android:name=".AppNameActivity"
。不过,我仍然完全相同的错误消息。
答案 0 :(得分:0)
我找到了解决自己问题的方法:我错过了一个超类。
当我尝试制作发布版本(之前,我总是尝试 debug build )时,我收到了几条错误消息,包括这一行:
Warning: de.companyname.appname.rebuild.AppNameActivity: can't find superclass or interface de.companyname.appname.rebuild.UnityPlayerActivity
事实上,我在捆绑AppName.jar
时犯了一个错误,因此缺少了超类。我仍然对AndroidRuntime中仅提及AppNameActivity
的非特定运行时错误感到惊讶。
我重建了jar
和发布版本,现在修复了这个错误,加载了AppNameActivity
并执行了构造函数(然后由于其他一些问题而在执行期间崩溃超出范围的问题。)
有一会儿,我仍然不确定这件事,因为我看了dexdump
的旧输出(我在我的问题中部分引用了),证实了{ {1}}。经过仔细检查,我注意到UnityPlayerActivity
被包含在内,但实际的超类是com.unity3d.player.UnityPlayerActivity