Dagger和Dagger 2.0有什么区别,为什么Google决定分叉现有项目?
我正在尝试将我的 android 上下文从一个模块注入另一个模块。到目前为止,这是我的代码:
用户配置文件模块.java
@Module(
library = true
)
public class UserProfileModule {
@Inject Context _context;
@Provides
public AccountUtils.UserProfile provideUserProfile() {
return AccountUtils.getUserProfile(_context);
}
}
Run Code Online (Sandbox Code Playgroud)
根模块.java
@Module(
injects = {
PizzaApplication.class,
UserProfileModule.class,
MainActivity.class
},
includes = {
UserProfileModule.class
},
library = true
)
public class RootModule {
private final Context _context;
public RootModule(Context context) {
_context = context;
}
@Provides
@Singleton
public Context provideApplicationContext() {
return _context;
}
}
Run Code Online (Sandbox Code Playgroud)
每当它尝试获取用户配置文件时,它都会失败,说该对象为空。]
编辑:
比萨应用程序
public class PizzaApplication extends Application {
private ObjectGraph …Run Code Online (Sandbox Code Playgroud) 我正在尝试将Dagger 2用于我的应用程序,但我遇到了有关实体存储库的一些问题,而我还没有弄明白我错过了什么.
这是我的应用程序组件:
@Singleton
@Component(
modules = {
AndroidModule.class,
RepositoryModule.class
}
)
public interface ApplicationComponent {
void inject(AndroidApplication app);
IDependenceyRepository dependencyRepository();
}
Run Code Online (Sandbox Code Playgroud)
我的模块:
@Module
public class RepositoryModule {
@Provides @Singleton IDependenceyRepository provideDependendencyRepository(Context context) {
return new DependencyRepository(context);
}
}
@Module
public class AndroidModule {
private final AndroidApplication app;
public AndroidModule(AndroidApplication app) {
this.app = app;
}
@Provides @Singleton Context provideApplicationContext() {
return app;
}
}
Run Code Online (Sandbox Code Playgroud)
我的AndroidApplication:
public class AndroidApplication extends Application {
private ApplicationComponent component;
@Override
public void onCreate() { …Run Code Online (Sandbox Code Playgroud) 我正在迁移到新的匕首android 2.11
所有设置都基于Google蓝图:MVP-Dagger.但我遇到此错误:
错误:(22,57)错误:@Binds方法必须只有一个参数,其类型可分配给返回类型
在这一行:
@ActivityScoped
@Binds abstract PresenterFactory<MainContract.Presenter> providePresenterFactory(MainPresenter presenter);
Run Code Online (Sandbox Code Playgroud)
主持人:
@ActivityScoped
public class MainPresenter extends BasePresenterImpl<MainContract.View>
implements MainContract.Presenter { public MainPresenter(String s) {..
} ... }
Run Code Online (Sandbox Code Playgroud)
有人知道如何解决这个问题吗?谢谢.
我正在尝试使用支持v4片段创建一个简单的匕首2应用程序.修改我的应用程序后,我得到了这个奇怪的编译错误
Error:(35, 8) error: [dagger.android.AndroidInjector.inject(T)] java.util.Map<java.lang.Class<? extends android.support.v4.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>> cannot be provided without an @Provides-annotated method.
java.util.Map<java.lang.Class<? extends android.support.v4.app.Fragment>,javax.inject.Provider<dagger.android.AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>> is injected at
dagger.android.DispatchingAndroidInjector.<init>(injectorFactories)
dagger.android.DispatchingAndroidInjector<android.support.v4.app.Fragment> is injected at
app.series.com.tvshowsapplication.ui.main.MainActivity.fragmentDispatchingAndroidInjector
app.series.com.tvshowsapplication.ui.main.MainActivity is injected at
dagger.android.AndroidInjector.inject(arg0)
Run Code Online (Sandbox Code Playgroud)
快速搜索后,我找到了这个链接.它说我需要使用
事情是我不明白我应该把这部分放在哪里
@Multibinds
abstract Map<Class<? extends android.app.Fragment>, AndroidInjector.Factory<? extends android.app.Fragment>> bindNativeFragments();
Run Code Online (Sandbox Code Playgroud)
我的猜测我需要将它放在BaseActivity中,因为它是一个抽象函数,但我如何在主活动中实现它?
我的主要活动:
public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> implements MainNavigator, HasSupportFragmentInjector {
@Inject
ViewModelProvider.Factory mViewModelFactory;
@Inject
DispatchingAndroidInjector<Fragment> fragmentDispatchingAndroidInjector;
private MainViewModel mMainViewModel;
private DrawerLayout mDrawer;
private Toolbar mToolbar;
private NavigationView mNavigationView;
// …Run Code Online (Sandbox Code Playgroud) 我正在使用Dagger 2.16,并在我的Dagger 实现中关注了本文。直到我只有一个Activity(HomeActivity)为止,一切都在此实现中运行良好。我一开始在SplashScreenActivity中实现Dagger。我开始收到此错误。这是我项目中的一些代码
AppComponent.kt
@Singleton
@Component(modules = [
AndroidInjectionModule::class,
AppModule::class,
ActivityBuilder::class,
ServiceBuilder::class,
BroadcastRecieverBuilder::class])
interface AppComponent : AndroidInjector<MyApp> {
@Component.Builder
abstract class Builder : AndroidInjector.Builder<MyApp>()
}
Run Code Online (Sandbox Code Playgroud)
AppModule.kt
@Module()
class AppModule {
@Provides
@Singleton
fun provideContext(application: MyApp): Context {
return application
}
@Provides
@Singleton
fun provideRestService(retrofit: Retrofit): RestService {
return retrofit.create(RestService::class.java)
}
...
}
Run Code Online (Sandbox Code Playgroud)
ActivityBuilder.kt
@Module
abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = [HomeActivityModule::class])
@PerActivity
abstract fun bindHomeActivity(): HomeActivity
@ContributesAndroidInjector(modules = [SplashScreenModule::class])
@PerActivity
abstract fun bindSplashActivity(): SplashScreenActivity
}
Run Code Online (Sandbox Code Playgroud)
BaseActivity.kt
abstract class …Run Code Online (Sandbox Code Playgroud) 我不明白如何解决这个错误。在尝试将片段添加到我的应用程序并使用 Dagger 进行 DI 后,我收到此错误。这是错误堆栈:
错误:[Dagger/IncompatifiableScopedBindings] di.component.ApplicationComponent (unscoped) 可能不引用作用域绑定:@Provides @di.ApplicationContext @di.ApplicationScope android.content.Context di.Module.ApplicationContextModule.getApplicationContext(application.MyApplication) @Provides @di.ApplicationScope android.content.SharedPreferences di.Module.SharedPreferencesModule.getSharedPreferences(@di.ApplicationContext android.content.Context) @Provides @di.ApplicationScope service.KeyStoreServiceInterface di.Module.KeyStoreModule.getKeyStoreService(@Named("KEY_STORE_FILE") ) java.io.File) @Provides @di.ApplicationScope repository.SharedPreferencesHelper di.Module.SharedPreferenceHelperModule.getSharedPreferencesHelper() @Provides @di.ApplicationScope service.CoinmarketcapService di.Module。CoinmarketcapModule.getCoinmarketcapService(com.google.gson.Gson, okhttp3.OkHttpClient) @Provides @di.ApplicationScope com.google.gson.Gson di.Module.GsonModule.getGson() @Provides @di.ApplicationScope okhttp3.OkHttpClient di.Module .OkHttpModule.getOkHttpClient() @Provides @di.ApplicationScope repository.WalletRepositoryInterface di.Module.WalletRepositoryModule.getWalletRepository(repository.SharedPreferencesHelper, service.KeyStoreService)SharedPreferencesHelper, service.KeyStoreService)SharedPreferencesHelper, service.KeyStoreService)
这是我的 ApplicationComponent 类:
@Component(modules = {ApplicationContextModule.class,
SharedPreferencesModule.class,
KeyStoreModule.class,
SharedPreferenceHelperModule.class,
AndroidInjectionModule.class,
BindModule.class,
AndroidSupportInjectionModule.class,
OkHttpModule.class,
GsonModule.class,
CoinmarketcapModule.class,
WalletRepositoryModule.class})
@SuppressWarnings("unchecked")
public interface ApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(MyApplication myApplication);
ApplicationComponent build(); …Run Code Online (Sandbox Code Playgroud) dagger-android 2.16
Run Code Online (Sandbox Code Playgroud)
我的Dagger模块中有一个依赖周期错误。我想我知道问题出在哪里,但不确定如何解决。
这是错误消息:
Found a dependency cycle:
public interface LoginFragmentSubcomponent extends AndroidInjector<LoginFragment> {
presentation.login.request.LoginRequest is injected at
mobileui.login.di.LoginActivityModule.provideLoginResponseListener(…, loginRequest)
presentation.login.response.LoginResponseListener is injected at
mobileui.login.di.LoginActivityModule.provideLoginRequest(…, loginPresenter)
presentation.login.request.LoginRequest is injected at
mobileui.login.di.LoginActivityModule.provideLoginPresenter(…, loginRequest)
mobileui.login.LoginPresenter is injected at
mobileui.login.LoginFragment.loginPresenter
Run Code Online (Sandbox Code Playgroud)
这是我收到错误的下面的模块
@Module
class LoginActivityModule {
@Reusable
@Provides
fun provideLoginPresenter(loginRequest: LoginRequest): LoginPresenter {
return LoginPresenterImp(loginRequest)
}
@Reusable
@Provides
fun provideLoginResponseListener(loginRequest: LoginRequest): LoginResponseListener {
LoginPresenterImp(loginRequest)
}
@Reusable
@Provides
fun provideLoginRequest(loginUser: LoginUser,
loginPresenter: LoginResponseListener): LoginRequest {
return LoginRequestImp(loginUser, loginPresenter)
}
}
Run Code Online (Sandbox Code Playgroud)
我的LoginPresenterImp实现了LoginResponseListener,我想将其传递给LoginRequestImp类,以便可以将其用作回调。
class LoginPresenterImp(private val loginRequest: …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 android 项目并使用 Dagger2 进行依赖注入?我正在尝试将 Fragment 注入到 Activity 中,但我不想使用 newInstance或new SomeFragment创建片段实例,并且希望该实例由 dagger 创建并注入。
我也想知道如果想传递一些参数,我该怎么做。
我从 Dagger 2.21 迁移,Builder 更改为 Factory。应该如何使用 AndroidInjector.Factory ?我有这个组件
interface AppComponent : AndroidInjector<MainApplication> {
@Component.Factory
interface Factory : AndroidInjector.Factory<MainApplication> {
fun create(@BindsInstance emailId: String) : AppComponent
}
}
Run Code Online (Sandbox Code Playgroud)
但它失败了“错误:@Component.Factory 类型必须只有一个抽象方法。已经找到:create(T)”。
AndroidInjector.Factory 似乎已经有一个绑定 MainApplication 实例的创建。我应该如何绑定另一个值?我发现的唯一解决方法不是像这样从 AndroidInjector.Factory 扩展,但为什么要使用 AndroidInjectory.Factory?
interface AppComponent : AndroidInjector<MainApplication> {
@Component.Factory
interface Factory {
fun create(@BindsInstance instance: MainApplication,
@BindsInstance emailId: String) : AppComponent
}
}
Run Code Online (Sandbox Code Playgroud)