簡體   English   中英

使用 minifyEnabled true 執行我的應用程序時出現 ClassCastException

[英]ClassCastException while executing my app with minifyEnabled true

我有一個 android 系統應用程序,它在清單中有一個自定義的 BroadCastReceiver(這將在 Android M 設備中運行):我的清單:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:sharedUserId="android.uid.system"
    package="mypackagename">

   ....

    <!-- custom permissions -->
    <uses-permission android:name="mypackagename.ASK_DISPLAY_INFO"
        android:protectionLevel="signatureOrSystem"/>

    <permission android:name="mypackagename.ASK_DISPLAY_INFO" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        ....

        <!-- custom receiver -->
        <receiver android:name=".CustomReceiver"
            android:permission="mypackagename.ASK_DISPLAY_INFO">
            <intent-filter>
                <action android:name="GET_HDMI_SUPPORTED_MODES"/>
                <action android:name="CHANGE_HDMI_RESOLUTION"/>
            </intent-filter>
        </receiver>



    </application>

</manifest>

我在 gradle Proguard 混淆中啟用了:

        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

        }

我有另一個測試應用程序向這個應用程序發送廣播。 問題是,使用 mignifyEnabled 是錯誤的,但是當接收到廣播意圖時,如果使用 mignifyEnabled 為真,它會給出錯誤:

java.lang.RuntimeException:無法實例化接收器 mypackagename.CustomReceiver:java.lang.ClassCastException:mypackagename.CustomReceiver 無法轉換為 android.content.BroadcastReceiver

將以下規則添加到 proguard-rules.pro:

-保持類android.content.BroadcastReceiver { *; }

當接收到一個意圖時拋出錯誤:

java.lang.AbstractMethodError: 抽象方法“void android.content.BroadcastReceiver.onReceive(android.content.Context, android.content.Intent)”

這是我的廣播接收器定義:


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;


public class CustomReceiver extends BroadcastReceiver {
    ...

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null) {
            Log.i(TAG, "CustomReceiver received action: "+intent.getAction());

            if (intent.getAction().equals(GET_HDMI_SUPPORTED_MODES)) {
                new GetHDMIModesTask(context).execute();
            }  else if (intent.getAction().equals(CHANGE_HDMI_RESOLUTION) && intent.getExtras() != null && intent.hasExtra(EXTRA_HDMI_MODE) ) {
                new ChangeHDMIModeTask(context, intent.getStringExtra(EXTRA_HDMI_MODE)).execute();
            }
        }

    }

}

由於我對 Proguard 規則非常陌生,我需要對此進行混淆,如果有人能告訴我我可以指定哪些規則來解決此問題,我將不勝感激

添加-keep public class * extends android.content.BroadcastReceiver到您的 proguard 規則而不是-keep class android.content.BroadcastReceiver { *; } -keep class android.content.BroadcastReceiver { *; } . 這將防止您的自定義廣播接收器被混淆。

我已經解決了這個問題,從清單中刪除了 CustomerReceiver,添加了一個自定義應用程序類並在其中定義了 BroadcastReceiver。 添加了proguard規則:

 -keep class android.app.Application {
        public <fields>;
        private <fields>;
       public <methods>;
    }

    -keep public class * extends android.app.Application

問題消失了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM