Android is changing! ButterKnife is deprecated: Resource IDs will be non-final in Android Gradle Plugin version 5.0

Codewords work hard! Please indicate the source!

0. Preface

Recently, Android Studio has been upgraded to version 4.1. When using ButterKnife, the BindView annotation gives this prompt: Resource IDs will be non-final in Android Gradle Plugin version 5.0, WTF? Is the old Google who changed the API once a month to pit developers again? ? ?

Yes, in the future, all R.id.* will become variables, although I don’t know why.

The author of ButterKnife also jumped out and declared that the development has come to an end and will no longer be updated and deprecated. It is recommended that we use the View Binding officially launched by Google :

Interestingly, before I noticed this change, I pushed a version with this warning, and this version did not experience any abnormal crashes.

In other words, if the project is tight, there is no problem keeping it still for the time being, but we still need to gradually migrate projects that use ButterKnife to ViewBinding , or simply switch to our son kotlin (which will automatically bind the layout)

1. Use View Binding

First, we need to activate this tool. In the app’s build.gradle, add the following:

android {
        ...
        viewBinding {
            enabled = true
        }
    }
    

2. Use in Activity

Suppose we have an activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>
</RelativeLayout>

AndroidStudio will detect all XML files, use the camel case method to name +Binding suffix, and create a binding class: for example, activity_main will generate an ActivityMainBinding class.

We use it like this:

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //注意!!!在setContentView之前!!!
        ActivityMainBinding inflate = ActivityMainBinding.inflate(getLayoutInflater());

        //注意!!!这里是inflate.getRoot(),不是R.layout.activity_main
        setContentView(inflate.getRoot());

        //这里的tv就是XML中的id为tv的TextView
        inflate.tv.setText("Fxxk gooooooogie !!!");
    }

It's quite simple to use, but it still can't cover up Google's confusing behavior. ButterKnife's annotation programming code is clearer, isn't it~

3. Use in Fragment

Suppose we have a fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>

We use it like this:

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        FragementMainBinding inflate = FragementMainBinding.inflate(inflater, container, false);
        inflate.tv.setText("Holy G00gie");
        return inflate.getRoot();
    }

Finally, the old rules. Although CSDN has subscription payments, I still prefer voluntary rewards (although there is basically no income QvQ)

 

Guess you like

Origin blog.csdn.net/u014653815/article/details/109216216