A string constant in the code is too big for a class file
string 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.
Fix it fast
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/main2Match what you see, then jump to its fix
- 1
The 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
The 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
- 1
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.json2Fix
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:assembleDebugConfirmed by developer.android.com
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.txt2Fix
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 + part23Verify
Build again after the change.
./gradlew :app:assembleDebugConfirmed by docs.oracle.com
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 fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
Gradle cannot apply the Android application or library plugin
Failed to apply plugin 'com.android.internal.application'.
Gradle loaded the Android Gradle plugin but could not run it.
2 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fix