在开发过程中有很多重复的代码没有必要每次详细的编写,下面以一键生成MVP相关代码以及对应的Fragment或者Activity
首先去intellij idea官网下在开发环境,可以免费使用三十天;
1、创建插件项目,File->New ->Project 打开窗口后选择Intellij Platform Plugin,选择对应的Project SDK版本,点击Next选择位置,示意图如下:
2、在src中创建Action帮助,New->Plugin Devkit -> Action,输入相关的信息,选择Action的分组、位置以及快捷键等想信息,选择group分组最好根据你具体的插件的功能决定,比如生成代码的功能可以在CodeMenu中也可以在NewGroup更方便,创建完成后可以在resources->plugin.xml中看到注册的Action信息,可以修改发布插件的其他相关信息;
<idea-plugin>
<id>com.hpaopao.mvp</id>
<name>MvpGenerater</name>
<version>1.0</version>
<vendor email="bitxiaozhang@163.com" url="http://www.×××××.com">杭州×××××有限公司</vendor>
<description><![CDATA[
Generate Code File for MVP,Acticity,Fragment,Contract,Presenter,Model<br>
<em>most HTML tags may be used</em>
]]></description>
<change-notes><![CDATA[
create the mvp contruct,set the BaseActivity and BaseFragment
<em>most HTML tags may be used</em>
]]>
</change-notes>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="145.0"/>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<!-- uncomment to enable plugin in all products
<depends>com.intellij.modules.lang</depends>
-->
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
</extensions>
<actions>
<!-- Add your actions here -->
<action id="Android.Activity.MvpHelpers" class="com.MvpGeneratorAction" text="ActivityMvp"
description="generate mvp relatived code files:Activity ,Fragment,Contract,Model,Presenter">
<add-to-group group-id="NewGroup" anchor="first"/>
<keyboard-shortcut keymap="$default" first-keystroke="shift ctrl alt G"/>
</action>
<action id="Android.Fragment.MvpHelpers" class="com.MvpGeneratorAction" text="FragmentMvp"
description="generate mvp relatived code files:Activity ,Fragment,Contract,Model,Presenter">
<add-to-group group-id="NewGroup" anchor="before" relative-to-action="Android.Activity.MvpHelpers"/>
</action>
</actions>
</idea-plugin>
3、在创建的Action类中,覆盖actionPerformed方法,加入相关的业务逻辑代码,举例:
@Override
public void actionPerformed(AnActionEvent e) {
// TODO: insert action logic here```
mProject = e.getData(PlatformDataKeys.PROJECT);
mPackageName = getPackageName();
DataContext dataContext = e.getDataContext();
VirtualFile file = DataKeys.VIRTUAL_FILE.getData(dataContext);
if (file != null && file.exists()){
mMenuRelativePath = file.getPath();
handleMenuPackagePath();
}else{
throw new RuntimeException("未获取到文件夹路径!");
}
}
4、创建生成代码的模板文件,我这里直接写到txt文件中创建时直接替换掉相关的信息,比如创建Activity的模板文件的内容:
package $packagename.activity;
import android.os.Bundle;
import $basemvppackage.mvp.BaseActivity;
import $packagename.mvp.$nameContract;
import $packagename.mvp.$nameModel;
import $packagename.mvp.$namePresenter;
import $basepackagename.R;
/**
* @author $author
* @Package $packagename
* @Description: $description
* @date $date
**/
public class $nameActivity extends BaseActivity<$namePresenter,$nameModel> implements $nameContract.View{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected int getLayoutId() {
return 0;
}
@Override
protected void initPresenter() {
mPresenter.setVM(this,mModel);
}
@Override
protected void initView(){
}
@Override
public void showLoading(String content) {
}
@Override
public void stopLoading() {
}
@Override
public void showErrorTip(String msg) {
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
5、创建Dialog输入相关的信息,替换模板中的相关字段,比如authorName和ModuleName,运行效果如下,项目地址