Issue with internals in Android Studio


Whenever the internal keyword is used in Android Studio, I get errors when I try to build code which accesses the variables from Java. For example:

Error:(74, 41) error: cannot find symbol method getSpan$production_sources_for_module_app()

There appear to be no errors with the method name (Android Studio suggested this name). I’ve tried searching online and I haven’t found a solution yet.


You are not supposed to be able to use anything declared internal outside of its compilation scope. The reason why you see it is that under the hood, internal properties/functions/whatever get compiled down to a public function with some sort of funky name. I am no Java expert but as far as I understand it, the name is still valid jvm byte code, but it no longer is valid java. That’s the reason you can’t call it. I’m surprised that Android Studio suggested the name. That sounds like a bug to me.
There might be some hacky way to access internal fields through reflection but in general internal means that you should not be allowed to access it.

PS: I am no expert on java or jvm byte code, so if my explanation is wrong, feel free to correct me :wink:


I’m no expert either, but according to the Kotlin site Java classes should be able to access internal visibility things as long as they are in the same module. The thing is Kotlin does name mangling (according to various sources) for internal methods, which is why the $production_sources_for_module_app is appended at the end.


can you share a link to that information. The info I found is this:

internal declarations become public in Java. Members of internal classes go through name mangling, to make it harder to accidentally use them from Java and to allow overloading for members with the same signature that don’t see each other according to Kotlin rules;