Kotlin generates a `.kotlin_module` file for each ...
# gradle
a
Kotlin generates a
.kotlin_module
file for each compiled module. The name of the file is the name of the module, but this can cause some issues when you have multiple modules with the same name (but different paths). Would it be worth filing a feature request to use the path instead of the module name?
👀 1
c
The problem extends well beyond this file; many parts of Gradle have issues when there are modules with the same name, in different paths. The best solution is to avoid this: • name modules uniquely, where possible; • adjust the group or project name to include the pathing, in some manner (this may not address all issues, but works fine in several large projects I have)
m
Shouldn't different modules generate different .jar/.aar and the compiler be able to know/link the
.kotlin_module
info to only those symbols that are in the jar?
I can see how that's an issue for fat jars but in the "regular" case, it could work?
c
I’ve never run into a problem with
.kotlin_module
with overlapping module names, rather other challenges such as confusion when pulling project dependencies etc.
a
The issue is that these files get packaged into a single APK on Android so they'll overwrite each other
m
But that's executable form, you're not supposed to compile against an APK, right?
Unless
.kotlin_module
is used for something else?
a
Supposedly it is used for some things as it's recommended to exclude them from the APK
The same issue happens with an AAR though
I think setting this file name yourself using
moduleName
is the best bet here
c
don’t know Android / APK that well but often build tasks that assemble multiple components (e.g. fat jars) there are options to customize merging, instead of blindly overwriting.
a
Unfortunately, the
kotlin_module
are special cased in the Android Gradle Plugin so the normal packaging options doesn't apply to them
c
It seems like the recommendation is to exclude these from the APK, would that not address the issue?
a
I'm not sure using packaging options work here. But also, we do want them since we do use kotlin-reflect
m
ah, reflection
c
Perhaps something like this then:
Since the actual names of the
*.kotlin_module
files do not affect anything at the moment, you can try to rename one of the files during build so that the names are different (the new name should still end with
.kotlin_module
). I’m not sure what is the best way to do this in your particular case, though. You might want to exclude the
*.kotlin_module
files using
packagingOptions
and add a custom task that extracts the files and puts them as resources, renaming one of them, so that both are included.
m
Why is it it's always reflection 😄
c
It’s always reflection 😉 well, or the firewall 😡
a
You don't need to rename them, you can set the name directly using
compileOptions.moduleName
c
excellent.
a
I'm guessing something in the generated code also references that file name. Otherwise kotlin-reflect would have to list all kotlin_module files
Which to be fair it might
c
from that comment it indicates the name is not currently used.
It should all line-up if you use
compileOptions.moduleName
a
I ended up adding this to our convention plugin:
Copy code
tasks.withType<KotlinCompile>().configureEach {
    compilerOptions {
        moduleName.set(path.removePrefix(":").replace(":", "_"))
    }
}
c
fwiw have this snippet to make modules unique when it comes to GAV coordinates (in a convention plugin for each module):
_group_ = "<prefix>" + _project_._path_._split_(":")._dropLast_(1)._joinToString_(".")