DEV Community

Denize Ignacio for Dolby.io

Posted on

How to Migrate from Butterknife to View Binding in Android

As the Android Development scene continues to grow, new build tools are created in order to address issues developers had seen using previous solutions. A commonly used tool was Butterknife. At the time, Butterknife was used to reduce the the amount of times the findViewById(...) function had been used in order to reference a view in your application. However, Butterknife still had its own issues such as null safety and speed. Now introduce View Binding: a low code, null safe, and fast binding tool! View Binding is a 1st party tool built by the Google Android Development team which helps reference and easily manage views within an app. In this post, you'll learn why you should switch and how to add View Binding to your own app.

Why switch from Butterknife to View Binding?

Butterknife is a 3rd party library made to address the issue of using findViewById(...) functions to reference and interact with views. It helped reduce boilerplate code but also had to set up a @Bind annotation every time you wanted to interact with a view. Then View Binding was introduced starting at Gradle version 3.6. A build feature which led to the deprecation of Butterknife and a decrease in the use of findViewById(...) functions.

Previously, we'd use Butterknife to handle referencing views and handling event listeners.

Java

@Bind(R.id.etExample)
EditText etExample;
Enter fullscreen mode Exit fullscreen mode

Kotlin

@BindView(R.id.etExample)
lateinit var etExample: EditText
Enter fullscreen mode Exit fullscreen mode

Can now be referenced as:
Java

private ActivityMainBinding binding;
Enter fullscreen mode Exit fullscreen mode

Kotlin

lateinit var binding: ActivityMainBinding
Enter fullscreen mode Exit fullscreen mode

This single binding variable allows us to access every view, set up event listeners, and any other functionality we'd do with Butterknife.

On top of Butterknife being deprecated, another reason to switch is because View Binding is compile time safe and builds fast. As for findViewById(...), this way of referencing views will lead to so much unnecessary code that could be replaced with a View Binding variable.

How to Update from Butterknife to View Binding

Note that in this blog post, we'll be updating the Android Getting Started Guide in the Dolby.io Communications SDK tutorial. You can find the repo here.

Below we'll list the steps needed in order to replace Butterknife with View Binding.

Enable View Binding in the build.gradle(app) file

The first change was to remove all Butterknife dependencies in our Android project.

In order for us to use view binding, we had to enable it within the build.gradle(app) file.

android {
    ...
    buildFeatures {
        viewBinding = true
    }
}
Enter fullscreen mode Exit fullscreen mode

Once viewBinding has been enabled, let's sync our project!

Removing Butterknife in our Code

We'll have to remove all views that were previously using Butterknife. In this example, we edited the MainActivity.java file in the Getting Started Guide repo.

Below you'll see how we previously initialized all the views in the class.

public class MainActivity extends AppCompatActivity {
    ...
    @Bind(R.id.user_name)
    EditText user_name;

    @Bind(R.id.conference_name)
    EditText conference_name;

    @Bind(R.id.video)
    protected VideoView video;

    @Bind(R.id.videoOther)
    protected VideoView videoOther;

    @Bind(R.id.participants)
    EditText participants;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        ...
    }

    @OnClick(R.id.login)
    public void onLogin() { ... }
}
Enter fullscreen mode Exit fullscreen mode

Now with View Binding it's as easy as using one binding variable to reference all the views.

Java

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

        binding.login.setOnClickListener(new View.OnClickListener() { ... }
}
Enter fullscreen mode Exit fullscreen mode

Kotlin

class MainActivity : AppCompatActivity() {

    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)

        binding.login.setOnClickListener{...}
    }
}
Enter fullscreen mode Exit fullscreen mode

When using Butterknife, we had initialize the views and reference them. Now we can directly access them thanks to View Binding!

With View Binding, if we ever needed to reference a Button, EditText, or any other view, all we'd have to do is retrieve it through the binding variable we created earlier.

And for any other behaviors such as setting text, handling onClick events, and so on, you can access them through the binding variable you initialize.

Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    binding.conferenceName.setText("Avengers meeting");
}
Enter fullscreen mode Exit fullscreen mode

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    binding.conferenceName.text = "Avengers meeting"
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

If your team is still using ButterKnife then now is the time to switch to View Binding. It'll provide all the same functionality, less code, and need for boilerplate referencing.

We decided to update our Android Getting Started Guide Sample App because ButterKnife has been deprecated. As new developers onboard to the Android Communications SDK, it's important for our sample apps to be using the latest Android practices that others would use in their own projects.

If you'd like to learn more about View Binding, visit the official docs here!

Thanks for reading!

Discussion (0)