kezong / fat-aar-android

A gradle plugin that merge dependencies into the final aar file works with AGP 3.+
MIT License
3.13k stars 622 forks source link

Merge kotlin_module files when embedding aars #186

Open aasitnikov opened 4 years ago

aasitnikov commented 4 years ago

Currently, fat-aar plugin discards *.kotlin_module files (located in META-INF folder) of embedded modules. But they store info about top-level extension functions (and maybe regular top-level functions), and if *.kotlin_module file discarded, compiler won't be able to find these functions. This blog post has some more explanation.

*.kotlin_module files can be read using handy kotlinx-metadata-jvm library.

boiler23 commented 3 years ago

Same issue here. Is there any workaround?

kezong commented 3 years ago

I'm not good at kotlin. I hope someone can pull request. If anyone can implement this enhancement, I will be very grateful.

kezong commented 3 years ago

It is supported in 1.3.1

boiler23 commented 3 years ago

Great, thanks! Gave it a brief testing - seem to work nicely!

diegoperini commented 3 years ago

Was this causing a compilation or runtime error?

aasitnikov commented 3 years ago

Was this causing a compilation or runtime error?

Without kotlin_module, if you would use your fat-aar artifact from other project, then Kotlin compiler wouldn't be able to find top-level extensions, and build would fail with "Unresolved reference"

aasitnikov commented 3 years ago

I tested kotlin_module support it in my project - it seems to be working, if extension is declared directly in module with fat-aar plugin applied, but not if it's declared in embedded aar. I tried to manually edit resulting aar and put second kotlin_module inside classes.jar, but it doesn't seem to work

kezong commented 3 years ago

I tested kotlin_module support it in my project - it seems to be working, if extension is declared directly in module with fat-aar plugin applied, but not if it's declared in embedded aar. I tried to manually edit resulting aar and put second kotlin_module inside classes.jar, but it doesn't seem to work

I tested in example, It seem ok, you can check it.

https://github.com/kezong/fat-aar-android/blob/33853ff6054a83d88b34ff0fdd54ba676388a7f6/example/lib-aar/src/main/java/com/kezong/demo/libaar/KotlinTest.kt#L1-L8

aasitnikov commented 3 years ago

I see now. In the :app module there's only one class - MainActivity.java. The original problem was about not being able to use top-level functions from Kotlin, if these functions was declared in module that was embeded. With version 1.3.1 it's working, only if top-level functions are declared in the module with fat-aar plugin applied.

Kotlin compiles top-level functions and properties into static functions of some "synthetic" class (it would be KotlinTestKt in example above, but you've changed it with annotation to KotlinTest2), and puts information about these functions and properties into kotiln_metadata file, so that other modules would be able to discover them. That's why these functions can be discovered from Java, but not from Kotlin, if kotlin_metadata is missing or corrupted.

To see the issue, you need to add some Kotlin file to :app module, and to try to call test2(), for example.