Code Monkey home page Code Monkey logo

dagger-vm-injector's Introduction

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后就好了

dagger-vm-injector's People

Contributors

hurshi1 avatar hurshi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.