Search code examples
javakotlinandroid-studiogradleandroid-library

Disable dollars in names in Android Studio when building Android Library


When creating and building an Android Library in Android Studio (gradle) from kotlin, the java auto generating tool puts dollars in classes and methods names.

It's acceptable in Java but I have problems when trying to use it in C# Android Binding Library.

Is there a way to custom Android Studio (gradle) generation, to change the dollar by any other symbol or just change the way it generates Java code from Kotlin code ?

Edit:
I should add that I am using Compose Composable and that this is, in fact, the core of the problem.

Here is an example of my Kotlin code:

open class ExampleActivity : ComponentActivity() {

    @Composable
    fun Greeting(name: String) {
        Text(text = "Hello $name!")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(
            ComposeView(this).apply {
                setContent {
                    MaterialTheme {
                        Greeting(name = "compose")
                    }
                }
            }
        )
    }
}

and an extract of the resulting java code, containing boring dollars:

private static final Unit Greeting$lambda$0(ExampleActivity $tmp0_rcvr, String $name, int $$changed, Composer $composer, int $force) {
    $tmp0_rcvr.Greeting($name, $composer, RecomposeScopeImplKt.updateChangedFlags($$changed | 0x1));
    return Unit.INSTANCE;
  }

or

static final class ExampleActivity$onCreate$1$1 implements Function2<Composer, Integer, Unit> {
    @Composable
    @ComposableTarget(applier = "androidx.compose.ui.UiComposable")
    public final void invoke(Composer $composer, int $changed) {
      ComposerKt.sourceInformation($composer, "C30@791L74,30@777L88:ComposeBind.kt#3l02gn");
      if (($changed & 0x3) != 2 || !$composer.getSkipping()) {
...

Since I asked the question, I found that the way Compose works creates lambdas and anonymous functions. And this autogenerated code contains dollars.

I don't really think there is a solution to this, but if anyone has an idea it would be great.


Solution

  • To close the question, and for anyone it could help, i'll answer what i found.

    The reason why I wanted to "disable dollars in name" is that, when Binding with the Android Binding Library, warnings were issued and binds were skipped.

    The fact is that, thoses binds were useless. Because even if they were skipped, the android library itself still had access to the components (for example, the composable Greeting). And importants things like the activity were binded anyways, and so, was useable with C#.

    So the problem was a non problem.

    If you are trying to bind an Android Library and face the same warnings, they probably aren't important, and the best manner is to take care of everything in the metadata.xml of your Android Binding Library.

    See https://learn.microsoft.com/en-us/dotnet/android/binding-libs/customizing-bindings/java-bindings-metadata
    and most importantly

    https://saratsin.medium.com/how-to-bind-a-complex-android-library-for-xamarin-with-sba-9a4a8ec0c65f
    basically removing everything in the package, then only adding manually what is important to expose from your android library

    That is for the case where all warnings are about non important components. If your important components are skipped, you should understand that binding components from your android library, in java or kotlin, that have java specific things like for example parameters types, is not possible (afaik). You should try to wrap them into less specific and more bindable components.

    For example, it's not possible to bind and expose a Composable, because of the auto generated lambda with a dollar in the name. That's why I wrapped it in a ComponentActivity, that is bindable for C#.

    Hope that'll help