Home > Java > javaTutorial > body text

6 Android libraries that make your code cleaner

高洛峰
Release: 2017-02-08 10:26:03
Original
1293 people have browsed it

Android development is fun - there's no doubt about it. However, there are still many platforms that force us to write repetitive boilerplate code. Many times this has to do with UI components that require you to work on them. There are some that you really need when you want your application architecture to be clean. There's a lot of operations happening asynchronously in the background; in fact, it's easy to end up with a bunch of spaghetti code that's unreadable or just doesn't feel right.

Today, we’ll look at 6 Android libraries that can help keep your code clear and readable, using example projects so you can see what the libraries do.

Project

We will use the Retrofit 2 Sample application that we used previously in the Retrofit guide. This is a simple open source project that can be found on GitHub. It requires a company name and a Git repository, and lists all contributors. The contributors are displayed as a list with avatars. While it's not a revolutionary app, it shows how to perform networking, work with images, create list components, and handle user input. You can play around with this fully functional toy project.

Let's apply the annotation library to the code and see how they can help maintain the cleanliness of Android app code.

1.Butter Knife

Whenever you need to access a view in code, you need to get an object instance of that view. You can do this by writing the rootView.findViewById() method and then convert the returned object to the correct view type. However, your code will quickly build up to annoying similar statements especially in the onCreate and onCreateView methods. Think about it; in those onCreate methods, you initialize everything, bind listeners, and tie the entire UI together. The more UI elements you have, the longer a single method will be.

Let's take a simple example:

6 个可以让代码变得更整洁的 Android 库

#This view will require three views: two EditTexts and a Button, which we need to reference in the fragment . Generally we will do this:

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {  // Inflate the layout for this fragment
  View rootView = inflater.inflate(R.layout.fragment_search, container, false);
  companyEditText = (EditText) rootView.findViewById(R.id.company_edittext);
  repositoryEditText = (EditText) rootView.findViewById(R.id.repository_edittext);
  searchButton = (Button) rootView.findViewById(R.id.search_button);

  searchButton.setOnClickListener(new View.OnClickListener() {    @Override
    public void onClick(View v) {
      searchContributors();
    }
  });  return rootView;
}
Copy after login

There is not much in the code other than finding views from the layout, storing them in the activity's fields, and adding an anonymous inner class as a listener to handle the search command. Lots of things happening. With Butter Knife we ​​can make our work and coding easier. View objects are stored in fields, so we can simply add the Butter Knife @BindView annotation to each field as follows:

@BindView(R.id.company_edittext) EditText companyEditText;
@BindView(R.id.repository_edittext) EditText repositoryEditText;
@BindView(R.id.search_button) Button searchButton;
Copy after login

We also need to make the onCreateView method aware of the existence of Butter Knife. Now, the initialization code will only contain the following short statement:

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {  // Inflate the layout for this fragment
  View rootView = inflater.inflate(R.layout.fragment_search, container, false);
  ButterKnife.bind(this, rootView);  return rootView;
}
Copy after login

We can go one step further and skip binding the listener to the searchButton method, and instead annotate the onSearchButtonClicked method, by magically binding it to the button click. @OnClick annotation:

@OnClick(R.id.search_button)
public void onSearchButtonClicked(View searchButton) {  searchContributors();
}
Copy after login

There are other examples on the official Butter Knife homepage. Why not check them out one by one! Generally speaking, if you need to access view elements programmatically, Butter Knife will make your code more concise and readable.

2.Ice Pick

A common problem faced by many Android applications is incorrect handling of activity and fragment lifecycles. Yeah, we know, it's not the most elegant part of the Android framework. However, disabling landscape mode in the AndroidManifest file so that the app doesn't crash when the user moves the device sideways isn't the right solution - firstly, because it looks a bit silly, and secondly, the code doesn't handle configuration changes properly. Still happens and ruins everything! Therefore, you must properly handle the state and lifecycle of application components.

The purpose of implementation is to store the contents of all fields in the activity into the bundle, and then correctly manage it through the life cycle by the Android framework. This can be quite boring.

幸运的是,Ice Pick使我们的生活变得容易多了,因为你再不必一个个添加所有的变量到bundle去保存。同样从bundle中再次读取数据,如果存在,那么会很有挑战性,但Ice Pick简化了很多很多。因此,作为示例,假设我们需要记住最后一家公司和存储库搜索的组合。

首先,我们对要保存到bundle的字段进行注解。

@State String lastSearchCombination;
Copy after login

现在我们需要在onSaveInstanceState()方法中调用Ice Pick:

@Overridepublic void onSaveInstanceState(Bundle outState) {  super.onSaveInstanceState(outState);
  Icepick.saveInstanceState(this, outState);
}
Copy after login

也在onCreateView()方法中调用Ice Pick来恢复状态:

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {  // Inflate the layout for this fragment
  View rootView = inflater.inflate(R.layout.fragment_search, container, false);
  ButterKnife.bind(this, rootView);
  Icepick.restoreInstanceState(this, savedInstanceState);  return rootView;
}
Copy after login

记住:你可以保存到bundle的内容的限制仍然存在。但是,没有必要因为为bundle键添加常量或为savedInstanceState添加空检查搞得一团乱。

3.Dart和Henson

与Ice Pick类似,Dart帮助我们避免为从一个活动传递到另一个活动的Intent Extras写入所有键和检查。它也适用于Fragments。这里有一个小例子,展示了我是如何使用@InjectExtra注释将搜索关键字从搜索屏幕传递到贡献者列表,实际上将执行搜索的地方。

所以我使用@InjectExtra注解定义了两个类变量:

@InjectExtra String repositoryQuery;
@InjectExtra String companyQuery;
Copy after login

一旦Dart.inject(this, getActivity());被调用,那么这些都将会被自动初始化。现在在Bundle中被添加到Intent的extras最终将如何。你可以手动进行,但这里使用Henson是非常有道理的。为了使它工作,我添加以下代码到我的SearchFragment:

Intent intentContributorsFragment = 
  Henson.with(getActivity())
        .gotoContributorsFragment()
        .companyQuery(companySearchKeyword)
        .repositoryQuery(repositorySearchKeyword).build();
Intent intentContributorsActivity = 
  Henson.with(getActivity())
        .gotoContributorsActivity().build();
intentContributorsActivity.putExtras(intentContributorsFragment);
startActivity(intentContributorsActivity);
Copy after login

这简化了代码中活动之间的通信,而无需每次都手动指定每个txtra。

4. Parceler

Parceler帮助你进行对象序列化。它可以帮助你传递任何对象作为Intent extra,而不会让你面对对象序列化的烦恼。

最好的事情是,Icepick,Henson和Dart也能很好地和Parceler一起玩。在我们的应用程序示例中,我使用@Parcel注释了我的Contributor类。这允许我使用Dart传递Contributor作为Intent Extra,使我的代码简洁和可读。

5.Timber

当我写代码的时候,过不了一会,我总有犯错误的倾向。通常情况下,这会导致应用程序的意外行为。我需要重现它,这样我才能解决这个问题。当你知道重现的步骤时,调试器会很方便,但是通常情况下,日志也包含了真相!

在Android中开箱即用的Log类足够好,因为可以提供不同的日志记录级别,等等。然而,每个Log.d()语句有两个参数;首先是tag,第二是message。99%的时间里,tag将是this.class.getName(),并且一次又一次地写会很烦人。幸运的是,使用Timber库,你只要这样做:

Timber.d("Informative output that needs to be logged.");
Copy after login

…并且它将为你提供正确的默认tag!此外,请记住,你需要在使用之前初始化Timber。查看我已添加调用的ContributorsApplication.onCreate()代码:

Timber.plant(new Timber.DebugTree());
Copy after login

这就是正确初始化Timber所有需要做的事情,所有没有理由你的app不使用Timber。

6.Dagger和Dagger2

最后,但并非最不重要的,Dagger和Dagger2库在app中管理依赖注入的表现真令人惊叹。为你处理依赖注入是编写代码的超棒做法。你指定应用程序的组件以及它们应如何相互交互。你可以定义代码的哪些部分需要其他部件的工作,瞧,这个库将为你初始化子部件,并根据需要注入它们。你可以检查示例项目代码以查看使用示例。

However, Dagger and Dagger 2 are too broad, so we will not explain them in detail in this article. If you want to start using Dagger, there is a great code example, coffee maker, which is also supported by excellent annotations.

Conclusion

There are many interesting Android libraries, I have only listed a few here. Installation is fairly easy as you only need to specify the dependencies. These are actively maintained projects, so they have great documentation.

All you have to do is be careful with the build process. When you start combining multiple libraries with annotation processors, make sure to use provided() or annotationprocessor() instead of combining them in build.gradle.

For more 6 Android libraries that can make the code cleaner, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template