hurshi/dagger-vm-injector


Download


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"]]
        
        

Readme


Dagger扩展组件

已废弃,查看新版 - SimplifyDagger

extension组件

概述

dagger.android提供了注入的便捷方式,但是只能注入到Activity,Fragment等组件中,对于MVP或者MVVM框架来说,更加希望能注入到Presenter或者ViewModle中。extension组件扩展了dagger.android中的support组件,能注入到自定义类中。(因为本人是在MVVM中使用,此组件为MVVM设计)

使用
  1. 添加jitpack仓库

    	maven { url 'https://jitpack.io'
    
  2. 添加依赖

    	implementation "com.github.hurshi.dagger-vm-injector:extension:last_version"
    
  3. 修改application

    	public class YourApplication extends Application implements HasDaggerVMInjector {
    
    		@Inject
    	DispatchingAndroidInjector<DaggerVM> daggerVMDispatchingAndroidInjector;
    
    	@Override
    	public AndroidInjector<DaggerVM> daggerVMInjector() {
       	     return daggerVMDispatchingAndroidInjector;
    	}
    	}
    
  4. component中将AndroidSupportInjectionModule或者AndroidInjectionModule替换成AndroidDaggerVMInjectionModule

    	@Singleton
    @Component(modules = {AndroidDaggerVMInjectionModule.class})
    public interface AppComponent {
    	     void inject(App app);
    }
    
    
  5. 如果你用到@ActivityKey/@FragmentKey,都替换成@DaggerVMKey
  6. 注入方式:(被注入的类需要implements DaggerVM)

    	class Test implements DaggerVM{
    	public Test(Application application){
        	AndroidDaggerVMInjection.inject(application, this);
    		}
    	}
    

自动绑定组件

概述

上述编写module,subcomponent代码稍显复杂,这里提供自动生成这些代码的功能,实现ViewModel和module直接绑定

  • 存在问题:因为生成的代码是需要dagger去解析的,但是注解不能指定解析顺序(必须先解析我的注解,再解析dagger的注解才行),导致如果本注解比dagger的注解后执行的话,就不能生成对应的dagger代码。
  • 解决方案 把生成的代码打成jar包,提供给dagger解析。
配置
  1. 添加依赖

    	implementation "com.github.hurshi.dagger-vm-injector:extension:last_version"
    
  2. modulebuild.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"
    	}
    
    
  3. 项目根目录添加shells文件夹,在shells中添加脚本inject.sh

    	../gradlew clean -p ../
    	../gradlew assembleDebug -p ../
    	../gradlew daggerModuleSourceJar -p ../
    	../gradlew toCopyJars -p ../
    	../gradlew clean -p ../
    	../gradlew assembleDebug -p ../
    
使用
  1. viewmodel绑定module

    	@BindVMModule(module = MainModule.class, scope = ActivityScope.class)
    	class Test implements DaggerVM{
    		public MainViewModel(Application application){
       		AndroidDaggerVMInjection.inject(application, this);
    	}
    	}
    
  2. cd shells -> sh inject.sh
  3. 确保component@Component -> modules 中有 _GPDaggerVMInjectModule.class

    	@Singleton
    @Component(modules = {AndroidDaggerVMInjectionModule.class,_GPDaggerVMInjectModule.class})
    public interface AppComponent {
    	     void inject(App app);
    }
    

4.ReBuild后就好了