簡體   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