1

One of my activities has around 4300 lines of code. When I try to compile it and run it in the emulator, an error appears saying "Code too large" and pointing towards this activity.

What should I do? Please help.

EDIT: It seems that it can handle perfectly anything below 4000 lines of code. Thank you everyone!

Johnny Uke
  • 71
  • 2
  • 9
  • What is the _exact, entire_ error message? 4300 lines isn't that much for hitting the class size limit - I wonder if you've hit the method size limit instead. – Ryan M Jun 13 '20 at 10:02
  • Well, the whole error goes like this: C:\Users\johnny\AndroidStudioProjects\MyApplication\app\src\main\java\com\example\myapplication\FourthActivity.java:48: error: code too large protected void onCreate(Bundle savedInstanceState) { – Johnny Uke Jun 13 '20 at 10:13
  • Is line 48 the class itself, or a particular method within it? Also, I misread my sources earlier - there's not a separate class size limit, I think...but if there is, I still don't think it's relevant here. – Ryan M Jun 13 '20 at 10:15
  • You need to make your code smaller. I'm not sure if there is a way to ignore this error but if there is I wouldn't do that. It really depends on your code how to fix this problem, but consider using fragments and also applying OOP principles like `single responsibility`. – ali73 Jun 13 '20 at 10:17

2 Answers2

3

I'm really shocked to see you have 4300 lines of code in a activity. I don't know how you do that. In this situation, you can't prevent "Code too large" error by android studio compiler. Because your code is really too large.

Only way to prevent this

I recommend you to move your code to another classes and access it from your activity. This is the right approach to do what you want and also it will increase your activity performance.

MMG
  • 3,226
  • 5
  • 16
  • 43
  • 2
    Thank you for the answer. I know it is shocking XDDD, but the funny thing is that before today I had around 4000 lines of code, and everything worked well. But when I added these 300 more, it would show the error. Could you please describe the way to move some of my code to another java file? I am new to this and have never done this (to move some of the code and then access it from a different class). – Johnny Uke Jun 13 '20 at 10:05
  • why not, ofcourse bro i will –  Jun 13 '20 at 10:06
  • 1
    can you provide here your code, not all but a little bit to understand it. –  Jun 13 '20 at 10:09
3

According to the Java Virtual Machine specification,

the code of a method must not be bigger than 65536 bytes.

I suppose your Activity having 4300+ Lines of code is crossing this Limit.

Also, as @Mukul pointed out, It is not a good practice to make a single class with such a large number of lines of code. Obviously, there could be certain cases where it is really necessary, but they still can be split into different classes.

Try and figure out the methods or variables which you feel can be moved to another class and recompile the project.

For Ex:

  1. Move all the final variables to a Constants.java class.
  2. Move all commonly used methods to Utils.java class.
  3. You can create another static class holding all the static methods separately.

Although there could be better ways than the examples above, however, such practice will help you minimize the bytecode of a single class and will help you maintain your code better.

Atish Agrawal
  • 2,857
  • 1
  • 23
  • 38
  • 1
    Thank you very much! Could you please tell me how I could move some of the code and then how am I supposed to access if afterwards? (I am new to this. I have never accessed it remotely). – Johnny Uke Jun 13 '20 at 10:06
  • 1
    @Atish agarwal you are right bro –  Jun 13 '20 at 10:10