Home Java javaTutorial 6 Android libraries that make your code cleaner

6 Android libraries that make your code cleaner

Feb 08, 2017 am 10:26 AM
android

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1236
24
New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Sep 11, 2024 am 06:37 AM

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size Sep 07, 2024 am 06:35 AM

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Sep 07, 2024 am 06:39 AM

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

Motorola Razr 50s shows itself as possible new budget foldable in early leak Motorola Razr 50s shows itself as possible new budget foldable in early leak Sep 07, 2024 am 09:35 AM

Motorola has released countless devices this year, although only two of them are foldables. For context, while most of the world has received the pair as the Razr 50 and Razr 50 Ultra, Motorola offers them in North America as the Razr 2024 and Razr 2

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

See all articles