我遇到了一个我正在创建的应用程序的问题。基本上,应用程序会在您第一次尝试打开它时崩溃,然后它就会正常运行。令人困惑的部分只有当您从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;
}
};
我知道这必须是服务绑定的问题,因为如果我删除所有的绑定代码并启动服务,则所有事情都按照它应该运行。只有当我添加绑定代码,我得到这个错误。
你可以尝试创建没有proguard的apk文件吗? – kimkevin
@KimKevin这是否只涉及删除条目在应用程序gradle中的buildTypes?如果是这样,我删除它,并上传了我的应用程序的测试版本,并仍然在启动时出现相同的错误 – saboehnke
是的,它是minifyEnabled设置为false或因为发生ClassNotFoundException ...你注册了吗? NotificationListenerService到AndroidMani fest.xml? – kimkevin