Skip to content

Latest commit

 

History

History
44 lines (41 loc) · 1.71 KB

README.md

File metadata and controls

44 lines (41 loc) · 1.71 KB

annotation-processor-playground

This annotation processor provides code generation for the initialization of an android.support.v7.app.AppCompatActivity in a MVVM project. To achieve that, it uses android.databinding.ViewDataBinding.

Annotations:

@AnnotatedActivity:

Contains the layoutId, this is the main entry point for the annotation processor.

@DataBinding:

Attribute of the DataBinding, the class is generated by Google Data Binding when having the binding defined in the layout xml file.

@ViewModel:

ViewModels that are defined in the DataBinding.

Example:

The annotation processor can generate code for classes in the style of:

@AnnotatedActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
    @ViewModel("setMainViewModel")
    MainViewModel mainViewModel;

    @DataBinding
    ActivityMainBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainActivityUtil.bind(this);
        setSupportActionBar(binding.toolbar);
    }
}

This class generates the following util class:

class MainActivityUtil {
    static void bind(final MainActivity bindee) {
        final @LayoutRes int layout = 2130968601;
        bindee.setContentView(layout);
        bindee.binding = DataBindingUtil.setContentView(bindee, layout);

        bindee.mainViewModel = new com.github.mosberger.helloannotationprocessor.viewmodel.MainViewModel();
        bindee.binding.setMainViewModel(bindee.mainViewModel);
    }
}

The ViewModels must be defined in the DataBinding and a class can contain 0 to n ViewModels. The annotation processor is capable of using only one DataBinding per Activity.