Git Product home page Git Product logo

butterfork's Introduction

Butter Fork

Logo

A fork of ButterKnife with support for Library projects (Solves the non-final id problem).

This change is incompatible with the current library and is therefore distributed under a different name. See the Why ButterFork? section below.

Usage is identical apart from 'Fork' instead of 'Knife' and 'B' instead of 'R'. The solution does not depend on reflection.

@Bind(B.id.user) EditText username;
@Bind(B.id.pass) EditText password;
//..
ButterFork.bind(this);

Dependency

Latest version: Maven Central

There are three components; the runtime module (butterfork-binder), annotation processor (butterfork-compiler) and a gradle plugin (butterfork-plugin). You will also need Hugo Visser's android-apt.

Your build.gradle should look like this:

buildscript {
    dependencies {
        //..
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7'
        classpath 'com.oguzbabaoglu:butterfork-plugin:1.0.0'
    }
}

// It is important that the library plugin is applied before butterfork-plugin
apply plugin: 'com.android.library'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.oguzbabaoglu.butterfork-plugin'

dependencies {
    compile 'com.oguzbabaoglu:butterfork-binder:1.0.0'
    apt 'com.oguzbabaoglu:butterfork-compiler:1.0.0'
}

How does it work?

On a clean build, the first thing to run is the plugin. The plugin adds a generateB task right after processResources (task that generates R) which generates a B class containing all field names in the R class as final String values.

public final class B {
  public static final class id {
    public static final String text_view = "text_view";
  }
  public static final class string {
    public static final String app_name = "app_name";
  }
}

Values from the generated B class should be used in place of R values in the annotations.

@Bind(B.id.text_view) TextView textView;
// Instead of 
@Bind(R.id.text_view) TextView textView;

The processor reads these annotations and generates binder classes. ButterKnife uses the resolved int value from the R class, but ButterFork instead places a reference back to the R class inside the generated code.

// What the ButterKnife processor generates:
view = finder.findRequiredView(source, 2131755092, "field 'textView'");
// What the ButterFork processor generates:
view = finder.findRequiredView(source, R.id.textView, "field 'textView'");

The runtime module is identical to ButterKnife (the binder), since the explicit R reference is resolved to an int when the generated classes are compiled.

Limitations

  • Currently B is only generated for the 'local' R. It it is not possible to use R references from other libraries or even android.R.
  • B is generated when gradle runs, whereas R is continually updated by the IDE. Therefore B will always lag behind R. This is especially problematic when resources are renamed using the IDE. It may however be possible to fix this by creating an IDE plugin (with a file watcher on R) to generate B instead of gradle.
  • Although the B class helps a lot with keeping things compile-safe, it is still less safe than using R directly since the compiler will happily accept any string value only to fail after generating the (invalid) binding classes. It will not however carry on to runtime as the generated classes reference R instead of using reflection on it.

Why ButterFork?

There are a couple of issue threads where this problem was discussed for ButterKnife. In these you can find detailed explanations from Jake Wharton about why it is a bad idea to add this support to the existing library:

License

Original work Copyright 2013 Jake Wharton
Modified work Copyright 2015 Oguz Babaoglu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

butterfork's People

Contributors

jakewharton avatar oguzbabaoglu avatar imminent avatar tomxor avatar bananeweizen avatar christiankatzmann avatar holmes avatar vanniktech avatar f2prateek avatar nguyentruongtho avatar lexs avatar aried3r avatar emilsjolander avatar farzanegan avatar jonreeve avatar kevinmost avatar lukaciko avatar mnonnenmacher avatar esteluk avatar pyricau avatar solcott avatar tonsv2 avatar simonvt avatar realdadfish avatar tonycosentini avatar naturalwarren avatar willgryan avatar frankiesardo avatar ligi avatar mlc avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.