Android studio project adds Androidannotations annotation framework

Add annotation framework to the project

To avoid forgetting later, write a blog and record your Android journey~

One, build.gradle in app

Add the following code

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'
    }
}

Two, the build.gradle of the Project layer

    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'
    }

Note: Error: Cause: peer not authenticated may appear when compiling gradle. There are many situations. What I encountered in writing is that the gradle version is too high. Just change 1.5.0 to 1.3.0. For details, refer to: Android studio import project reports Error: Cause: peer not authenticated exception

Three, sync gradle

Write picture description here

Four, use

1. The object name used in the code, the click event method name should be consistent with the id defined in the XML file 2. The
XXActivity in the AndroidManifest file should be changed to XXActivity_
for example

@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("按钮") ? "点击状态" : "按钮");
    }
}

Guess you like

Origin blog.csdn.net/lizebin_bin/article/details/50433336