Planning on building an android application for the first time can be overwhelming. This guide gives you the essential guidelines on principles that you must incorporate into your code.
If you’re planning on building an android application for the first time, you’re most likely overwhelmed by the plethora of technicalities that you need to keep in mind. If you’ve tried exploring the internet regarding advice for beginners, you must have come across dozens of tips opinions regarding the best practices to follow when developing an android application on various platforms, each advocating something different. So what exactly should you do?
This article outlines about some of the most basic (yet crucial) pointers that you must keep in mind when building your first android application. These principles are followed by even the most successful android developers around the globe and will get you started on your journey towards becoming an android developing wizard.
1 - Follow the Basic Trifecta of Principles:
Even before you begin to actually write down the code for your application it is important to make sure that your code will have the following principles:
You can achieve these key principles by using an architected code, without which the application that you build is not likely to perform well in the long run. This is because as your application grows, it will come across various needs that you might not have envisaged previously and will require revisions in your code.Thus, if your code is unable to accommodate these changes with the passage of time, then you do not have an easily adaptable application- definitely not something that you wish to build, even for your first time.
2 - Ensure Testability of Your Code
Another reason why it is recommended to develop an architected code is because it is much easier to write test cases in it as compared to a non-architected code.
You can ensure the testability of your code by defining test features before starting development of that particular feature.
This practice is known as test driven development (TDD) which involves writing test cases before actually writing the code, which is in contrast to behavior driven development (BDD),as depicted by the infographic below.
Defining Your Test Cases is Crucial
Test cases define the possible usage and functions of the application. This is intended to test how the app would work in different scenarios prior to launching the application.
It is important for you to clearly define all the features of the application so that you can write a comprehensive set of test cases.
You can use the following tools to conduct test cases:
JUnit:JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. This is used to write unit tests for methods and logic, and to simulate method inputs and test output.
EspressoEspresso Test Recorder is used for conducting and launching UI tests. It tests whether the screen is working or how you would want it to work on different devices. This includes aspects such as proper screen alignment, button placement and text visibility. This test is especially useful because it displays how the screen would show on a number of different devices without having to test the application on each device separately.
Mockito/RobolectricThis tool can be used to create simple mock objects. It simulates the database and ensures that the test conditions are always the same in the application.
All of this requires more time, but is a beneficial trade-off as when application grows you would have to spend less time in coding which makes your life easier in the future.
3 - Check for Any Signs of a Memory Leak
“A small leak will sink a great ship.” - Benjamin Franklin
Memory leak occurs when "programmers create a memory in heap and forget to delete it". A component in the Android Profiler, the Memory Profiler aids developers in identifying memory leaks and memory churn which might cause stutters, freezes, and even app crashes. It shows a real time graph of your app's memory use and lets you capture a heap dump, force garbage collections, and track memory allocations.
The best tool that is recommended for memory profiling is LeakCanary.
Common Causes For a Memory Leak:
The most common cause of a memory leak is the bugs that are related to the lifecycle of the objects. Given below are few very frequent Android mistakes:
- Storing an Activity context as a field in an object that survives activity recreation configuration changes.
- Registering a listener, broadcast receiver or RxJava subscription which references an object with lifecycle, and forgetting to unregister when the lifecycle reaches its end.
- Storing a view in a static field, and not clearing that field when the view is detached.
4 - Remember: Continuous Integration Is a Must
Continuous Integration (CI) is a development practice where developers integrate code into a shared repository frequently, preferably several times a day. Each integration can then be verified by an automated build and automated tests.
It is advisable for your code to be continuously integrated. This becomes beneficial when a new developer pushes your code to the depository whereby the system automatically runs all the test cases again and ensures the entire application is intact and ensures that all of the test cases that you have written pass the test. This is essential to make sure that any new code added into the application does not disrupt or break anything: ensuring stability of the application.
Continuous integration also compiles the code to make a build, configure that build, and also produce post-build report that It also alerts the system in case of any breakages in the app. Continuous integration consists of the following basic steps:
Compile the code
Run test cases
Configure build reports/ tests cases reports
One of the recommended tools for continuous integration is Jenkis.
5 - Don't Forget to Obfuscate Your Code
In software development, obfuscation is the deliberate act of creating source or machine code that is difficult for humans to understand. Like obfuscation in natural language, it may use needlessly roundabout expressions to compose statements. Code is not readable anymore, if a hacker gains access to APK and tries to get code through reverse engineering then he is not able to build the code.
This is especially useful when you wish to protect intellectual property and to prevent any kind of cyber attacks.
6 - Choose Methods to Save Data in Your App Based on Data Type
It is extremely important to be mindful of the type of data when you choose a method to save it in your application. Here are some basic guidelines:
- For small/non-critical data: Use shared preferences for small data. If that is not enough use Content Providers or store in database
- For sensitive data: Sensitive data should not be stored in shared preference, use keystore for this instead. In case of extremely sensitive information such as credit card details, it is advisable to not save such information at all.
By following the guide given above, you can be sure to build an application that is sustainable in the long-term, in a dynamic environment that requires constant changes and upgrades in technology. Once you become habitual of making sure you fulfill this criteria when developing an app, it won’t be long until you join some of the most successful developers in the world.