FIX string too large to encode using UTF-8 written instead as ‘STRING_TOO_LARGE’.

Recently, there are some people who reported that they got an error: string too large to encode using UTF-8 written instead as ‘STRING_TOO_LARGE’. How to fix it? Let us find out that solution here.

How to fix the error ‘STRING_TOO_LARGE’

The error may be caused due to a string being too long. After you check the length of the string, you are able to try using the Android plugin. The ‘cleanTasks’ are able to clear your projectbuild/Directory. Similar, you are able to run ‘cleanBuildCacheTask’ to clear the build cache for your project. If the project assigns a build cache for it. Running this task from this project will be able to clear the associated cache, not the shared cache in the default location. To do this task, you have to choose from the menu barView. Here are steps:

  • At the first step, you are able to choose from the menu bar View.
  • After that, you have to navigate tool Windows.
  • Then, you are able to navigate Terminal.
  • In this step, you are able to use the command below:

gradlew cleanBuildCache   

In order to increase your build speed, you are able to follow these steps below:

  • Some measures are able to be taken immediately for the benefit of the majority of Android Studio project, please optimize your build configuration.
  • Analyze your building to recognize and diagnose some of your projects or workstations more difficult bottleneck.

We have some examples of people who have got this error. Here are their statement:

  1. When I clean my Android project in Android studio, the error occur. I have already  backed to previous commit or different branch that works find couple days ago, but has the error. I have already checked this question and there is not large image or strings added for my project. STRING_TOO_LARGE String in Kothlin.

Solution: For the present, you are able to downgrade Gradle version to fix this problem. Please use gradle 3.1 version like 3.1.3.

classpath ‘com.android.tools.build:gradle:3.1.3’

  1. I always get this error when i am trying to add the stripe library “com.stripe:stripe-android:6.1.2” to any project including an empty one. Sometimes, I can get it go away by removing the Gradle dirs and resyncing, however downgrading Gradle plugin seems to be the only reliable option to make it go away permanently Gradle Plugin Versions.

Solution: Try to use the Android plugin. The ‘cleanTasks’ are able to clear the projectbuild or Directory.

  1. Recently, I have come across a weird and totally unexpected problem while I am developing an Android app. I bet many people may have enough luck to never notice it but if it finally occurs to you and crashes your app, it is quite hard to determine the cause of the problem quickly if you do not know what you are actually looking for.

What is ‘STRING_TOO_LARGE’ error?

Apparently, it is quite probable that you are not going to know about this error in your project until you see or experience it by yourself. So, let us start with its symptoms. First, you may see a crash with a stack trace looking more or less. A little less noticeable problem is when you attempt to display a text in the UI. As you will be able to can see, in both cases there is that strange STRING_TOO_LARGE either displayed directly in the UI or mentioned in the stack trace.

The STRING_TOO_LARGE error is pretty easy to reproduce and may occur to you unexpectedly. A simplest way is to add a string resource to your app which is longer, and then displaying it. We suggest you to make a long string by using some kind of lorem ipsum generator rather than writing it yourself. However, if you want to crash your app in a way we have explained above, it will be a little more complex. You still must add a string that large but this time try to put it into the vector drawable android:pathData attribute. For note: Having vector drawables with very long paths is not recommended. Android Studio should warn you about that. However there is no warning when you exceed the explained above limit in a string resource.

How does the error happen?

How does the error happen

The error is caused by the AAPT/AAPT2 (Android Asset Packaging Tool) that processes your app’s resources and replaces them with the STRING_TOO_LARGE value once it finds a large string. If you take a look into the APK file affected by this problem, you are going to find something error. If you are interested in the details, you will be able to find the AAPT2 source code fragment that converts the large strings: StringPool.cpp

Detect the error before you release it

Detect the error before you release it

The simplest method to find out if your app is affected is to read the build log carefully looking for the error message. Surprisingly, the build process does not fail when it occurs. If you utilize a build system like TeamCity, Jenkins, or QuickBuild, you will be able to configure the build to fail once such a log message is detected. In case of TeamCity, it is about adding a failure condition.

About UTF-8

UTF-8 is a variable-width character encoding that used for electronic communication. It defined by the Unicode Standard, the name is derived from Unicode (or Universal Coded Character Set) Transformation Format. UTF-8 is capable of encoding all valid character code points in Unicode by using one to four one-byte code units. The code points with lower numerical values (which tend to occur more frequently) are encoded by using fewer bytes. It was designed for compatibility with ASCII, the first 128 characters of Unicode that correspond one-to-one with ASCII (which are encoded by using a single byte with the same binary value as ASCII). So, a valid ASCII text is valid UTF-8-encoded Unicode as well. As ASCII bytes do not occur once encoding non ASCII code points into UTF-8, then UTF-8 is safe to utilize within most programming and document languages.