1

Recently I've faced an issue related to max line (string) length limit in a path attribute of vector xml. In my case 51k chars appears to be an overlimit while 29k is OK. Anyway, I guess it is not related to some specific kind of xml like manifest, string, style, attr, drawable, layout or any other, I'm sure it is a general limitation for any xml in Android project.
So I'm curious what is the limit (I mean an exact max number of chars allowed for one line in Android xml) and why its not being applied by AndroidStudio at vector importing time? It looks like Google does not know about this limit itself or maybe its a limitation of a gradle.
Please do not point me that long strings are bad - I do realize it and it does not cancel my question.
Guys, please read my question and other SO questions you might think are the dupes carefully! There is no answer to my question there.
Android strings.xml length SO question has absolutely other meaning. Asker asks if there is a way to get the count of strings in strings.xml. Besides that, my question is not related to strings.xml exactly but to any xml file at all.
Android IndexOutOfBound Exception for resource getString is about absolutely another exception/issue and gives absolutely NO answer on my question. Again, my question is not related to strings.xml at all.
What is the maximum amount of data that a String can hold in java? is not what I am asking about, its about Java String class limitation. Even if its related then the limit is about Integer.MAX/2(cuz for Unicode it takes 2 bytes to encode 1 char) so its not 51k chars in any way.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Stan
  • 6,511
  • 8
  • 55
  • 87
  • 1
    bro you write a lot about your question but you are not on your point can you simply describe me about your question.Please be specific. – BlackBlind May 08 '19 at 08:03
  • Previously I wrote a simple and short question and it was marked as duplicate for unknown to me reason (linked dupe question was absolutely different, thanks to @Marcin Orlowsky who didn't realize what I ask in my question). Also the **main point of my question is marked bold**. Please follow by the 1st link and you will see screenshot there, it might help to understand. – Stan May 08 '19 at 08:31

1 Answers1

2

My guess reading your question was that the limit would be somewhere between 29k and 51k but clipped to a logical number

I might have found the answer by testing it. As you said, a char is stored on two bytes. So 29k char would be 58k bytes and 51k would be 102k bytes. The "logical" limit would be 65536 as it is 2^16. so the limit in char is 2^16 / 2 or 2^15 which is 32768

I tested to put strings in my string.xml (basically a long line of 'a')

<string name="length_test32000">(32 000 a)</string>
<string name="length_test32767">(32 767 a)</string>
<string name="length_test32768">(32 768 a)</string>
<string name="length_test32769">(32 769 a)</string>
<string name="length_test33000">(33 000 a)</string>

Then i tried to log their size :

String test32k = getString(R.string.length_test32000);

String test32k767 = getString(R.string.length_test32767);
String test32k768 = getString(R.string.length_test32768);
String test32k769 = getString(R.string.length_test32769);

String test33k = getString(R.string.length_test33000);

Log.i("tag", "32000 : "+test32k.length());
Log.i("tag", "32767 : "+test32k767.length());
Log.i("tag", "32768 : "+test32k768.length());
Log.i("tag", "32769 : "+test32k769.length());
Log.i("tag", "33000 : "+test33k.length());

Here are the results :

I/tag: 32000 : 32000
I/tag: 32767 : 32767
I/tag: 32768 : 16
I/tag: 32769 : 16
I/tag: 33000 : 16

From 32768 it seems to be truncated so i log what was inside

Log.i("tag", "32768 : "+test32k768.length() + " content : " + test32k768);

And the result is :

I/tag: 32768 : 16 content : STRING_TOO_LARGE

The maximum char seems to be 32767 (2^15 - 1) characters. I didn't find any official doc that say that, but it is what i found while testing

Kilarn123
  • 723
  • 5
  • 6
  • Thanks, man. Actually I came to the same conclusion and my guess was about power of 2 and is also 32767 (since 29k is OK and 51k is not) and I'm going to check it. Interesting who is responsible for that limit? Is it AndroidStudio or gradle? – Stan May 08 '19 at 09:06
  • it does compile and run since i checked with logcat so i don't know who is responsible for that limit – Kilarn123 May 08 '19 at 09:31
  • also the limit does not come from the String class since i'm able to assign this value directly in activity without using xml and can show sizes above 32767, so it realy is limited by xml importation – Kilarn123 May 08 '19 at 09:35
  • Yes, the main point is that it will compile OK but at run time you will get some mysterious crashes like ResourceNotFound etc. Thats the baddest thing. – Stan May 08 '19 at 10:13