Android studio updated and code too large now

There is no need to pass the remoteMessageMap to another class. The source of the problem is the limitation in the java method size. Here is a piece of the official documentation of oracle which is related to this problem:

code_length
The value of the code_length item gives the number of bytes in the code array for this method.
The value of code_length must be greater than zero (as the code array must not be empty) and less than 65536.

The point is that your onMessageReceived method is too long, which is bigger than 64KB of compiled code. It is weird why it was compiled fine in previous versions of Android Studio 🙂

Anyway, the solution is to break the method into smaller fragments. My suggestion is fragmentation by some message types. For example:

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
}

In this way, the method size would be optimized and the performance of calling it will be far improved.

Leave a Comment