RuntimeException仅在生产版本中加载应用程序
我遇到了一个我正在创建的应用程序的问题。基本上,应用程序会在您第一次尝试打开它时崩溃,然后它就会正常运行。令人困惑的部分只有当您从Google Play下载应用时才会发生。如果我直接从Android Studio将应用加载到手机上,则不会收到任何错误。RuntimeException仅在生产版本中加载应用程序
java.lang.RuntimeException:
at android.app.ActivityThread.handleReceiver (ActivityThread.java:3290)
at android.app.ActivityThread.-wrap20 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1715)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6682)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1410)
Caused by: java.lang.ClassNotFoundException:
at dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass (ClassLoader.java:380)
at java.lang.ClassLoader.loadClass (ClassLoader.java:312)
at android.app.ActivityThread.handleReceiver (ActivityThread.java:3285)
在研究了这个问题之后,我发现有人说要清除设备的缓存。这解决了这个问题......但这只是抹去了应用程序,并从那时起工作正常。初始安装仍然崩溃。这发生在多个设备上,但我无法在调试时重现它。我唯一的想法是,在我调用setContentView()之后,我在onCreate()中有这个代码。
Intent intent = new Intent(this, NotificationListener.class);
startService(intent);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
NotificationListener.class extends NotificationListenerService。除了最初的发布之外,一切都很好。
UPDATE
这里是我设置的AndroidManifest服务:
<service android:name=".NotificationListener"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
这里是我的NotificationListenerService的简化版本:
public class NotificationListener extends NotificationListenerService
{
private IBinder mBinder = new LocalBinder();
@Override
public void onNotificationPosted(StatusBarNotification sbn)
{
super.onNotificationPosted(sbn);
// custom code
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn)
{
super.onNotificationRemoved(sbn);
}
@Override
public IBinder onBind(Intent intent)
{
if (SERVICE_INTERFACE.equals(intent.getAction()))
return super.onBind(intent);
else return mBinder;
}
public class LocalBinder extends Binder
{
public NotificationListener getService()
{
return NotificationListener.this;
}
}
}
下面是ServiceConnection粘结剂:
private ServiceConnection serviceConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder)
{
NotificationListener.LocalBinder localBinder = (NotificationListener.LocalBinder) iBinder;
notificationListener = localBinder.getService();
bound = true;
// code to call custom function in NotificationListener class
}
@Override
public void onServiceDisconnected(ComponentName componentName)
{
notificationListener = null;
bound = false;
}
};
我知道这必须是服务绑定的问题,因为如果我删除所有的绑定代码并启动服务,则所有事情都按照它应该运行。只有当我添加绑定代码,我得到这个错误。
看来你不需要再次启动和绑定服务。请删除以下代码后创建签名apk文件。
Intent intent = new Intent(this, NotificationListener.class);
startService(intent);
[编辑]
我认为这是混淆proguard的,所以请您在以下代码添加到您的ProGuard文件。
# Base Android exclusions, required for proper function of various components
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.preference.Preference
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Fragment
通常这个问题是由于proguard发生的,如果你将proguard添加到你的项目中,它会停止一些这样的功能。
刚刚尝试通过增加添加您的服务模块中proguard-rules.pro文件
-keep类{包名} {服务模块名} ** {*。 }
例如:> -keep class com.demo.service。** {*; }
这将在proguard中添加所有服务权限。
我可以只删除proguard,如果它导致问题? – saboehnke
每一件事情都有自己的优点和缺点,Proguard也会为你提供很好的安全性和许多其他功能。 所以,我的意见是将此文件添加到proguard-rules.pro文件,而不是完全删除它。 –
ClassNotFoundException
这里有一个空的消息。查看dalvik.system.BaseDexClassLoader
源代码,如果它找不到它,它会抛出一个带有指定类名称的ClassNotFoundException
,因此,您在某处传递了一个空的类名称(即在由ActivityThread::handleReceiver
处理的消息中)如果你可以提供完整的堆栈跟踪(即handleMessage
分支代表这个handleReceiver
调用)
你可以尝试创建没有proguard的apk文件吗? – kimkevin
@KimKevin这是否只涉及删除条目在应用程序gradle中的buildTypes?如果是这样,我删除它,并上传了我的应用程序的测试版本,并仍然在启动时出现相同的错误 – saboehnke
是的,它是minifyEnabled设置为false或因为发生ClassNotFoundException ...你注册了吗? NotificationListenerService到AndroidMani fest.xml? – kimkevin