dagger-2

Android Dagger2 + OkHttp + Retrofit dependency cycle error

北城以北 提交于 2019-11-28 08:26:56
Hey there I am using Dagger2 , Retrofit and OkHttp and I am facing dependency cycle issue. When providing OkHttp : @Provides @ApplicationScope OkHttpClient provideOkHttpClient(TokenAuthenticator auth,Dispatcher dispatcher){ return new OkHttpClient.Builder() .connectTimeout(Constants.CONNECT_TIMEOUT, TimeUnit.SECONDS) .readTimeout(Constants.READ_TIMEOUT,TimeUnit.SECONDS) .writeTimeout(Constants.WRITE_TIMEOUT,TimeUnit.SECONDS) .authenticator(auth) .dispatcher(dispatcher) .build(); } When providing Retrofit : @Provides @ApplicationScope Retrofit provideRetrofit(Resources resources,Gson gson,

Can a Dagger 2 dependency be non-injectable?

点点圈 提交于 2019-11-28 08:19:50
问题 Is there a way to tell Dagger 2 how to provide something, but not allow it to be injected? Say I want to inject a Qux . A Qux requires a Foo and a Bar : @Provides @Singleton Foo foo() { return new Foo(); } @Provides @Singleton Bar bar() { return new Bar(); } @Provides @Singleton Qux qux(final Foo foo, final Bar bar) { return new Qux(foo, bar); } But what if I don't want Foo and Bar to be injectable? Perhaps exposing them would break the encapsulation of the Qux , or perhaps they're factories

Is there any way of making IntelliJ IDEA recognizing Dagger 2 generated classes in a Java project?

左心房为你撑大大i 提交于 2019-11-28 08:18:30
Context I have started a personal project in java with Gradle as the build system and I want to use Dagger 2 as a DI. The main reason of doing that is to get used to that library and be able to use it easily in bigger projects. What have I tried I've managed to make the Google sample runs on IntelliJ IDEA Problem IntelliJ IDEA keeps telling me that it cannot resolve the generated class (in this case DaggerCoffeeApp_Coffee ). It's a bit annoying not to know if the written code is correct (specially when you are learning to use Dagger 2). All java classes are the same as the Google sample . Here

LiveData is not updating its value after first call

自古美人都是妖i 提交于 2019-11-28 07:37:20
I have been beating my head against the wall and I cannot understand why this is happening. I am working with the new Architectural Components for Android and I am having problems updating a LiveData with a List of Objects. I have two spinners. When i change the option in the first one, The second one must have its content changed. But this last part is not happening. Can anyone help me? State.java @Entity(tableName = "states") public class State{ @PrimaryKey(autoGenerate = false) private int id; private String name; @ColumnInfo(name = "countryId") private String CountryId; @Ignore private

Dagger 2 static provider methods in kotlin

佐手、 提交于 2019-11-28 07:10:02
With the recent versions of dagger 2 one of the improvements made are the possibility of having static provide methods. Simply so: @Provides static A providesA() { return A(); } I was wondering how does one go about doing this in kotlin? I've tried @Module class AModule { companion object { @JvmStatic @Provides fun providesA(): A = A() } } But I get the error message: @Provides methods can only be present within a @Module or @ProducerModule I'm guessing there's something going on here with the companion object, however I'm quite new to Kotlin and I'm unsure of how one can do this. Is it even

Kotlin + Dagger - inject Map for ViewModel factory

核能气质少年 提交于 2019-11-28 07:04:02
I'm using the new Architecture Components with Dagger2 and I would like to inject my ViewModels using a Factory class. The Factory class is itself injectable. This all works well when the Factory class is defined in Java, but when I convert it to Kotlin, Dagger2 does not know how to generate the Map for the constructor, while in Java it knows how to do so. I presume the difference is that, after conversion, the Factory class uses the Map from the kotlin package, as opposed to from java.util.Map package. How can I get Dagger2 to generate the map for the Factory constructor? Here's the Factory

Scopes in Dagger 2

我的未来我决定 提交于 2019-11-28 05:50:53
I probably missed something, but I thought Scopes like @Singleton are used to define "scoped lifecycles". I use Dagger 2 in an Android app (but I don't think the problem is android related at all). I have 1 Module: @Module public class MailModule { @Singleton @Provides public AccountManager providesAccountManager() { return new AccountManager(); } @Singleton @Provides public MailProvider providesMailProvider(AccountManager accountManager) { return new MailProvider(accountManager); } } I have two different components with @Singleton scope: @Singleton @Component(modules = MailModule.class)

ViewModelProviders with Dagger 2, not able to grasp the concept

江枫思渺然 提交于 2019-11-28 04:46:16
问题 I have a Retrofit Service like this public interface BrandsService { @GET("listBrand") Call<List<Brand>> getBrands(); } Then I have a Repository to get data from an api like this public class BrandsRepository { public static final String TAG = "BrandsRepository"; MutableLiveData<List<Brand>> mutableLiveData; Retrofit retrofit; @Inject public BrandsRepository(Retrofit retrofit) { this.retrofit = retrofit; } public LiveData<List<Brand>> getListOfBrands() { // Retrofit retrofit = ApiManager

Component which dependent from other components with different scopes (component hierarchies with different scopes)

此生再无相见时 提交于 2019-11-28 04:41:50
问题 I have complex multi-tier architecture in my Android project. Currently i want to use the following structure of the DI components and modules: [Data Layer] @DataScope //scope is used for caching (Singleton) some Data Layer entities for whole application - DataComponent //exposes just interfaces which should be used on the BL Layer //Modules exposes entities for internal (Data Layer) injections and entities which exposed by DataComponent for BL Layer * DataModule1 * DataModule2 * DataModule3

Can I just inject super class when use dagger2 for dependency injection?

不想你离开。 提交于 2019-11-28 04:19:43
I use Dagger2 for DI in my android application. I found that I have to write inject method for every class that uses @Inject field. Is there a way that I can just inject the parent class so that I don't have to call inject on every subclass? Take Activity for example. I have a BaseActivity that that every Activity extends from. Is there a way that I can just create an inject method in the component for BaseActivity and just call inject in BaseActivity's onCreate, and @inject fields in sub activities get injected automatically? Kirill Boyarshinov It cannot be done right now. Explanation by