androiddagger-2butterknifebuck

How do I set up Buck with Dagger & Butterknife?


Hello my fellow developers,

I'm trying to switch from Gradle to Buck and have problems setting it up with Dagger and Butterknife since they need annotation processing.

I found a few links and tutorials, but they are already a few (~4) years old and I thought there might be a little bit newer approach on doing things.

This is what I looked at already: https://github.com/ryan-endacott/android-buck-dagger-starter

Thanks in advance, Patrick


Solution

  • Buck supports annotation processing for Java-based rules (including android_library). Take a look at how Immutables are used in buck repository (link to source code):

    1. First of all, you need to declare library jars:
        java_library(
            name = "immutables",
            exported_deps = [
                ":builder",
                ":value",
            ],
            visibility = [
                "PUBLIC",
            ],
        )
    
        prebuilt_jar(
            name = "value",
            binary_jar = "value-2.5.6.jar",
            source_jar = "value-2.5.6-sources.jar",
            licenses = [
                "COPYING",
            ],
        )
    
        prebuilt_jar(
            name = "builder",
            binary_jar = "builder-2.5.6.jar",
            source_jar = "builder-2.5.6-sources.jar",
            licenses = [
                "COPYING",
            ],
        )
    
    1. Define annotation processor:
        java_annotation_processor(
            name = "processor",
            isolate_class_loader = True,
            processor_class = "org.immutables.value.internal.$processor$.$Processor",
            visibility = [
                "PUBLIC",
            ],
            deps = [
                ":immutables",
            ],
        )
    
    1. You can then add this annotation processor to the list of plugins of the targets:
        java_library(
            name = "target",
            plugins = [
                "//third-party/java/immutables:processor",
            ],
            deps = [
                ...
            ],
            ...
        )
    

    Buck repository contains a custom rule (java_immutables_library) that adds this annotation processor so that developers don't need to specify plugins on every target. You can probably use similar approach.