본문 바로가기

카테고리 없음

[Android] ButterKnife Library 소개

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
http://blog.teamtreehouse.com/android-libraries-use-every-project

ButterKnife

On to my favorite libraries! Let’s start with a test: which of these do you like better?

protected TextView mWelcomeLabel;
protected EditText mUsernameField;
protected EditText mPasswordField;
protected Button mSubmitButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mWelcomeLabel = (TextView) findViewById(R.id.welcomeLabel);
  mUsernameField = (EditText) findViewById(R.id.usernameField);
  mPasswordField = (EditText) findViewById(R.id.passwordField);
  mSubmitButton = (Button) findViewById(R.id.submitButton);
}

or

@InjectView(R.id.welcomeLabel) protected TextView mWelcomeLabel;
@InjectView(R.id.usernameField) protected EditText mUsernameField;
@InjectView(R.id.passwordField) protected EditText mPasswordField;
@InjectView(R.id.submitButton) protected Button mSubmitButton;

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

The latter code is more concise and understandable, don’t you think? The second block of code is using a library called ButterKnife, which uses annotations to “inject” views by creating boilerplate code for you. ButterKnife is small, simple, and lightweight, and because it makes your life as a developer easier, you should pretty much always use it. It would be great if the Android SDK itself could improve in this manner!

There are additional attributes you can use to make OnClickListeners and other common, verbose aspects of Android development easier to write and understand.

Below is all you need to include this library automatically in your Android Studio projects. Just add this one line to your app’s build.gradle file (in app/src):

compile 'com.jakewharton:butterknife:5.1.2'

You need to add it in the dependencies section, like this:

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  compile 'com.jakewharton:butterknife:5.1.2' 

}


위 라이브러리를 쓰면 불필요한 소스를 좀더 간편하게 할 수 있어서 편할 듯 쉽다.

그래서 공유차 적어놓았다.