Remove getters and setters from kotlin code
Asked Answered
N

2

6

For this code

class Foo {
    var name: String? = null
}

kotlin compiler generates:

private String name;
public final String getName() { ... }
public final void setName(String name) { ... }

Even if property name doesn't have custom getter or setter. It's possible to remove redundant get and set methods using @JvmField annotation. So this code

class Foo {
    @JvmField
    var name: String? = null
}

generates only one field without additional methods.

public String name;

But is there any way to make kotlin compiler not generate getters and setters for all properties in entire project? No matter if it has annotation or not. Maybe some experimental compiler flags?

I want it because I have android instrumentation tests written on kotlin. Test apk exceeds 65k method count limit. About 2k of methods is generated getters/setters. I cannot use proguard or multidex due to some bugs in android build system. So removing kotlin synthetic methods will help me a lot.

Nosography answered 22/11, 2017 at 23:46 Comment(6)
this page: developer.android.com/studio/build/multidex.html#testing and couple of comments in google issue tracker say that "Using multidex to create a test APK is not currently supported."Nosography
Remove synthetic getters and setters is just a one step to optimize method count in my apk. But if there is compiler options or something like that it would be great. I can easily remove 2k methods.Nosography
Maybe my question isn't clear. Not my app apk exceeds limit. It easily can be solved with multidex. Apk that contains all my test code and test libs exceeds limit. And this apk cannot be proguarded nor multidexed due to android build system bugs.Nosography
Removed comment, as I see what you're trying to achieve! Can you not use minifyEnabled true with useProguard false in your build configuration?Grayce
There is no such compiler option. d8 (the replacement for dex tool) might be able to remove accessors, but I'm not sure if the functionality is currently implemented.Couvade
Many thanks for your answer. I'll try d8Nosography
S
2

You can simply declare your properties as private, they won’t have getters and setters generated then.

class Foo {
  private val foo: String = "foo"
}

This will generate the following class:

→ javap -classpath build/classes/kotlin/test -p test.Foo
Compiled from "Foo.kt"
public final class test.Foo {
  private final java.lang.String foo;
  public test.Foo();
}
Stinker answered 24/11, 2017 at 8:47 Comment(1)
It is not a solutionFeet
W
0

There is currently no Kotlin compiler option that can globally disable the generation of getters and setters across an entire project. Kotlin generates these synthetic methods by default to maintain interoperability with Java, as the JVM does not natively support properties like Kotlin does. These synthetic methods (getters and setters) are a standard way to represent properties in JVM bytecode.

The closest you can get to suppressing the generation of these methods is by using the @JvmField annotation on individual properties. This annotation exposes the property as a public field without generating getters and setters, but it requires manual application to each property where you want this behavior.

https://kotlinlang.org/docs/java-interop.html#getters-and-setters

Woodborer answered 3/9 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.