Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.hurshi:ProduceApi:1.1.1'
}
dependencies {
implementation("com.github.hurshi:ProduceApi:1.1.1")
}
<dependency>
<groupId>com.github.hurshi</groupId>
<artifactId>ProduceApi</artifactId>
<version>1.1.1</version>
</dependency>
libraryDependencies += "com.github.hurshi" % "ProduceApi" % "1.1.1"
:dependencies [[com.github.hurshi/ProduceApi "1.1.1"]]
dagger.android
提供了注入的便捷方式,但是只能注入到Activity
,Fragment
等组件中,对于MVP
或者MVVM
框架来说,更加希望能注入到Presenter
或者ViewModle
中。extension
组件扩展了dagger.android
中的support
组件,能注入到自定义类中。(因为本人是在MVVM
中使用,此组件为MVVM
设计)
添加jitpack仓库
maven { url 'https://jitpack.io'
添加依赖
implementation "com.github.hurshi.dagger-vm-injector:extension:last_version"
修改application
public class YourApplication extends Application implements HasDaggerVMInjector {
@Inject
DispatchingAndroidInjector<DaggerVM> daggerVMDispatchingAndroidInjector;
@Override
public AndroidInjector<DaggerVM> daggerVMInjector() {
return daggerVMDispatchingAndroidInjector;
}
}
在component
中将AndroidSupportInjectionModule
或者AndroidInjectionModule
替换成AndroidDaggerVMInjectionModule
@Singleton
@Component(modules = {AndroidDaggerVMInjectionModule.class})
public interface AppComponent {
void inject(App app);
}
@ActivityKey
/@FragmentKey
,都替换成@DaggerVMKey
注入方式:(被注入的类需要implements DaggerVM
)
class Test implements DaggerVM{
public Test(Application application){
AndroidDaggerVMInjection.inject(application, this);
}
}
上述编写module,subcomponent代码稍显复杂,这里提供自动生成这些代码的功能,实现ViewModel和module直接绑定
添加依赖
implementation "com.github.hurshi.dagger-vm-injector:extension:last_version"
在module
的build.gradle
中添加
task daggerModuleSourceJar(type: Jar) {
from fileTree(dir: "${project.projectDir.absolutePath}/build/intermediates/classes/debug", include: '**/_GPDaggerVMInjectModule.class')
from fileTree(dir: "${project.projectDir.absolutePath}/build/intermediates/classes/debug", include: '**/*_GpSubcomponent.class')
from fileTree(dir: "${project.projectDir.absolutePath}/build/intermediates/classes/debug", include: '**/*_GpSubcomponent$Builder.class')
}
task toCopyJars(type: Copy) {
from 'build/libs'
into 'libs_compileonly'
rename { String fileName ->
fileName.replace(".jar", "DaggerInjections.jar")
}
}
dependencies {
compileOnly fileTree(dir: 'libs_compileonly', include: ['*.jar'])
implementation "com.github.hurshi.dagger-vm-injector:annotation:last_version"
//kapt "com.github.hurshi.dagger-vm-injector:compiler:last_version"
annotationProcessor "com.github.hurshi.dagger-vm-injector:compiler:last_version"
}
项目根目录添加shells
文件夹,在shells中添加脚本inject.sh
../gradlew clean -p ../
../gradlew assembleDebug -p ../
../gradlew daggerModuleSourceJar -p ../
../gradlew toCopyJars -p ../
../gradlew clean -p ../
../gradlew assembleDebug -p ../
给viewmodel
绑定module
@BindVMModule(module = MainModule.class, scope = ActivityScope.class)
class Test implements DaggerVM{
public MainViewModel(Application application){
AndroidDaggerVMInjection.inject(application, this);
}
}
cd shells -> sh inject.sh
确保component
中@Component -> modules
中有 _GPDaggerVMInjectModule.class
@Singleton
@Component(modules = {AndroidDaggerVMInjectionModule.class,_GPDaggerVMInjectModule.class})
public interface AppComponent {
void inject(App app);
}
4.ReBuild
后就好了