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?










share|improve this question


























    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?










    share|improve this question
























      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?










      share|improve this question













      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?







      android kotlin dagger-2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 5 hours ago









      Ali

      4,5431650114




      4,5431650114
























          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)
          }
          }





          share|improve this answer























          • 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










          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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)
          }
          }





          share|improve this answer























          • 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










          • 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















          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)
          }
          }





          share|improve this answer























          • 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










          • 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













          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)
          }
          }





          share|improve this answer














          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)
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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










          • 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


















          • 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










          • 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
















          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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)