Android studio项目添加Androidannotations注解框架

给项目添加annotation框架

为避免自己以后忘记,写到博客中记录一下自己的安卓历程~

一、app中的build.gradle

添加如下代码

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '3.3.2'  //版本

dependencies {
    /**
    原本存在的一些jar包,只需在后面添加下面两行代码即可
     */
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}
apt {
    arguments {
        androidManifestFile variant.outputs[0]?.processResources?.manifestFile
        // if you have multiple outputs (when using splits), you may want to have other index than 0

        // you should set your package name here if you are using different application IDs
        // resourcePackageName "your.package.name"

        // You can set optional annotation processing options here, like these commented options:
        // logLevel 'INFO'
        // logFile '/var/log/aa.log'
    }
}

二、Project层的build.gradle

    dependencies {
        // replace with the current version of the Android plugin
        classpath 'com.android.tools.build:gradle:1.3.0'
        // replace with the current version of the android-apt plugin
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }

注意:编译gradle的时候有可能会出现Error:Cause: peer not authenticated错误,有多种情况,我在编写中遇到的是gradle版本过高,将1.5.0改成1.3.0即可。详情参照:Android studio 导入项目报 Error:Cause: peer not authenticated 异常

三、同步gradle

这里写图片描述

四、使用

1.代码中使用的对象名,点击事件方法名要和XML文件中定义的id一致
2.AndroidManifest文件中的XXActivity要改成XXActivity_
例如

@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
    
    
    @ViewById
    TextView main_text_tv;
    @ViewById
    Button main_button_btn;

    @AfterViews
    public void init() {
        main_text_tv.setText("文字");
        main_button_btn.setText("按钮");
    }

    @Click
    void main_text_tv() {
        main_text_tv.setText(main_text_tv.getText().toString().equals("文字") ? "点击状态" : "文字");
    }

    @Click
    void main_button_btn() {
        main_button_btn.setText(main_button_btn.getText().toString().equals("按钮") ? "点击状态" : "按钮");
    }
}

猜你喜欢

转载自blog.csdn.net/lizebin_bin/article/details/50433336
今日推荐