dagger-2

How to exclude Dagger2 classes from test coverage

人盡茶涼 提交于 2019-12-06 17:15:56
问题 Is there any option to exclude Dagger2 classes from test coverage report in Android Studio 回答1: JaCoCo excludes If you're using JaCoCo, for example using android instrumentation connected tests , you need to configure the excludes (or includes), which, according to the documentation is... A list of class files to exclude from the report. May use wildcard characters (* and ?). When not specified nothing will be excluded. Which means you need to match the generated dagger class names. The

dagger2 cannot find symbol

瘦欲@ 提交于 2019-12-06 16:15:12
问题 I'm following the Dagger2 sample of TODO app but encounted with 2 errors. Error1: can't find symbol DaggerNetComponent . (Which is actually there) Error2: Sharedpreference can't be provided without @provider-annotated method.(Which I think results from error1) And here's my long but simple code: Three modules: @Module public class AppModule { private final Application mApplication; AppModule(Application application) { mApplication = application; } @Provides Application provideApplication() {

Espresso testing with Dagger 2 and custom scopes

拜拜、爱过 提交于 2019-12-06 11:23:07
After a recent migration to Dagger 2, the app I am working on is using an @ActivityScope for every feature. Each app feature is implemented using MVP pattern and has it's own local dagger Component setup which depends on the Application component for the dependencies that are required during the entire app lifecycle (provided by the App). Each feature’s Activity extends a base class which provides the main application component to a method that is overridden by each activity in order to set up the local dagger component (builds the local component and instantiates the local module). The issue

Dagger 2 - Lack of Constructor Injection example

喜你入骨 提交于 2019-12-06 11:13:04
I spend several hours for searching but I still don't find any clearly example about Constructor Injection with Dagger 2. Assume I have below declaration, how can I create an instance of class B somewhere using Dagger 2 injection? @Module public class ClassA { @Provides public ClassA provideA(){ return new ClassA(); } } public class ClassB{ private ClassA a; @Inject public ClassB(ClassA a){ this.a = a; } } If you have @Module public class ModuleA { /*unscoped*/ @Provides public ClassA provideA(){ return new ClassA(); } } /*unscoped*/ public class ClassB{ private ClassA a; @Inject public ClassB

PowerMock + Robolectric + Dagger2

大兔子大兔子 提交于 2019-12-06 08:48:01
I test custom view class which contain: android ui elements some logic static methods callings dagger2 dependencies So i use next tools for testing Robolectric for UI elements mocking unit tests for logic testing PowerMock for static methods mocking Robolectric + PowerMock integration problem is known and solution is known - https://github.com/robolectric/robolectric/wiki/Using-PowerMock But with this solution dagger2 dependencies fail. Attention to code. My custom view: public class ProgressTextView extends TextView { private String defaultText; private int fileSize; private String

Is “inject everything” a bad practice in Android?

南楼画角 提交于 2019-12-06 08:04:33
问题 Studying about dependency injection I found some approaches that suggests to inject everything and other saying that it's not necessary to do so. In my current project, my rule of thumb regarding Dependency Injection is " if the class was created by me, I make it injectable ". In other words only classes like SimpleDateFormat , ArrayList , HashMap are newables in my project. My intent doing this approach is that I can @Inject any class anywhere once calling Injector.getApplicationComponent()

Disable Dagger injection in tests

南笙酒味 提交于 2019-12-06 06:47:13
问题 I have the following LoginFragment that uses Dagger to inject its fields: class LoginFragment : DaggerFragment() { @Inject lateinit var viewModelFactory: ViewModelProvider.Factory override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) viewModel = ViewModelProviders.of(this, viewModelFactory) .get(LoginViewModel::class.java) } I also have a corresponding test that mocks the LoginViewModel according to the documentation from Google: "You can

Why it still works without installing AndroidInjectionModule?

女生的网名这么多〃 提交于 2019-12-06 06:11:31
问题 According to Dagger documentation about injecting activity objects, it says that installing AndroidInjectionModule in your application component. However, everything is fine without it. Does it means that I don't need to declare it? Under what circumstances will it be wrong? For example: Injected instance data class Food(val name: String) Module @Module class FoodModule{ @Provides fun provideFood(): Food { return Food("cholocate") } } BindingModule @Module abstract class MainActivityModule {

Dagger 2 injecting two retrofit objects

℡╲_俬逩灬. 提交于 2019-12-06 03:55:59
I'm using Dagger 2 with retrofit2 library while using MVP. Everything went well till I tried to integrate another service (basically I tried to initialize another retrofit object to another service). I followed this answer but without any success. Every time I'm getting an errors that each of my fragments and application classes don't seem to recognize the component classes. error: cannot find symbol class DaggerApplicationComponent error: cannot find symbol class DaggerEpisodeComponent Code ApplicationComponent @Singleton @Component(modules = ApplicationModule.class) public interface

Accessing strings.xml from ViewModel

﹥>﹥吖頭↗ 提交于 2019-12-06 02:59:55
I am using Dagger 2 DataBindng and the new Android Lifecycle components, which have ViewModels . Inside my ViewModel how could I get access to my strings.xml? I was thinking at first, to inject a Context into the viewModel , however, this will just leak memory. Are there any other ways? There is an AndroidViewModel , which receives Application instance as parameter. From docs: Application context aware ViewModel . Subclasses must have a constructor which accepts Application as the only parameter. You can retrieve a string from strings.xml using that parameter. The repo in the link, however