[英]NumberFormatException while starting app after minifyEnabled true
[英]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.