[英]RuntimeException on app load only in production release
I am having an issue with an app I am creating. 我正在创建的应用程序有问题。 Basically the app will crash the first time you try to open it and then it will be alright after that.
基本上,该应用将在您首次尝试打开时崩溃,然后此后一切正常。 The confusing part is it only happens when you download the app from Google Play.
令人困惑的部分是,仅当您从Google Play下载应用程序时才会发生。 If I load the app on my phone straight from Android Studio I don't get any errors.
如果直接从Android Studio将应用程序加载到手机上,则不会出现任何错误。
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)
After researching this issue I have found people say to clear the cache of the device. 在研究了此问题之后,我发现人们说要清除设备的缓存。 This fixed the issue...but that just wipes the app and it works fine from then on.
这解决了问题...但是只是擦除了该应用程序,从那时起它运行良好。 The initial install still crashes.
初始安装仍然崩溃。 This has happened on multiple devices yet I cannot reproduce it while debugging.
这已在多台设备上发生,但我在调试时无法重现。 My only thought is that I have this code in the onCreate() right after I call setContentView().
我唯一的想法是,在我调用setContentView()之后,我就在onCreate()中拥有了这段代码。
Intent intent = new Intent(this, NotificationListener.class);
startService(intent);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
NotificationListener.class extends NotificationListenerService. NotificationListener.class扩展了NotificationListenerService。 Everything works great except for the initial launch.
除了最初的启动,一切都很好。
UPDATE 更新
Here is how I set the service in the AndroidManifest: 这是我在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>
Here is a simplified version of my NotificationListenerService: 这是我的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;
}
}
}
Here is the ServiceConnection for the Binder: 这是活页夹的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;
}
};
I know this has to be an issue with the binding of the service because if I remove all of the binding code and just start the service, everything runs as it should. 我知道这对于服务的绑定必定是一个问题,因为如果我删除所有绑定代码并仅启动该服务,那么一切都会正常运行。 It is only when I add the binding code that I get this error.
仅当我添加绑定代码时,才会出现此错误。
It seems that you don't need to start and bind service again. 似乎您无需再次启动并绑定服务。 please creating signing apk file after deleting below codes.
请在删除以下代码后创建签名apk文件。
Intent intent = new Intent(this, NotificationListener.class);
startService(intent);
[EDIT] [编辑]
I think it's proguard obfuscation, so please add the following codes to your proguard file. 我认为这是对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
Generally this problem happen because of proguard, If you add proguard into your project it'll stop some of functionality like this. 通常,由于proguard会发生此问题。如果将proguard添加到项目中,它将停止某些功能。
Just try to add your service module in proguard-rules.pro file by adding 只需尝试通过添加将您的服务模块添加到proguard-rules.pro文件中
-keep class {package-name}.{service-module-name}.** { *;
-keep class {package-name}。{service-module-name}。** {*; }
}
For Example : > -keep class com.demo.service.** { *;
例如:> -keep class com.demo.service。** {*; }
}
this will add all services permission in proguard. 这将在proguard中添加所有服务权限。
The ClassNotFoundException
here has an empty message. 这里的
ClassNotFoundException
消息为空。 Looking at the dalvik.system.BaseDexClassLoader
source code, it throws a ClassNotFoundException
with the name of the indicated class if it can't find it, therefore, you are passing an empty class name somewhere (namely, in the message that is handled by ActivityThread::handleReceiver
. It would greatly help if you could provide the full stack trace (ie the handleMessage
branch which delegates this handleReceiver
call). 查看
dalvik.system.BaseDexClassLoader
源代码,如果找不到指定的类,则会抛出ClassNotFoundException
,因此,您在某个地方传递了一个空的类名(即,在由ActivityThread::handleReceiver
。这将极大地帮助,如果你能提供完整的堆栈跟踪(即handleMessage
分支,代表这个handleReceiver
调用)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.