关于kotlin插件 MVP Generator的使用方式
来源:互联网 发布:淘宝怎么发布买家秀 编辑:程序博客网 时间:2024/06/05 15:04
无意间在android studio中看到了 MVP Generator 插件,插件的github地址为:https://github.com/stfalcon-studio/MVPHelper;插件的作者没有说明这个插件的具体使用方式,导致做为新手折腾了好一段时间啊,现在记录一下这个插件的使用方式,该插件主要是快速创建mvp框架,其中使用了MVP + Dagger + Rx , 是编写Kotlin代码时使用;
MVP Generator的使用注意事项:
1.首先需要在android studio 中添加插件 Mvp Generator:
File -->Setting-->Plugins -->Browse repositories...,搜索Mvp Generator ,install ---> restart As;
2.在项目app module中java包下创建相应的模块文件夹名称,右键new–>+mvp–>选择创建Activity或者fragment–>输入要创建的Activity或者fragment名称,例如:login, 同时需要点击kotlin base DI classes,生成di文件夹及代码;
3.此时会自动生成如图所示的类和文件夹
4.此时生成的就是mvp的架子,但是你点开,发现全报错,why? 我就这完全不知所措了。。。。。
5.淡定,其实如果了解Dagger2的朋友,肯定知道,那就是我们需要导入一些依赖:
ext这个和dependencies同个级别,其实就是来控制依赖的版本,完全可以不用ext { support_libraries_version = '25.3.1' dagger_version = '2.10'}在项目 module的bulid.gradle的dependencies {......}中添加如下: //Dagger compile 'com.github.stfalcon:mvphelper:0.2.1' compile "com.google.dagger:dagger:$dagger_version" compile "com.google.dagger:dagger-android:$dagger_version" compile "com.google.dagger:dagger-android-support:$dagger_version" annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version" annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version" kapt "com.google.dagger:dagger-compiler:$dagger_version" //Rx compile 'io.reactivex.rxjava2:rxjava:2.0.8' compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' //新增kotlin kapt脚本/***注掉了kapt代码,因为kapt3不支持*/Error:Execution failed for task ':app:compileDebugJavaWithJavac'.> kotlin.jvm.internal.FunctionReference.<init>(ILjava/lang/Object;)V//kapt {// generateStubs = true//}kapt主要是为了生成DaggerAppComponent类,该类在app\build\generated\source\kapt\debug\com\example\szw\myapplication\di\components\DaggerAppComponent.java
添加如图所示:
6. 可能还需要在项目的build.gradle中添加依赖:
classpath 'com.novoda:bintray-release:0.3.4'
7.到这里差不多就算配置的差不多了,然后开始移动代码
- 在factories文件夹中可以看到两个类:ActivitiesInjectorFactories和FragmentsInjectorFactories,
如果你是创建的activity,那么会在xxxxxActivitySubComponent中生成
@Binds @IntoMap @ActivityKey(xxxxxActivity::class) internal abstract fun bindxxxxxActivityInjectorFactory( builder: xxxxxActivitySubComponent.Builder): AndroidInjector.Factory<out Activity>
如果是fragment,在xxxxFragmentSubComponent中有如下代码:
@Binds @IntoMap @FragmentKey(xxxxFragment::class) internal abstract fun bindxxxxFragmentInjectorFactory(builder: xxxxFragmentSubComponent.Builder): AndroidInjector.Factory<out Fragment>
把这些代码,如果是Activity中的就剪贴到ActivitiesInjectorFactories中;
Fragment中的就剪贴到FragmentsInjectorFactories中。
2.如果是创建的fargment,同时需要在FragmentsInjectorFactories中的这段代码里添加:
@Module(subcomponents = arrayOf( // register your fragments' subcomponents here xxxxFragmentSubComponent::class //这里添加生成的xxxxFragmentSubComponent))
- 如果是创建的activity,同时需要在module文件夹下的AppModule类的下面代码中添加:
@Module(subcomponents = arrayOf( // register your activities' subcomponents here xxxxxActivitySubComponent::class //这里添加生成的xxxxxActivitySubComponent))
4.最后一步,就是把App这个配置到AndroidManifest.xml中去,如下:
<application android:name=".App" //就是这里 android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".login.LoginActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
5.也是最重要的一步,就是需要重新编译,看能不能生成DaggerAppComponent类,如果没有,看下什么地方不对,在检查检查,就ok了。。
新手自己使用怕忘了,就记录一下
阅读全文
0 0
- 关于kotlin插件 MVP Generator的使用方式
- mybatis generator插件的使用 。
- mybatis generator插件的使用
- 关于kotlin的简单使用
- KotLin的简单使用 KotLin入门基础 AndroidStudio中配置KotLin插件 新建KotLin工程
- Eclipse插件:mybatis generator的使用步骤
- mybatis插件 mybatis-generator的使用
- Maven插件mybatis-generator的使用
- Eclipse插件:mybatis generator的使用步骤
- eclipse插件:myBatis generator的使用
- Android Studio Kotlin插件的简单使用
- Mybatis Generator插件的手动安装及插件的使用
- 使用Kotlin的方式添加Fragment
- MVP + Kotlin
- 在kotlin-MVP使用dagger2(v2.11+)
- 使用Kotlin语言编写Android MVP案例
- 关于lobibox Jquery+bootstrap的弹出框插件使用方式
- 关于MVP模式的使用设计
- 家娃xiancheng-duizhan分析
- iOS 项目架构与代码规范
- qtabwidget表内容设置
- Java入门基础之重点补充
- Unity NGUI HUD text简单使用
- 关于kotlin插件 MVP Generator的使用方式
- 【CUGBACM15级BC第10场 A】hdu 5018 Revenge of Fibonacci
- 区块链在版权保护方面的探索与实践
- C++ 归并排序的实现实例代码
- 区块链学习
- Java多线程系列(二)-----实现方式
- 文章标题
- nyoj 27 水池数目
- Python列表排序函数