Dagger Android 用于自定义 class 可能吗?
Dagger Android for custom class possible?
我正在尝试让 dagger-android 与 Conductor(或任何自定义 class)一起工作。我尝试复制 AndroidSupportInjectionModule(和朋友)所做的一切,在我看来,这与自定义 class 处理相同。
不过我得到
C:\Users\ursus\AndroidStudioProjects\...\ControllersModule.java:15: error: com.foo.bar.ChannelsController is not a framework type
public abstract com.foo.bar.ChannelsController channelsController();
所以,我的"library"代码
package com.foo.bar
import com.bluelinelabs.conductor.Controller;
import dagger.Module;
import dagger.android.AndroidInjectionModule;
import dagger.android.AndroidInjector;
import dagger.internal.Beta;
import dagger.multibindings.Multibinds;
import java.util.Map;
@Beta
@Module(includes = AndroidInjectionModule.class)
public abstract class ConductorInjectionModule {
private ConductorInjectionModule() {
}
@Multibinds
abstract Map<Class<? extends Controller>, AndroidInjector.Factory<? extends Controller>> controllerInjectorFactories();
@Multibinds
abstract Map<String, AndroidInjector.Factory<? extends Controller>> controllerInjectorFactoriesWithStringKeys();
}
我什至没有编译,所以假设粘贴 ConductorInjection 和 HasControllerInjector 毫无意义
用法:
@Module
abstract class AppModule {
@ContributesAndroidInjector abstract fun mainActivity(): MainActivity
@ContributesAndroidInjector abstract fun channelsController(): ChannelsController
}
class App : Application(), HasActivityInjector, HasControllerInjector {
@Inject lateinit var activityInjector: DispatchingAndroidInjector<Activity>
@Inject lateinit var controllerInjector: DispatchingAndroidInjector<Controller>
private lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.builder()
.applicationContext(this)
.build()
.apply {
inject(this@App)
}
}
override fun activityInjector() = activityInjector
override fun controllerInjector() = controllerInjector
}
@Singleton
@Component(
modules = [
AndroidInjectionModule::class,
ConductorInjectionModule::class,
AppModule::class,
NetModule::class
]
)
interface AppComponent {
fun inject(app: App)
@Component.Builder
interface Builder {
@BindsInstance
fun applicationContext(context: Context): Builder
fun build(): AppComponent
}
}
implementation deps.dagger.runtime
implementation deps.dagger.androidRuntime
kapt deps.dagger.compiler
kapt deps.dagger.androidCompiler
这里都是“2.19”版本(试过2.16)
AGP"com.android.tools.build:gradle:3.3.0-rc02"(已尝试3.2.1稳定)
有线索吗?在我看来,这一切都应该像 dagger-android-support
所做的那样工作
对于未来的旅行者,他们在注释中硬编码了一些 appcompat 内容。处理器,这就是 appcompat 片段起作用的原因。
更新到 dagger 2.20,它会神奇地工作
error: com.foo.bar.ChannelsController is not a framework type
所以要回答的问题是,"how does dagger-android know what a framework type is or not"。
答案可以在 2.19 和 2.20 之间的 this commit to Dagger-Android 中找到,他们 "removed the old way of doing things for better compatibility with AndroidX".
正如我们在 中看到的那样,
/**
* Returns the Android framework types available to the compiler, keyed by their associated {@code
* dagger.android} {@link MapKey}s. This will always contain the types that are defined by the
* framework, and only contain the support library types if they are on the classpath of the
* current compilation.
*/
static ImmutableMap<Class<? extends Annotation>, TypeMirror> frameworkTypesByMapKey(
Elements elements) {
return ImmutableMap.copyOf(
Stream.of(
elements.getPackageElement("dagger.android"),
elements.getPackageElement("dagger.android.support"))
.filter(packageElement -> packageElement != null)
.flatMap(packageElement -> typesIn(packageElement.getEnclosedElements()).stream())
.filter(AndroidMapKeys::isNotAndroidInjectionKey)
.filter(type -> isAnnotationPresent(type, MapKey.class))
.filter(mapKey -> mapKey.getAnnotation(MapKey.class).unwrapValue())
.flatMap(AndroidMapKeys::classForAnnotationElement)
.collect(toMap(key -> key, key -> mapKeyValue(key, elements))));
}
他们有代码检查 dagger.android
和 dagger.android.support
包中自己的 @MapKey
类型,看起来像这样:
// java/dagger/android/support/FragmentKey.java
@Beta
@MapKey
@Documented
@Target(METHOD)
@Deprecated
public @interface FragmentKey {
Class<? extends Fragment> value();
}
所以他们根据 dagger.android
和 dagger.android.support
包中可用的 @MapKey
来阅读框架类型。
显然他们在 2.20 中删除了这个检查,所以现在你可以注入任何你想要的东西。高兴!
但除此之外,您实际上可以通过在项目的 dagger.android
包中添加 @ControllerKey
和 @ViewKey
的方式对其进行破解,实际上可能与 2.19 一起使用。
在 "is not a framework type" 中检查错误的测试也在该提交中删除。
啊,还有
@Multibinds
abstract Map<String, AndroidInjector.Factory<? extends Controller>> controllerInjectorFactoriesWithStringKeys();
你也可以用 2.20 删除这部分,你现在只需要 AndroidInjectionModule
。
我正在尝试让 dagger-android 与 Conductor(或任何自定义 class)一起工作。我尝试复制 AndroidSupportInjectionModule(和朋友)所做的一切,在我看来,这与自定义 class 处理相同。
不过我得到
C:\Users\ursus\AndroidStudioProjects\...\ControllersModule.java:15: error: com.foo.bar.ChannelsController is not a framework type
public abstract com.foo.bar.ChannelsController channelsController();
所以,我的"library"代码
package com.foo.bar
import com.bluelinelabs.conductor.Controller;
import dagger.Module;
import dagger.android.AndroidInjectionModule;
import dagger.android.AndroidInjector;
import dagger.internal.Beta;
import dagger.multibindings.Multibinds;
import java.util.Map;
@Beta
@Module(includes = AndroidInjectionModule.class)
public abstract class ConductorInjectionModule {
private ConductorInjectionModule() {
}
@Multibinds
abstract Map<Class<? extends Controller>, AndroidInjector.Factory<? extends Controller>> controllerInjectorFactories();
@Multibinds
abstract Map<String, AndroidInjector.Factory<? extends Controller>> controllerInjectorFactoriesWithStringKeys();
}
我什至没有编译,所以假设粘贴 ConductorInjection 和 HasControllerInjector 毫无意义
用法:
@Module
abstract class AppModule {
@ContributesAndroidInjector abstract fun mainActivity(): MainActivity
@ContributesAndroidInjector abstract fun channelsController(): ChannelsController
}
class App : Application(), HasActivityInjector, HasControllerInjector {
@Inject lateinit var activityInjector: DispatchingAndroidInjector<Activity>
@Inject lateinit var controllerInjector: DispatchingAndroidInjector<Controller>
private lateinit var appComponent: AppComponent
override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.builder()
.applicationContext(this)
.build()
.apply {
inject(this@App)
}
}
override fun activityInjector() = activityInjector
override fun controllerInjector() = controllerInjector
}
@Singleton
@Component(
modules = [
AndroidInjectionModule::class,
ConductorInjectionModule::class,
AppModule::class,
NetModule::class
]
)
interface AppComponent {
fun inject(app: App)
@Component.Builder
interface Builder {
@BindsInstance
fun applicationContext(context: Context): Builder
fun build(): AppComponent
}
}
implementation deps.dagger.runtime
implementation deps.dagger.androidRuntime
kapt deps.dagger.compiler
kapt deps.dagger.androidCompiler
这里都是“2.19”版本(试过2.16)
AGP"com.android.tools.build:gradle:3.3.0-rc02"(已尝试3.2.1稳定)
有线索吗?在我看来,这一切都应该像 dagger-android-support
所做的那样工作
对于未来的旅行者,他们在注释中硬编码了一些 appcompat 内容。处理器,这就是 appcompat 片段起作用的原因。
更新到 dagger 2.20,它会神奇地工作
error: com.foo.bar.ChannelsController is not a framework type
所以要回答的问题是,"how does dagger-android know what a framework type is or not"。
答案可以在 2.19 和 2.20 之间的 this commit to Dagger-Android 中找到,他们 "removed the old way of doing things for better compatibility with AndroidX".
正如我们在
/** * Returns the Android framework types available to the compiler, keyed by their associated {@code * dagger.android} {@link MapKey}s. This will always contain the types that are defined by the * framework, and only contain the support library types if they are on the classpath of the * current compilation. */ static ImmutableMap<Class<? extends Annotation>, TypeMirror> frameworkTypesByMapKey( Elements elements) { return ImmutableMap.copyOf( Stream.of( elements.getPackageElement("dagger.android"), elements.getPackageElement("dagger.android.support")) .filter(packageElement -> packageElement != null) .flatMap(packageElement -> typesIn(packageElement.getEnclosedElements()).stream()) .filter(AndroidMapKeys::isNotAndroidInjectionKey) .filter(type -> isAnnotationPresent(type, MapKey.class)) .filter(mapKey -> mapKey.getAnnotation(MapKey.class).unwrapValue()) .flatMap(AndroidMapKeys::classForAnnotationElement) .collect(toMap(key -> key, key -> mapKeyValue(key, elements)))); }
他们有代码检查 dagger.android
和 dagger.android.support
包中自己的 @MapKey
类型,看起来像这样:
// java/dagger/android/support/FragmentKey.java
@Beta
@MapKey
@Documented
@Target(METHOD)
@Deprecated
public @interface FragmentKey {
Class<? extends Fragment> value();
}
所以他们根据 dagger.android
和 dagger.android.support
包中可用的 @MapKey
来阅读框架类型。
显然他们在 2.20 中删除了这个检查,所以现在你可以注入任何你想要的东西。高兴!
但除此之外,您实际上可以通过在项目的 dagger.android
包中添加 @ControllerKey
和 @ViewKey
的方式对其进行破解,实际上可能与 2.19 一起使用。
在 "is not a framework type" 中检查错误的测试也在该提交中删除。
啊,还有
@Multibinds
abstract Map<String, AndroidInjector.Factory<? extends Controller>> controllerInjectorFactoriesWithStringKeys();
你也可以用 2.20 删除这部分,你现在只需要 AndroidInjectionModule
。