Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

A string constant in the code is too big for a class file

Errorstring too large to encode using UTF-8 written instead as 'STRING_TOO_LARGE'

Short answer

A class file stores each string constant with a two-byte length counted in bytes. A literal that needs more than 65535 bytes in modified UTF-8 cannot be written, so the compiler stops with STRING_TOO_LARGE. Keep big text out of source: put it in assets or res/raw and read it at run time.

  • Applies to Android Studio, AGP 8, Java 17
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the longest string literals in your sources. The one that fails is usually pasted JSON, an embedded script, or Base64 data.

    grep -rn --include=*.java --include=*.kt -E '.{20000,}' app/src/main
  2. 2Match what you see, then jump to its fix

    1. 1The error names the file that holds a very long String, often generated data or a pasted payloadOne literal is bigger than the class file can storeFix
    2. 2The string has fewer than 65535 characters but contains accented, Arabic, CJK or emoji textNon-ASCII text makes the literal bigger in bytes than it looksFix

What does the error look like?

  • string too large to encode using UTF-8 written instead as 'STRING_TOO_LARGE'

Cause 1 of 2

One literal is bigger than the class file can store

1Check

The class file layout for a UTF-8 constant is a tag, a u2 length and the bytes. The u2 length is the number of bytes, so it cannot go past what two unsigned bytes hold (65535).

wc -c path/to/the/text.json

2Fix

Move the text into a file in assets/, or in res/raw/ when you only need to read the bytes. Read it with AssetManager or openRawResource.

// app/src/main/assets/data.json
val json = assets.open("data.json").bufferedReader().use { it.readText() }

// or app/src/main/res/raw/data.json
val json2 = resources.openRawResource(R.raw.data).bufferedReader().use { it.readText() }

3Verify

Delete the literal, build again, and run the screen that reads the text.

./gradlew :app:assembleDebug

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 2

Non-ASCII text makes the literal bigger in bytes than it looks

1Check

The JVM spec says the length item counts bytes, not the length of the string, and the constant is stored in modified UTF-8. Text that needs several bytes per character reaches the limit sooner.

wc -c text.txt && wc -m text.txt

2Fix

Compare the byte count with 65535 and move the text out of the source the same way, or split it into several shorter constants joined at run time.

val part1 = "first chunk..."
val part2 = "second chunk..."
val text = part1 + part2

3Verify

Build again after the change.

./gradlew :app:assembleDebug

Confirmed by docs.oracle.com

Did this fix it?

How do I stop it coming back?

  • Keep large text, JSON and Base64 blobs in assets or res/raw, never in a string literal.
  • If a code generator writes the literal, change it to write a file instead.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Android app crashes because the Apache HTTP classes are missing

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;

From Android 9 the Apache HTTP client is no longer on the default classpath, so code or a library that still uses org.apache.http fails at runtime.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.