子组件(unscoped)可能不引用范围绑定:@Singleton @Provides @ org.jetbrains.annotations.NotNull

时间:2018-05-21 11:48:07

标签: android singleton dagger-2 dagger

我正在使用匕首2.11

模块

from datasketch import MinHash
from multiprocessing import Pool
from collections import defaultdict
from nltk import ngrams
import json
import sys
import codecs
import config

cores = 24
window_len = 12
step = 4
worker_files = 50
permutations = 256
hashband_len = 4

def minhash_text(args):
  '''Return a list of hashband strings for an input doc'''
  try:
    file_id, path = args
    with codecs.open(path, 'r', 'utf8') as f:
      f = f.read()
    all_hashbands = []
    for window_idx, window in enumerate(ngrams(f.split(), window_len)):
      window_hashbands = []
      if window_idx % step != 0:
        continue
      minhash = MinHash(num_perm=permutations, seed=1)
      for ngram in set(ngrams(' '.join(window), 3)):
        minhash.update( ''.join(ngram).encode('utf8') )
      hashband_vals = []
      for i in minhash.hashvalues:
        hashband_vals.append(i)
        if len(hashband_vals) == hashband_len:
          window_hashbands.append( '.'.join([str(j) for j in hashband_vals]) )
          hashband_vals = []
      all_hashbands.append(window_hashbands)
    return {'file_id': file_id, 'hashbands': all_hashbands}
  except Exception as exc:
    print(' ! error occurred while processing', file_id, exc)
    return {'file_id': file_id, 'hashbands': []}

if __name__ == '__main__':

  file_ids = json.load(open('file_ids.json'))
  file_id_path_tuples = [(file_id, path) for file_id, path in file_ids.items()]

  worker_id = int(sys.argv[1])
  worker_ids = list(ngrams(file_id_path_tuples, worker_files))[worker_id]

  hashband_to_ids = defaultdict(list)
  pool = Pool(cores)

  for idx, result in enumerate(pool.imap(minhash_text, worker_ids)):
    print(' * processed', idx, 'results')
    file_id = result['file_id']
    hashbands = result['hashbands']
    for window_idx, window_hashbands in enumerate(hashbands):
      for hashband in window_hashbands:
        hashband_to_ids[hashband].append(file_id + '.' + str(window_idx))

  with open(config.out_dir + 'minhashes-' + str(worker_id) + '.json', 'w') as out:
    json.dump(dict(hashband_to_ids), out)

活动绑定模块

@Module
class MyModule {
    @Provides
    fun provideString() : String = "yo"
    @Provides @Named("injector")
    fun provideInzectorString() : String = "named_injection"

    @Singleton @Provides //The error goes away if I remove @Singleton
    fun provideRepository() = Repository(Interceptor(),"")
}

AppComponent

@Module
abstract class ActivityBindingModule {
    @ContributesAndroidInjector(modules = [MyModule::class])
     abstract fun suggestionActivity() : SuggestionsActivity

    @ContributesAndroidInjector(modules = [MyModule::class])
    abstract fun editSubscriptionActivity() : SubscribeActivity
}

我在编译时遇到此错误

@Singleton
@Component(modules = {
        AndroidInjectionModule.class,
        MyModule.class
})
interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(MyApplication application);

        AppComponent build();
    }

    void inject(MyApplication app);
}

我见过这些解决方案12。两者都要求您使用我已经在做的SubscribeActivitySubcomponent (unscoped) may not reference scoped bindings: @Singleton @Provides @org.jetbrains.annotations.NotNull 注释你的app组件。我的代码出了什么问题?

1 个答案:

答案 0 :(得分:4)

问题是MyModule的范围(应用程序或单例)大于活动范围。

 @ContributesAndroidInjector(modules = [MyModule::class])
 abstract fun suggestionActivity() : SuggestionsActivity

删除两个(modules = [MyModule::class])或定义特定于活动的模块。

此处您不需要MyModule。这是多余的,因为它已经包含在AppComponent