2017-10-16 14 views
7

Tôi đang gặp sự cố với ứng dụng tôi đang tạo. Về cơ bản các ứng dụng sẽ sụp đổ lần đầu tiên bạn cố gắng để mở nó và sau đó nó sẽ được alright sau đó. Phần khó hiểu là nó chỉ xảy ra khi bạn tải xuống ứng dụng từ Google Play. Nếu tôi tải ứng dụng trên điện thoại của mình trực tiếp từ Android Studio, tôi không gặp bất kỳ lỗi nào.RuntimeException khi tải ứng dụng chỉ trong bản phát hành sản xuất

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) 

Sau khi nghiên cứu vấn đề này, tôi đã tìm thấy mọi người nói để xóa bộ nhớ cache của thiết bị. Điều này đã khắc phục vấn đề ... nhưng điều đó chỉ xóa sạch ứng dụng và nó hoạt động tốt từ đó trở đi. Cài đặt ban đầu vẫn bị treo. Điều này đã xảy ra trên nhiều thiết bị nhưng tôi không thể tái tạo nó khi đang gỡ lỗi. Suy nghĩ duy nhất của tôi là tôi có mã này trong onCreate() ngay sau khi tôi gọi setContentView().

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

NotificationListener.class extends NotificationListenerService. Tất cả mọi thứ hoạt động tuyệt vời ngoại trừ khởi chạy ban đầu.

CẬP NHẬT

Sau đây là cách tôi đặt dịch vụ trong 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> 

Đây là một phiên bản đơn giản của NotificationListenerService tôi:

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; 
     } 
    } 
} 

Đây là ServiceConnection cho the Binder:

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; 
    } 
}; 

Tôi biết điều này phải là một vấn đề với sự ràng buộc của dịch vụ bởi vì nếu tôi loại bỏ tất cả các mã ràng buộc và chỉ bắt đầu dịch vụ, mọi thứ đều chạy như nó cần. Đó là chỉ khi tôi thêm mã ràng buộc mà tôi nhận được lỗi này.

+0

Bạn có thể thử tạo tập tin apk mà không cần proguard nếu bạn làm như vậy? – kimkevin

+1

@KimKevin Điều đó có liên quan đến việc xóa mục nhập Nếu vậy, tôi đã gỡ bỏ nó và tải lên một phiên bản beta của ứng dụng của tôi và vẫn gặp lỗi tương tự khi khởi chạy – saboehnke

+0

vâng, nó đã được cài đặt là minifyEnabled là false hoặc do ClassNotFoundException xảy ra ... bạn đã đăng ký chưa NotificationListenerService cho AndroidMani fest.xml? – kimkevin

Trả lời

1

Dường như bạn không cần phải bắt đầu và ràng buộc dịch vụ một lần nữa. hãy tạo tệp APK ký sau khi xóa các mã bên dưới.

Intent intent = new Intent(this, NotificationListener.class); 
startService(intent); 

[EDIT]

Tôi nghĩ đó là Proguard obfuscation, vì vậy hãy thêm các mã sau vào tập tin Proguard của bạn.

# 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

Tôi đã đi trước và loại bỏ các startService() gọi vì nó bindService() nên xử lý nó. Nó vẫn bị treo. Tôi muốn xem nếu lỗi đã thay đổi nhưng tôi không nhận được bất kỳ sự cố nào đối với bản beta vì một lý do nào đó. – saboehnke

+0

Vâng tôi vẫn gặp lỗi tương tự khi tôi xóa startService(). – saboehnke

+0

Tôi không có kinh nghiệm đối phó với các tập tin proguard vì vậy tôi đã đi trước và thêm vào đó cả tập tin proguard-android.txt-2.3.3 và tệp aapt_rules.txt của tôi. Nó vẫn bị treo. – saboehnke

0

Thông thường vấn đề này xảy ra vì bảo vệ, Nếu bạn thêm proguard vào dự án, nó sẽ dừng một số chức năng như thế này.

Chỉ cần cố gắng thêm mô-đun dịch vụ của bạn trong proguard-rules.pro tập tin bằng cách thêm

lớp -keep {package-name} {-module-tên dịch vụ} ** {*..; }

Ví dụ:> -keep class com.demo.service. ** {*; }

điều này sẽ thêm tất cả các dịch vụ cho phép trong proguard.

+0

Tôi có thể chỉ cần gỡ bỏ proguard nếu nó gây ra sự cố không? – saboehnke

+0

Mọi thứ đều có ưu và nhược điểm riêng, Proguard cũng cung cấp cho bạn bảo mật tốt và nhiều tính năng khác nữa. Vì vậy, ý kiến ​​của tôi là thêm tệp này vào tệp proguard-rules.pro thay vì xóa hoàn toàn tệp đó. –

0

ClassNotFoundException ở đây có thông báo trống.Nhìn vào mã nguồn dalvik.system.BaseDexClassLoader, nó ném một ClassNotFoundException với tên của lớp được chỉ định nếu nó không thể tìm thấy nó, do đó, bạn đang đi qua một tên lớp trống ở đâu đó (cụ thể là, trong thông báo được xử lý bởi ActivityThread::handleReceiver. giúp đỡ rất nhiều nếu bạn có thể cung cấp dấu vết ngăn xếp đầy đủ (ví dụ: chi nhánh handleMessage đại diện cho cuộc gọi handleReceiver này)

Các vấn đề liên quan