Convert Static method in Dagger Module class to Kotlin
up vote
2
down vote
favorite
I have following project in Github : https://github.com/Ali-Rezaei/TMDb-Paging which I use Dagger2 for dependency injection.
One of my Module classes is as Follow in java :
@Module
public abstract class DetailModule {
@FragmentScoped
@ContributesAndroidInjector
abstract DetailFragment detailFragment();
@Provides
@ActivityScoped
static Movie provideMovie(DetailActivity activity) {
return activity.getIntent().getExtras().getParcelable(EXTRA_MOVIE);
}
}
As you can see provideMovie method is static. When I convert it to Kotlin :
@Module
abstract class DetailModule {
@FragmentScoped
@ContributesAndroidInjector
internal abstract fun detailFragment(): DetailFragment
companion object {
@Provides
@ActivityScoped
internal fun provideMovie(activity: DetailActivity): Movie {
return activity.intent.extras.getParcelable(EXTRA_MOVIE)
}
}
}
But when I build the project I get following Kotlin compiler error :
error: @Provides methods can only be present within a @Module or @ProducerModule
public final com.sample.android.tmdb.vo.Movie provideMovie$app_debug(@org.jetbrains.annotations.NotNull()
Could be any solution to have the class in Kotlin?
add a comment |
up vote
2
down vote
favorite
I have following project in Github : https://github.com/Ali-Rezaei/TMDb-Paging which I use Dagger2 for dependency injection.
One of my Module classes is as Follow in java :
@Module
public abstract class DetailModule {
@FragmentScoped
@ContributesAndroidInjector
abstract DetailFragment detailFragment();
@Provides
@ActivityScoped
static Movie provideMovie(DetailActivity activity) {
return activity.getIntent().getExtras().getParcelable(EXTRA_MOVIE);
}
}
As you can see provideMovie method is static. When I convert it to Kotlin :
@Module
abstract class DetailModule {
@FragmentScoped
@ContributesAndroidInjector
internal abstract fun detailFragment(): DetailFragment
companion object {
@Provides
@ActivityScoped
internal fun provideMovie(activity: DetailActivity): Movie {
return activity.intent.extras.getParcelable(EXTRA_MOVIE)
}
}
}
But when I build the project I get following Kotlin compiler error :
error: @Provides methods can only be present within a @Module or @ProducerModule
public final com.sample.android.tmdb.vo.Movie provideMovie$app_debug(@org.jetbrains.annotations.NotNull()
Could be any solution to have the class in Kotlin?
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have following project in Github : https://github.com/Ali-Rezaei/TMDb-Paging which I use Dagger2 for dependency injection.
One of my Module classes is as Follow in java :
@Module
public abstract class DetailModule {
@FragmentScoped
@ContributesAndroidInjector
abstract DetailFragment detailFragment();
@Provides
@ActivityScoped
static Movie provideMovie(DetailActivity activity) {
return activity.getIntent().getExtras().getParcelable(EXTRA_MOVIE);
}
}
As you can see provideMovie method is static. When I convert it to Kotlin :
@Module
abstract class DetailModule {
@FragmentScoped
@ContributesAndroidInjector
internal abstract fun detailFragment(): DetailFragment
companion object {
@Provides
@ActivityScoped
internal fun provideMovie(activity: DetailActivity): Movie {
return activity.intent.extras.getParcelable(EXTRA_MOVIE)
}
}
}
But when I build the project I get following Kotlin compiler error :
error: @Provides methods can only be present within a @Module or @ProducerModule
public final com.sample.android.tmdb.vo.Movie provideMovie$app_debug(@org.jetbrains.annotations.NotNull()
Could be any solution to have the class in Kotlin?
I have following project in Github : https://github.com/Ali-Rezaei/TMDb-Paging which I use Dagger2 for dependency injection.
One of my Module classes is as Follow in java :
@Module
public abstract class DetailModule {
@FragmentScoped
@ContributesAndroidInjector
abstract DetailFragment detailFragment();
@Provides
@ActivityScoped
static Movie provideMovie(DetailActivity activity) {
return activity.getIntent().getExtras().getParcelable(EXTRA_MOVIE);
}
}
As you can see provideMovie method is static. When I convert it to Kotlin :
@Module
abstract class DetailModule {
@FragmentScoped
@ContributesAndroidInjector
internal abstract fun detailFragment(): DetailFragment
companion object {
@Provides
@ActivityScoped
internal fun provideMovie(activity: DetailActivity): Movie {
return activity.intent.extras.getParcelable(EXTRA_MOVIE)
}
}
}
But when I build the project I get following Kotlin compiler error :
error: @Provides methods can only be present within a @Module or @ProducerModule
public final com.sample.android.tmdb.vo.Movie provideMovie$app_debug(@org.jetbrains.annotations.NotNull()
Could be any solution to have the class in Kotlin?
asked 5 hours ago
Ali
4,5431650114
4,5431650114
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Comanion Object is technically different class and not annotated with @Module. (So you are getting that error)
you need to use JVM Annotations for methods. So Kotlin will create a static method inside DetailModule itself.
Try @JvmStatic
@Module
companion object {
@JvmStatic
@Provides
@ActivityScoped
internal fun provideMovie(activity: DetailActivity): Movie {
return activity.intent.extras.getParcelable(EXTRA_MOVIE)
}
}
Thanks, I get the same error when I build.
– Ali
4 hours ago
Can you try annotatingCompanion Objectwith@Module? I have updated the answer.
– I Don't Exist
4 hours ago
Yes, it worked, thanks again.
– Ali
4 hours ago
So what was happening is that -@JvmStaticindeed adds a static methodprovideMovietoDetailModuleitself. Butcompanion classwill also contains same method with@Providesannotation that was causing this issue sincecompanion classis not aModule. Adding@Moduletocompanion objectwill make this error go away.
– I Don't Exist
4 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Comanion Object is technically different class and not annotated with @Module. (So you are getting that error)
you need to use JVM Annotations for methods. So Kotlin will create a static method inside DetailModule itself.
Try @JvmStatic
@Module
companion object {
@JvmStatic
@Provides
@ActivityScoped
internal fun provideMovie(activity: DetailActivity): Movie {
return activity.intent.extras.getParcelable(EXTRA_MOVIE)
}
}
Thanks, I get the same error when I build.
– Ali
4 hours ago
Can you try annotatingCompanion Objectwith@Module? I have updated the answer.
– I Don't Exist
4 hours ago
Yes, it worked, thanks again.
– Ali
4 hours ago
So what was happening is that -@JvmStaticindeed adds a static methodprovideMovietoDetailModuleitself. Butcompanion classwill also contains same method with@Providesannotation that was causing this issue sincecompanion classis not aModule. Adding@Moduletocompanion objectwill make this error go away.
– I Don't Exist
4 hours ago
add a comment |
up vote
0
down vote
accepted
Comanion Object is technically different class and not annotated with @Module. (So you are getting that error)
you need to use JVM Annotations for methods. So Kotlin will create a static method inside DetailModule itself.
Try @JvmStatic
@Module
companion object {
@JvmStatic
@Provides
@ActivityScoped
internal fun provideMovie(activity: DetailActivity): Movie {
return activity.intent.extras.getParcelable(EXTRA_MOVIE)
}
}
Thanks, I get the same error when I build.
– Ali
4 hours ago
Can you try annotatingCompanion Objectwith@Module? I have updated the answer.
– I Don't Exist
4 hours ago
Yes, it worked, thanks again.
– Ali
4 hours ago
So what was happening is that -@JvmStaticindeed adds a static methodprovideMovietoDetailModuleitself. Butcompanion classwill also contains same method with@Providesannotation that was causing this issue sincecompanion classis not aModule. Adding@Moduletocompanion objectwill make this error go away.
– I Don't Exist
4 hours ago
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Comanion Object is technically different class and not annotated with @Module. (So you are getting that error)
you need to use JVM Annotations for methods. So Kotlin will create a static method inside DetailModule itself.
Try @JvmStatic
@Module
companion object {
@JvmStatic
@Provides
@ActivityScoped
internal fun provideMovie(activity: DetailActivity): Movie {
return activity.intent.extras.getParcelable(EXTRA_MOVIE)
}
}
Comanion Object is technically different class and not annotated with @Module. (So you are getting that error)
you need to use JVM Annotations for methods. So Kotlin will create a static method inside DetailModule itself.
Try @JvmStatic
@Module
companion object {
@JvmStatic
@Provides
@ActivityScoped
internal fun provideMovie(activity: DetailActivity): Movie {
return activity.intent.extras.getParcelable(EXTRA_MOVIE)
}
}
edited 4 hours ago
answered 5 hours ago
I Don't Exist
2,25511418
2,25511418
Thanks, I get the same error when I build.
– Ali
4 hours ago
Can you try annotatingCompanion Objectwith@Module? I have updated the answer.
– I Don't Exist
4 hours ago
Yes, it worked, thanks again.
– Ali
4 hours ago
So what was happening is that -@JvmStaticindeed adds a static methodprovideMovietoDetailModuleitself. Butcompanion classwill also contains same method with@Providesannotation that was causing this issue sincecompanion classis not aModule. Adding@Moduletocompanion objectwill make this error go away.
– I Don't Exist
4 hours ago
add a comment |
Thanks, I get the same error when I build.
– Ali
4 hours ago
Can you try annotatingCompanion Objectwith@Module? I have updated the answer.
– I Don't Exist
4 hours ago
Yes, it worked, thanks again.
– Ali
4 hours ago
So what was happening is that -@JvmStaticindeed adds a static methodprovideMovietoDetailModuleitself. Butcompanion classwill also contains same method with@Providesannotation that was causing this issue sincecompanion classis not aModule. Adding@Moduletocompanion objectwill make this error go away.
– I Don't Exist
4 hours ago
Thanks, I get the same error when I build.
– Ali
4 hours ago
Thanks, I get the same error when I build.
– Ali
4 hours ago
Can you try annotating
Companion Object with @Module ? I have updated the answer.– I Don't Exist
4 hours ago
Can you try annotating
Companion Object with @Module ? I have updated the answer.– I Don't Exist
4 hours ago
Yes, it worked, thanks again.
– Ali
4 hours ago
Yes, it worked, thanks again.
– Ali
4 hours ago
So what was happening is that -
@JvmStatic indeed adds a static method provideMovie to DetailModule itself. But companion class will also contains same method with @Provides annotation that was causing this issue since companion class is not a Module. Adding @Module to companion object will make this error go away.– I Don't Exist
4 hours ago
So what was happening is that -
@JvmStatic indeed adds a static method provideMovie to DetailModule itself. But companion class will also contains same method with @Provides annotation that was causing this issue since companion class is not a Module. Adding @Module to companion object will make this error go away.– I Don't Exist
4 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407039%2fconvert-static-method-in-dagger-module-class-to-kotlin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown