Androidstudio更新了,现在代码太大了

我有一个已经生产了 2 年的系统。它是用于控制企业设备的 EMM 系统。

它用于FireBase将在设备上执行的功能从服务器应用程序发送到设备。

您可以向设备发送大约 400 个可能的命令,所有这些命令最初都在一个类中处理,该类覆盖了onMessageReceived()来自FireBaseMessagingService类的 。

旧版本的 Android Studio 构建了现在正在生产中的 apk。大约一年后,我开始使用我的系统的第 2 版。所以我将我的 Android 工作室更新到最新的 (4)。

问题:

当我尝试构建项目并推送到设备上时,我得到

error: code too large public void onMessageReceived(RemoteMessage remoteMessage) {

如前所述,此onMessageReceived方法可以处理来自服务器应用程序的 400 种不同类型的推送通知,因此方法主体中有很多 if/else 语句。

是否有任何原因,因为 AS 升级这将不起作用?

有没有我可以在 AS 中更改的设置来解决这个问题?

我尝试过的:

我想把一半的 if/else 放在另一个服务类中,以减少方法代码。这将涉及将 传递remoteMessageMap给另一个类以进行 if/else 处理。

remoteMessageMap 从 FireBase 是一个 Map 并且 Maps 不可序列化,因为它们扩展了接口,因此无法传递它。

public class MyAndroidFirebaseMsgService extends FirebaseMessagingService {

    private static final String TAG = "MyAndroidFCMService";
    AppObj appObj;


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


        Log.e(TAG, "remoteMessage.getData() = " + remoteMessage.getData());

        Map remoteMessageMap = remoteMessage.getData();

        String message = (String)remoteMessageMap.get("message");

谢谢

[编辑1]

else if(message.trim().equalsIgnoreCase("CLEARCACHE_REMOVE_APP_WL")){


            Log.e(TAG, "received CLEARCACHE_REMOVE_APP_WL");

            String pushGuid =  (String)remoteMessageMap.get("pushguid");
            Log.e(TAG, "pushGuid = " + pushGuid);

            String clearCacheRemoveWhitelist =  (String)remoteMessageMap.get("clear_cache_app_names");


            Intent intentExecutePushCommand = new Intent( getApplicationContext(), ExecutePushCommandIntentService.class);
            intentExecutePushCommand.putExtra("compID", MenuActivity.companyID);
            intentExecutePushCommand.putExtra("command", message);
            intentExecutePushCommand.putExtra("pushguid", pushGuid);
            intentExecutePushCommand.putExtra("clear_cache_app_names", clearCacheRemoveWhitelist);



            startService(intentExecutePushCommand);

        }else if(message.trim().equalsIgnoreCase("CLEARCACHE_GET_PACKAGENAMES_WL")){


            Log.e(TAG, "received CLEARCACHE_GET_PACKAGENAMES_WL");

            String pushGuid =  (String)remoteMessageMap.get("pushguid");
            Log.e(TAG, "pushGuid = " + pushGuid);


            Intent intentExecutePushCommand = new Intent( getApplicationContext(), ExecutePushCommandIntentService.class);
            intentExecutePushCommand.putExtra("compID", MenuActivity.companyID);
            intentExecutePushCommand.putExtra("command", message);
            intentExecutePushCommand.putExtra("pushguid", pushGuid);

            startService(intentExecutePushCommand);

        }else if(message.trim().equalsIgnoreCase("CLEARCACHE_ADD_PACKAGENAME_WL")){


            Log.e(TAG, "received CLEARCACHE_ADD_PACKAGENAME_WL");

            String pushGuid =  (String)remoteMessageMap.get("pushguid");
            Log.e(TAG, "pushGuid = " + pushGuid);

            String packageName =  (String)remoteMessageMap.get("package_name");


            Intent intentExecutePushCommand = new Intent( getApplicationContext(), ExecutePushCommandIntentService.class);
            intentExecutePushCommand.putExtra("compID", MenuActivity.companyID);
            intentExecutePushCommand.putExtra("command", message);
            intentExecutePushCommand.putExtra("pushguid", pushGuid);
            intentExecutePushCommand.putExtra("package_name", packageName);

            startService(intentExecutePushCommand);

        }

回答

没有必要将 传递remoteMessageMap给另一个类。问题的根源在于java方法大小的限制。这是oracle官方文档中与这个问题相关的一段:

code_length
code_length 项的值给出了此方法的代码数组中的字节数。
code_length 的值必须大于零(因为代码数组不能为空)且小于 65536。

关键是你的onMessageReceived方法太长,大于64KB的编译代码。奇怪的是为什么它在以前版本的 Android Studio 中编译得很好:)

无论如何,解决方案是将方法分解为更小的片段。我的建议是按某些消息类型进行分段。例如:

private static final String COMMAND_1 = "COMMAND_1";
private static final String COMMAND_2 = "COMMAND_2";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e(TAG, "remoteMessage.getData() = " + remoteMessage.getData());

    Map remoteMessageMap = remoteMessage.getData();

    String message = (String) remoteMessageMap.get("message");
    
    String type = extrated_from_received_message;

    switch (type) {
        case COMMAND_1:
            handleCommand1(remoteMessageMap);
            break;
        case COMMAND_2:
            handleCommand2(remoteMessageMap);
            break;

        // more commands ...

        default:
            // ...
    }
}

private void handleCommand1(Map remoteMessageMap){
    // do whatever related to command 1
}

private void handleCommand2(Map remoteMessageMap){
    // do whatever related to command 2
}

这样,方法的大小会得到优化,调用的性能会大大提高。


以上是Androidstudio更新了,现在代码太大了的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>