繁体   English   中英

仅在生产版本中对应用程序加载RuntimeException

[英]RuntimeException on app load only in production release

我正在创建的应用程序有问题。 基本上,该应用将在您首次尝试打开时崩溃,然后此后一切正常。 令人困惑的部分是,仅当您从Google Play下载应用程序时才会发生。 如果直接从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)

在研究了此问题之后,我发现人们说要清除设备的缓存。 这解决了问题...但是只是擦除了该应用程序,从那时起它运行良好。 初始安装仍然崩溃。 这已在多台设备上发生,但我在调试时无法重现。 我唯一的想法是,在我调用setContentView()之后,我就在onCreate()中拥有了这段代码。

Intent intent = new Intent(this, NotificationListener.class);
startService(intent);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

NotificationListener.class扩展了NotificationListenerService。 除了最初的启动,一切都很好。

更新

这是我在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 class {package-name}。{service-module-name}。** {*; }

例如:> -keep class com.demo.service。** {*; }

这将在proguard中添加所有服务权限。

这里的ClassNotFoundException消息为空。 查看dalvik.system.BaseDexClassLoader源代码,如果找不到指定的类,则会抛出ClassNotFoundException ,因此,您在某个地方传递了一个空的类名(即,在由ActivityThread::handleReceiver 。这将极大地帮助,如果你能提供完整的堆栈跟踪(即handleMessage分支,代表这个handleReceiver调用)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM