5

I created simple Base64Images helper class which contains this body:

companion object{
  val ABSTRACT_COLORS = "[image encoded in base64]"
}

ABSTRACT_COLORS is actually a string which has 570438 characters.

During compilation I got:

org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Failed to generate property ABSTRACT_COLORS
...
...
The root cause was thrown at: ByteVector.java:213 at org.jetbrains.kotlin.codegen.MemberCodegen.genFunctionOrProperty(MemberCodegen.java:205)
Caused by: java.lang.IllegalArgumentException

I thought I can store 2147483647 (231 - 1) characters in a string.

Why is that?

I posted this image below.
You can use this tool to generate base64.

Hint: editing this class or compiling the project freezes Android Studio.
I'd use some lightweight editor to edit and terminal to compile it.

enter image description here

Community
  • 1
  • 1
klimat
  • 24,711
  • 7
  • 63
  • 70
  • 1
    This looks like a compilation bug. e.g. If the compiler is failing because it doesn't have enough memory allocated then the exception thrown should state such (which it doesn't). I suggest reporting the issue at https://youtrack.jetbrains.com/issues/KT. You might search online for a way to increase the kotlin compiler's allocated memory or something like that. – mfulton26 Sep 19 '16 at 13:33

1 Answers1

4

As mentioned in a comment by @mfulton26 that something is going on with the compiler when loading the string. A crash bug that should be reported to Kotlin issue tracker.

As a work-around you can add this as a file in your src/main/resources directory and loading the string dynamically either as a String or as ByteArray.

For example, if the file was src/main/resources/abstract-colors.txt you could read the entire file into a string:

val ABSTRACT_COLORS = javaClass.getResourceAsStream("/abstract-colors.txt")
                               .bufferedReader().use { it.readText() }

If you did not need it to be base64 encoded, you could store the image as binary and read it into an ByteArray.

val ABSTRACT_COLORS = javaClass.getResourceAsStream("/abstract-colors.jpg")
                               .use { it.readBytes() }
Community
  • 1
  • 1
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
  • 1
    the string has less than 0,5MB not 0.5GB ;) I know i can load the content from file... but still it would be nice to have it at compilation time. – klimat Sep 19 '16 at 14:44
  • @mklimek edited and leaving answer (other people in the future may need this work around, the answer is for the community as well) – Jayson Minard Sep 19 '16 at 14:47
  • There is much better solution. Put files to assets folder (not in the resources) and use `assets.open("jpeg_base64.txt").bufferedReader().readText()` – user155 May 30 '19 at 12:14