Skip to content
Related Articles

Related Articles

Improve Article

12 Best Practices For Android Development

  • Difficulty Level : Expert
  • Last Updated : 15 Aug, 2021
Geek Week

As Android engineers, we’re all motivated by the desire to create memorable experiences for people all across the world. And, with more people relying on your apps than ever before, expectations are higher, and your job as a designer isn’t getting any easier. Now not only do you have to be updated but also stand unique from other developers to make your unique mark in the market. Listed down are few best practices that you can take on to have better outcomes in your development.

Best Practices for Android Development

1. Choose Suitable Development Method

Not all apps are built with the same approach. You have to brainstorm, plan and pick an appropriate method before starting the development process. Before proceeding you will have to make a prior move that your further app will be a Native, Hybrid, or Web-based app. There are pros and cons of each of these development methods, having proper research will help you better. 

2. Maintain High Code Quality 

We all know that Coding is an Art. And once an individual starts coding correctly he/she starts loving the flavor. Moreover how creatively you code, shows how better developer you are. Your Quality Of Code Always Matters. Because writing long code is never a smart step, it not only increases the chances of having more bugs and consumes a lot of time, and creates lots of complexity. Here are few points listed you can adopt for superior code quality. 



  • To give the finest look to your code you can initially study the code of an expert, analyze their approach and try to modify that.
  • Provide the purposes of every function you use.

3. Always keep a consistent coding style and accurate Architecture

  1. Use descriptive names.
  2. Always keep your code simple and sober, remove unnecessary lines and keep your work short and descriptive.

Tip: Use Proguard, this will help to remove all unused codes and minimize the APK size. 

4. Learn How To Use Android Documentation 

Android itself is a very vast concept, and it’s very much impractical to imagine that you are a master in all the concepts and techniques. And when it comes to programming or building application nothing can help you out other than the stack’s documentation. Learning how to use/read and understand the Android Documentation is important this will help you to build lots of applications with a variety of features.  So it’s highly recommended to go through developer training at android:- Getting Started | Android Developers 

5. User Input 

Your project work seems more friendly if it’s highly interacted with the users and it seems more helpful as well. While coding, every text field is planned for a different job. We take input in some text fields as numbers or as alphabets. It is counted as better practice if only the number pad is open if we ask the user for numeric value. The syntax will be:

XML




<EditText
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/editText"
   android:layout_alignParentRight="true"
   android:layout_alignParentEnd="true"
   android:hint="User Name"
   android:layout_below="@+id/imageView"
   android:layout_alignLeft="@+id/imageView"
   android:layout_alignStart="@+id/imageView"
   android:numeric="integer" />

Other than that if your field is for the password, then it must show a password hint to, easily remember the password. It can be achieved as.

XML




<EditText
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/editText2"
   android:layout_alignLeft="@+id/editText"
   android:layout_alignStart="@+id/editText"
   android:hint="Pass Word"
   android:layout_below="@+id/editText"
   android:layout_alignRight="@+id/editText"
   android:layout_alignEnd="@+id/editText"
   android:password="true" />

6. Avoid Deep Level in Layouts 

Having deep hierarchical code views actually makes the UI slower to manage the layout. Deep hierarchies can mostly be avoided by using the correct view group. It’s a recommendation to use a single-level hierarchy.

XML






<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
  
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/magnifying_glass" />
  
    <TextView
        android:id="@+id/top_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:text="top text" />
  
    <TextView
        android:id="@+id/bottom_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/top_text"
        android:layout_toRightOf="@id/image"
        android:text="bottom text" />
</RelativeLayout>

7. Keep Design In Mind 

The duties of Developers are different that from Designers. For the maximum part, builders tend to be great with technical concepts, whilst designers have recognition for being greater creative. But with that said, you can’t have your blinders on while you’re growing an app. You want to apprehend how the layout additives are going to create so that you can code the app accordingly. To get designers and developers operating collectively and effectively, your whole group desires to be on the identical page. You can use mood boards and lots of other tools to have a thought and maintain the tracking of the design elements organized. This will make your improvement manner plenty less difficult and decrease the possibilities of getting to make masses of modifications overdue in the sport on the way to accommodate the designers. For the ones of you that recognize a way to expand and layout, you should still sort out your design elements in these early stages.

8. Speeding up Android Gradle Builds 

  • Always keep your Gradle updated and use the latest Gradle plugin for android.
  • Enable Offline mode, Gradle Daemon, and Parallel build of your project.  to use Gradle offline see the setting done below.

Check the Offline work checkbox. Click Apply or OK.

  • Use specific dependency versions instead of Dynamics versions.
  • Add below flags in gradle.properties
  • org.gradle.jvmargs=-Xmx2048m – Amount of memory allocation increases around 2GB to the Gradle Daemon VM.
  • org.gradle.daemon=true – improves the build and allows the data and code to store in memory for the next build.
  • org.gradle.configureondemand=true – only project-related tasks are configured, saves time from the Configuration phase of Gradle.
  • org.gradle.parallel=true – executes tasks from different projects in parallel. Especially useful for modularising the app.
  • android.enableBuildCache=true – speed up build items by sorting files and directories created in previous builds.
  • org.gradle.caching=true – Turns on Gradle-caching.

9. Go for  a selective layout and Re-using layouts with <include/> tag

Be it a website or an app the way of picking a layout before developing your project. But before we choose any layout we need to understand that how the UI of the project is created. So when we talk about layouts as we know there are ConstraintLayout, LinearLayout, RelativeLayout, FrameLayout, CoordinatorLayout, and out of all these layouts, you just need to do performance analysis for some of them before getting started with it. If you have some part of your XML getting reused and you want to Avoid replication of code in different layouts. Just add  <include/> by extracting them in a separate layout. 

10. Using Android Debug Bridge (ADB) for your project

So, ADB is basically a tool that is not only used for reverse engineering but also by a lot of android-tech enthusiasts but very few numbers its total potentials or use it at the fullest and is mostly overlooked. ADB- a command-line tool or we can say a client-server program that lets you communicate with android operating systems. Though developers are not using the features of this tool daily or as quotidian, you can ease your lots of developing experiences. It has lots of features like it can add or remove files/data from your android devices and also address lots hidden of data with just a few clicks on your computer. Developers use these tools for testing and making changes in android OS devices. If a user forgets his mobile password, we can use ADB to delete the password file from the android directory, reboot it and the device is unlocked. 

11. Handling the Configuration changes of your project/app 

Handling the configuration of the android app is one of the hectic tasks for android developers. Basically what happens in configuration change is the android usually destroys your application’s existing Activities and Fragments and recreates them. Proper handling of the Orientationgives a rich-user experience and makes the app work smooth too. Now you want to listen for simple screen orientation changes programmatically and have your application react to them, you can use the OrientationEventListener class to do this within your Activity. Let’s see its implementation in Activity. Just instantiate an OrientationEventListener and provide its implementation. Following is the example of the Activity class called SimpleOrientationActivity logs orientation information to LogCat:

Java




public class SimpleOrientationActivity extends Activity {
    OrientationEventListener mOrientationListener;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
  
        mOrientationListener = new OrientationEventListener(this,
            SensorManager.SENSOR_DELAY_NORMAL) {
  
            @Override
            public void onOrientationChanged(int orientation) {
                Log.v(DEBUG_TAG,
                    "Orientation changed to " + orientation);
  
            }
        };
  
       if (mOrientationListener.canDetectOrientation() == true) {
           Log.v(DEBUG_TAG, "Can detect orientation");
           mOrientationListener.enable();
       } else {
           Log.v(DEBUG_TAG, "Cannot detect orientation");
           mOrientationListener.disable();
       }
  
    }
  
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mOrientationListener.disable();
    }

12. Provide Top-most Security 

No matter what sort of application the developer intended to make but security should definitely be prioritized by the developer, from facts security clouds are one of the most neglected because it’s not only a large concept but many seem to be imperfect with it. Your apps will have tons of data and the sensitive information of your company as well as the users on your app. Depending on the app you develop may have payment information crucial information like home address, phone number, and many more things of a user-provided the. Users will hesitate to provide you with information if your app isn’t secure. If security gets breached, it could be so damaging to your brand that it will be tough to recover.

So as a developer it’s your duty to take measures and use encryptions and other tactics like (Two- Factor-Authentication 2FA), to protect sensitive info. 

Tip: Make sure your application has the feature to wipe out all the data and set up automatic backup as well if urgent the user demands.

Now we’ll mention a few of the quick tips which can boost  your productivity

  1. Switch to Android Studio – It’s an official IDE of Android
  2. Use Android Debug Database– Allow you to use the database and send preferences into the browser.
  3. Use legit external libraries and give them required permissions only. Firstly ignore third-party libraries.
  4. Avoid using floating-point – Floating points are around 2times slower than the integers on android devices.
  5. If there are lots of modifications to strings of characters, Use StringBuffer or StringBuilder classes.
  6. Install Modularization in-App.
  7. Use strings.xml – Adding text as String resources is always useful in the long run especially when support for new languages needs to be added.

Conclusion

Developing a mobile app is not everyone’s cup of tea. It’s not only a vast development stack but also has lots of varieties in each of its parts. On your road of development, you will have to cross bumps and breakers to achieve milestones, So, I believe having few guidelines and best practices helps you develop the application much easier if you implement it in your routine.   

GeeksforGeeks LIVE courses




My Personal Notes arrow_drop_up
Recommended Articles
Page :