Dealing with kotlin_module conflict when building apk for project with non unique project names

I have the following Android multi-module project structure:

/android
   /feature1
     /foo
     /bar
   /feature2
     /foo
     /bar

The structure above allows me to cleanly organize files for my modules by feature.
It also works well in the IDE as I can specify :feature1:foo and feature2:foo in the settings.gradle file and they appear as feature1-foo and feature2-foo in the IDE navigation views which is great!

However this structure runs into a problem when building an APK with the following error:

More than one file was found with OS independent path ‘META-INF/foo_debug.kotlin_module’

This appears to be happening due to the fact that the above file is using kotlin compiler’s module-name which appears to be set to the Gradle project.name by default. Per Gradle docs Project name doesn’t have to be unique thus leading to the conflict in my desired project structure.

Here’s some online resource that also mentions this problem along with some solutions:

Per the above and other online discussions I’m working around this problem by adding this to my root project:

subprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            freeCompilerArgs += ['-module-name', project.path]
        }
    }
}

This now generates feature1/foo.kotlin_module and feature2/foo.kotlin_module.
Note that though / separator however is not a path but rather part of the actual file name.

Questions:

  • Does anyone feel the above workaround is safe? Everything builds and app deploys fine but I’m wondering if the workaround may produce issues later on.

  • Would it be reasonable to propose that this issue be fixed such that this workaround isn’t needed? I’m not sure though who owns the root cause of the problem though. The argument for fixing that might make sense is that whatever generates the kotlin_module file should rely on a unique property from a Gradle project (ex: project path) as opposed to a non unique name (project name).

1 Like

We’ve run into this same issue and are thinking of using your workaround. Have you run into any problems doing this?