2017-10-16 90 views
7

我遇到了一個我正在創建的應用程序的問題。基本上,應用程序會在您第一次嘗試打開它時崩潰,然後它就會正常運行。令人困惑的部分只有當您從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; 
    } 
}; 

我知道這必須是服務綁定的問題,因爲如果我刪除所有的綁定代碼並啓動服務,則所有事情都按照它應該運行。只有當我添加綁定代碼,我得到這個錯誤。

+0

你可以嘗試創建沒有proguard的apk文件嗎? – kimkevin

+1

@KimKevin這是否只涉及刪除條目在應用程序gradle中的buildTypes?如果是這樣,我刪除它,並上傳了我的應用程序的測試版本,並仍然在啓動時出現相同的錯誤 – saboehnke

+0

是的,它是minifyEnabled設置爲false或因爲發生ClassNotFoundException ...你註冊了嗎? NotificationListenerService到AndroidMani fest.xml? – kimkevin

回答

1

看來你不需要再次啓動和綁定服務。請刪除以下代碼後創建簽名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 
+0

我繼續去掉startService()調用,因爲bindService()應該處理它。它仍然崩潰。我想查看錯誤是否發生了變化,但由於某種原因,我沒有發現任何崩潰。 – saboehnke

+0

是的,當我刪除startService()時仍然出現同樣的錯誤。 – saboehnke

+0

我沒有處理proguard文件的經驗,所以我繼續添加到我的proguard-android.txt-2.3.3文件和我的aapt_rules.txt文件中。它仍然崩潰。 – saboehnke

0

通常這個問題是由於proguard發生的,如果你將proguard添加到你的項目中,它會停止一些這樣的功能。

剛剛嘗試通過增加添加您的服務模塊中proguard-rules.pro文件

-keep類{包名} {服務模塊名} ** {*。 }

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

這將在proguard中添加所有服務權限。

+0

我可以只刪除proguard,如果它導致問題? – saboehnke

+0

每一件事情都有自己的優點和缺點,Proguard也會爲你提供很好的安全性和許多其他功能。 所以,我的意見是將此文件添加到proguard-rules.pro文件,而不是完全刪除它。 –

0

ClassNotFoundException這裏有一個空的消息。查看dalvik.system.BaseDexClassLoader源代碼,如果它找不到它,它會拋出一個帶有指定類名稱的ClassNotFoundException,因此,您在某處傳遞了一個空的類名稱(即在由ActivityThread::handleReceiver處理的消息中)如果你可以提供完整的堆棧跟蹤(即handleMessage分支代表這個handleReceiver調用)

相關問題