I was trying my hand learning dagger 2, tried to convert the basic java example into kotlin but unable to do so because of the following error I am getting...
error: Dagger does not support injection into private fields private com.example.Engine engine;
Here is the Java code which is working fine,
public class Car {
private Engine engine;
private Wheel wheel;
@Inject
public Car(Engine engine, Wheel wheel) {
this.engine = engine;
this.wheel = wheel;
}
}
Car.java class with having 2 dependency.
public class Engine {
@Inject
Engine(){}
}
public class Wheel {
@Inject
Wheel(){}
}
Here is my kotlin code :-
class Car(@Inject var engine: Engine, @Inject val wheels: Wheels) {
fun drive(){
Log.d("CAR","<<<<<< DRIVING >>>>>")
}
}
class Engine @Inject constructor() {}
class Wheels @Inject constructor() {}
@Component
interface CarComponent {
fun getCar() : Car
}
This is the generated code
public final class Car {
@org.jetbrains.annotations.NotNull()
@javax.inject.Inject()
private com.toi.roboelectric.Engine engine;
@org.jetbrains.annotations.NotNull()
@javax.inject.Inject()
private final com.toi.roboelectric.Wheels wheels = null;
public final void drive() {
}
@org.jetbrains.annotations.NotNull()
public final com.toi.roboelectric.Engine getEngine() {
return null;
}
public final void setEngine(@org.jetbrains.annotations.NotNull()
com.toi.roboelectric.Engine p0) {
}
@org.jetbrains.annotations.NotNull()
public final com.toi.roboelectric.Wheels getWheels() {
return null;
}
public Car(@org.jetbrains.annotations.NotNull()
com.toi.roboelectric.Engine engine, @org.jetbrains.annotations.NotNull()
com.toi.roboelectric.Wheels wheels) {
super();
}
}
Please update Car Class as
class Car @Inject constructor(var engine: Engine,val wheels: Wheels) {
fun drive(){
Log.d("CAR","<<<<<< DRIVING >>>>>")
}