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

sab*_*nke 6 java android android-service android-service-binding

我正在创建一个我正在创建的应用程序的问题.基本上,当你第一次尝试打开它时,应用程序会崩溃,之后它就会好起来.令人困惑的部分是它只在您从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)
Run Code Online (Sandbox Code Playgroud)

在研究了这个问题后,我发现有人说要清除设备的缓存.这解决了这个问题......但是这只是擦除应用程序,从那时起它就可以正常工作了.初始安装仍然崩溃.这发生在多个设备上,但我无法在调试时重现它.我唯一的想法是,在调用setContentView()之后,我在onCreate()中有了这个代码.

Intent intent = new Intent(this, NotificationListener.class);
startService(intent);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)

NotificationListener.class扩展了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>
Run Code Online (Sandbox Code Playgroud)

这是我的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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是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;
    }
};
Run Code Online (Sandbox Code Playgroud)

我知道这必须是服务绑定的问题,因为如果我删除所有绑定代码并启动服务,一切都按预期运行.只有当我添加绑定代码时才会出现此错误.

kim*_*vin 2

看来不需要再次启动和绑定服务了。请删除以下代码后创建签名 apk 文件。

Intent intent = new Intent(this, NotificationListener.class);
startService(intent);
Run Code Online (Sandbox Code Playgroud)

[编辑]

我认为这是混淆器混淆,所以请将以下代码添加到您的混淆器文件中。

# 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
Run Code Online (Sandbox Code Playgroud)