search
HomeJavajavaTutorial6 Android libraries that make your code cleaner

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;
}

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;

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;
}

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();
}

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;

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

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

也在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;
}

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

3.Dart和Henson

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

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

@InjectExtra String repositoryQuery;
@InjectExtra String companyQuery;

一旦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);

这简化了代码中活动之间的通信,而无需每次都手动指定每个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.");

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

Timber.plant(new Timber.DebugTree());

这就是正确初始化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
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
Is Java Platform Independent if then how?Is Java Platform Independent if then how?May 09, 2025 am 12:11 AM

Java is platform-independent because of its "write once, run everywhere" design philosophy, which relies on Java virtual machines (JVMs) and bytecode. 1) Java code is compiled into bytecode, interpreted by the JVM or compiled on the fly locally. 2) Pay attention to library dependencies, performance differences and environment configuration. 3) Using standard libraries, cross-platform testing and version management is the best practice to ensure platform independence.

The Truth About Java's Platform Independence: Is It Really That Simple?The Truth About Java's Platform Independence: Is It Really That Simple?May 09, 2025 am 12:10 AM

Java'splatformindependenceisnotsimple;itinvolvescomplexities.1)JVMcompatibilitymustbeensuredacrossplatforms.2)Nativelibrariesandsystemcallsneedcarefulhandling.3)Dependenciesandlibrariesrequirecross-platformcompatibility.4)Performanceoptimizationacros

Java Platform Independence: Advantages for web applicationsJava Platform Independence: Advantages for web applicationsMay 09, 2025 am 12:08 AM

Java'splatformindependencebenefitswebapplicationsbyallowingcodetorunonanysystemwithaJVM,simplifyingdeploymentandscaling.Itenables:1)easydeploymentacrossdifferentservers,2)seamlessscalingacrosscloudplatforms,and3)consistentdevelopmenttodeploymentproce

JVM Explained: A Comprehensive Guide to the Java Virtual MachineJVM Explained: A Comprehensive Guide to the Java Virtual MachineMay 09, 2025 am 12:04 AM

TheJVMistheruntimeenvironmentforexecutingJavabytecode,crucialforJava's"writeonce,runanywhere"capability.Itmanagesmemory,executesthreads,andensuressecurity,makingitessentialforJavadeveloperstounderstandforefficientandrobustapplicationdevelop

Key Features of Java: Why It Remains a Top Programming LanguageKey Features of Java: Why It Remains a Top Programming LanguageMay 09, 2025 am 12:04 AM

Javaremainsatopchoicefordevelopersduetoitsplatformindependence,object-orienteddesign,strongtyping,automaticmemorymanagement,andcomprehensivestandardlibrary.ThesefeaturesmakeJavaversatileandpowerful,suitableforawiderangeofapplications,despitesomechall

Java Platform Independence: What does it mean for developers?Java Platform Independence: What does it mean for developers?May 08, 2025 am 12:27 AM

Java'splatformindependencemeansdeveloperscanwritecodeonceandrunitonanydevicewithoutrecompiling.ThisisachievedthroughtheJavaVirtualMachine(JVM),whichtranslatesbytecodeintomachine-specificinstructions,allowinguniversalcompatibilityacrossplatforms.Howev

How to set up JVM for first usage?How to set up JVM for first usage?May 08, 2025 am 12:21 AM

To set up the JVM, you need to follow the following steps: 1) Download and install the JDK, 2) Set environment variables, 3) Verify the installation, 4) Set the IDE, 5) Test the runner program. Setting up a JVM is not just about making it work, it also involves optimizing memory allocation, garbage collection, performance tuning, and error handling to ensure optimal operation.

How can I check Java platform independence for my product?How can I check Java platform independence for my product?May 08, 2025 am 12:12 AM

ToensureJavaplatformindependence,followthesesteps:1)CompileandrunyourapplicationonmultipleplatformsusingdifferentOSandJVMversions.2)UtilizeCI/CDpipelineslikeJenkinsorGitHubActionsforautomatedcross-platformtesting.3)Usecross-platformtestingframeworkss

See all articles

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

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software