[Solved] "Code too large" error on Android (CerberusGame.java)

garret

New Member
Joined
Jun 12, 2020
Location
Berlin
Hey,

I’m working on my app, which includes some career tests. The first version is nearly ready for testing, but I get an error message when I try to release on Android (HTML5 works). It says that the code is too large:

Code_too_large.png



The code is now about 5000 lines long (I’d say 3.000 to 4.000 without the indentations). After reducing some lines the release on Android worked, but since I‘ not completely ready and plan to add more tests, this is a problem.

I’m not to deep in the Methods topic, therefore the whole code is in one main .cxs with a big Select-Case-loop. For obvious length reasons, I won’t copy the whole source code, but to get an idea of it’s structure I’ll describe it with pseudo code.

You’ve got the main loop with mode = 0, the next menus with mode = 1, 2, 3 etc. and single tests with „Case 101“



Cerberus X:
Field Images, variables etc.

Method OnRender()

Select mode

    Case 0' Start menu

        If (Click on button1)  mode = 1
        If (Click on button2)  mode = 2

    Case 1' Sub menu 1

        If (Click on button1)  mode = 10
        If (Click on button2)  mode = 11

    Case 10' Sub sub menu 10

        If (Click on button1)  mode = 101 (test 101)
        If (Click on button2)  mode = 102 (test 102)

    Case 101
        (test 101)

    Case 2' Sub menu 2

        If (Click on button1)  mode = 20
        If (Click on button2)  mode = 21

End Select


My questions:
  • How can I write more code and avoid the error? If possible, I’d like to keep the Select/Case structure (at least for this project).

My thoughts if that doesn’t work were:
  • Using a main .cxs file and use „Import“ for the other parts. Would this split the code into more files or will it become a big file either way?
  • Putting some code in an external file and use „Include“.

Greetings
Benny
 

MikeHart

Administrator
Joined
Jun 19, 2017
Location
Germany
Hi Benny, you need to split your code into functions or methods. One huge select statement on Android breaks the dalvik interpreter. We had this problem before i think.
 

MikeHart

Administrator
Joined
Jun 19, 2017
Location
Germany
I was wrong, we had a similar problem with DEsktop compiling in Debug mode. But googling about error for Android then shows up that this a problem in Android indeed. You have to structure your code differently.
Maybe split your case statement and place them inside they own functions or methods.
 
Last edited:

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
@MikeHart is completely right. Here are just some additional annotations.

The code is now about 5000 lines long
Like @MikeHart said it is not the whole code, but your OnRender() Method, that is to long.
So how long is it actually? This could be interesting for others encountering this error.

How can I write more code and avoid the error? If possible, I’d like to keep the Select/Case structure (at least for this project).
To me the select case structure seems to be something that should reside in OnUpdate() and the resulting mode value should then be used in OnRender() to draw the correct stuff.

I also can not imagine that the select case structure is eating up the whole 64kb, that seems to be the problem. So you could just put the selct case structure into a separate method called CalculateMode:Int(mode:Int) and leave the rest like it is.


  • Using a main .cxs file and use „Import“ for the other parts. Would this split the code into more files or will it become a big file either way?
  • Putting some code in an external file and use „Include“.
The general filesize is not the problem, so this is not necessary and like you insinuated, import doesnt reduce the resulting filesize.
Using Include and combining those files might be possible, but not too easy and definitely not necessary for your problem.
 

garret

New Member
Joined
Jun 12, 2020
Location
Berlin
Thanks for the answers so far, I will try your suggestions and get back to you afterwards!

The Select/Case loop is about 4500 lines long, but I use empty lines to indent quite much (anyhow not more than one per command and no semicolons in the lines). So I'd say round about 2250 to 2500 lines of commands where it works, with about 100-200 lines more it crashes. The OnRender() method is ~ 10 code lines longer.


Update 1: I put the whole Select/Case loop in an own Method CalculateMode() and called it in OnRender() and afterwards in OnUpdate(), but it still fails with "code too large" in both cases.
 
Last edited:

MikeHart

Administrator
Joined
Jun 19, 2017
Location
Germany
That could lead to unexpected behavior. I hope the parser catched any syntax error in your huge select case statement. On html5 it is more forgiving. But first see what your restructuring results in.
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
This is really a huge select block!
You could also post a representative part of your select case structure. Maybe we can come up with a solution that doesn't result in such an amount of lines.

Regarding your pseudocode this could be something like:

Cerberus X:
Strict

Function CalculateMode:Int(currentMode:Int, button1Clicked:Bool, button2Clicked:Bool)
    Local clickCnt:Int = 0
    
    If currentMode < 100
        If button1Clicked
            clickCnt+=1
        ElseIf button2Clicked
            clickCnt+=2
        EndIf
        
        currentMode = currentMode * 10 + clickCnt   
    EndIf
    
    Return currentMode
End

Function Main:Int()
    For Local i:Int = 0 To 100
        Print("curMode: " + i + "  newMode: " + CalculateMode(i,False, True))
    Next
    Return 0
End
 

garret

New Member
Joined
Jun 12, 2020
Location
Berlin
Did I mention that there are up to 3 further Select/Cases in the Case blocks as well? :D


I tried the idea of @MikeHart now and put three tests from "Case 201 (Do Test 201)" into their own Methods. Works fine even with an additional test that had the release crashed otherwise and the code looks somehow like this now:

Cerberus X:
Method Mode201()
[...]
End Method

Method Mode211()
[...]
End Method


Method OnRender()

Select Mode

    Case 201
   
        Mode201()
       
    Case 211
   
        Mode211()
       
End Select

End Method
Basically the same code, except each of them in a single Method. I was shocked twice:
The first career test worked, at the second the app crashed which was by random a test I've changed some array sizes in the last days. Therefore I first thought it would be a huge work because I had to check for each test, but all other ~15 tests work fine.

The second shock was when I realized that I didn't change pretty much. I mean, it's the same code, just differently structured :oops:

So I'd say it's solved for now and thank you very much :) I'll get back if there are still crashes.

I will make a showcase out of the first version once I've found the bug in the one test.
 
Last edited:

garret

New Member
Joined
Jun 12, 2020
Location
Berlin
I think you are out to become the most select case statements in a block champion. ;)
I think it's because I often start with a variable and don't know how much cases it will have at the end and therefore use Select/Case.

When I look at some Select/Case constructions now, which only have two options (0 and 1), I could have used If/Elseif/Else instead, that's true ;)

And by the way, I fixed the broken test. Wrong array sizes, as expected.

Title edited as well.
 
Last edited:
Top Bottom