7

Note: This is specific problem with android system, not simply out of bound exception which some suggesting duplicate

I have a problem with investigating the cause of the exception. I'm trying to get string resource text(which is HTML):

val string = getString(R.string.long_html_text)

This HTML is of course valid(works on emulator and other real devices) but not on real device LGE Nexus 5X API 27. The html text has 40113 characters.

When I am trying to get this string resource as above I am getting this Exception:

   04-17 14:38:17.136 2847-2847/com.myapp.tmt.app.myapp E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.myapp.tmt.app.myapp, PID: 2847
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.tmt.app.myapp/com.myapp.tmt.app.myapp.ui.eula.EulaActivity}: java.lang.IndexOutOfBoundsException
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
       at android.app.ActivityThread.-wrap11(Unknown Source:0)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6494)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
    Caused by: java.lang.IndexOutOfBoundsException
       at android.content.res.StringBlock.nativeGetString(Native Method)
       at android.content.res.StringBlock.get(StringBlock.java:82)
       at android.content.res.AssetManager.getResourceValue(AssetManager.java:236)
       at android.content.res.AssetManager.getResourceText(AssetManager.java:164)
       at android.content.res.Resources.getText(Resources.java:335)
       at android.content.res.Resources.getString(Resources.java:433)
       at android.content.Context.getString(Context.java:556)
       at com.myapp.tmt.app.myapp.ui.eula.EulaActivity.prepareLicence(EulaActivity.kt:33)
       at com.myapp.tmt.app.myapp.ui.eula.EulaActivity.onCreate(EulaActivity.kt:28)
       at android.app.Activity.performCreate(Activity.java:6999)
       at android.app.Activity.performCreate(Activity.java:6990)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
       at android.app.ActivityThread.-wrap11(Unknown Source:0) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
       at android.os.Handler.dispatchMessage(Handler.java:106) 
       at android.os.Looper.loop(Looper.java:164) 
       at android.app.ActivityThread.main(ActivityThread.java:6494) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Has anyone encounter it?

reVerse
  • 35,075
  • 22
  • 89
  • 84
K.Os
  • 5,123
  • 8
  • 40
  • 95
  • Did you use this <![CDATA[

    Text

    ]]>

    while adding html is strings.xml
    – Anonymous Apr 17 '18 at 13:01
  • 1
    @Konrad yes i have encountered this with the latest android security update. It appears that the max length of a string has been reduced due to security vulnerabilities. Try to "shorten" your string length and check if the error will be gone This is very annoying if you are using string resources to mock data and as you can imagine larger strings (news etc.) will break your app. – bko Apr 17 '18 at 13:03
  • @bko of course, when i shorten the text the error not appears. Strange is that it work for every other devices but not this API 27(works also on emulator) – K.Os Apr 17 '18 at 13:23
  • Do you have any messages before? The IndexOutOfBound is thrown when [ResStringPool::stringAt](https://github.com/aosp-mirror/platform_frameworks_base/blob/master/libs/androidfw/ResourceTypes.cpp#L696) returns [NULL](https://android.googlesource.com/platform/frameworks/base.git/+/master/core/jni/android_util_StringBlock.cpp#89) ... so search for messages like `No memory trying to allocate decode cache table of %d bytes`, `Bad string block: string...`, etc.... – Selvin Apr 17 '18 at 13:23
  • @bko can you link me to this latest security update, where i can read about it? – K.Os Apr 17 '18 at 13:25
  • 1
    @Konrad https://source.android.com/security/bulletin/2018-04-01, Im pretty sure they changed something in this update because my other phones that are not updated work fine with a particularly "long" string in the xml resources, but my nexus throws the same exception you are getting. – bko Apr 17 '18 at 13:34
  • @bko Thanks for your suggestion – K.Os Apr 17 '18 at 13:42
  • @Selvin normalny. Zobacz komentarz bko – K.Os Apr 17 '18 at 14:13
  • did you find a solution for this problem? – Ruan_Lopes Jun 14 '18 at 16:29
  • @Ruan_Lopes I am 90% sure that this is related only for Android Oreo - there is maximum range for string resource. I resolve this issue by split my very long string on two parts – K.Os Jun 14 '18 at 17:13
  • I had the same error reported on Android 7.1.1 and the string is very short. But the string contains a %d – ARLabs Oct 16 '18 at 13:55

1 Answers1

3

As this was only happening on Android Oreo devices i resolve this issue only by split my very long HTML string to two parts.

I think that this issue was due to new Android security restrictions (as some suggested here)

K.Os
  • 5,123
  • 8
  • 40
  • 95