How to make Android App Faster - Android App Optimization

Optimizing your Android App to make it faster and efficient is what will set your app apart from the crowd of apps in the Play Store. I use some optimization and good practice that I thought of sharing with you guys, maybe it can help you write better code and improve your app performance. If you are interested in video, you can check the video too:



The are mostly generic type and can be used with most programming languages. The things that we'll cover to optimize Android Performance are listed below:

android-app-optimization

  1. Don't create unnecessary objects:

    As you can see in the example below, we are creating a new variable hi just to store some data that is no longer further used. If you have such scenarios, don't create a new variable especially if it's not a primitive data type.

    private String getString(String name){
        String hi = "Hello" + name;
    }


    Instead of creating a new object, we can just return the data as show below.

    private String getString(String name){
        return "Hello" + name;
    }

  2. Create static methods where applicable:

    If your method is not using any non-static variables in the  class, make the method static as it is faster and you don't need to initialize the class to access these methods.

    private static String getString(String name){
        return "Hello" + name;
    }


  3. Create final static variables where applicable:

    You can make the static int below final, and it is relatively inexpensive.
    static int val = 12;
    final static int val = 12;

    Without the final keyword used, the values are accessed using field lookups, but if we use final, then constants go into staic field initializers in the dex file. Code that refer to val will use the value 12 directly without the filed lookup and that is inexpensive than using a field lookup.

  4. Use the Enhanced For loop:

    We all learnt the basic for loop and have been using it for a long time, but it's been more than a decade since the enhanced for loop was introduced so it's only fair we use it in out code too.

    int[] array = ...

    public void func1() {
        int sum = 0;
        for (int i = 0; i < array.length; ++i) {
            sum += array[i];
        }
    }

    public void func2() {
        int sum = 0;
        int length = localArray.length;

        for (int i = 0; i < length
    ; ++i) {
            sum += localArray[i];
        }
    }

    public void func3() {
        int sum = 0;
        for (Foo a : array) {
            sum += a;
        }
    }


    The func3 here it the best for devices without a JIT, it's performance it basically the same for devices with JIT however, consider using the enhanced loop most of the time not just because of the speed in case of normal arrays, but also because this looks much cleaner.
    However, do note that this is slower with arraylist, so if you have an ArrayList, handwritten for loop is faster.
  5. Minimize the access to private methods from inner class:


    public class MyClass{
        private class MyInnerClass {
            void doStuffIn() {
                Foo.this.doStuff(Foo.this.mValue);
            }
        }

        private int mValue;

        public void run() {
        MyInnerClass in = new MyInnerClass ();
            mValue = 27;
            in.stuff();
        }

        private void doStuffEx(int value) {
            System.out.println("Value is " + value);
        }
    }


    As we can see in the example above that the MyInnerClass accesses a method that is private in the parent class, method doStuffEx. In Java, this is syntactically correct and works as expected but do not that this breaks the concept of OOP as a class's private function is accessed by another class albeit, an inner class. But to allow this "syntax", the compiler needs to generate a couple of synthetic methods.

    These synthetic methods are use by the inner class whenever it needs to access the doStuffEx() method in the outer class. These synthetic methods are auto-generated and called accessors. So basically we are using accessors to access the private method or a class. Accessors are considerably slower, and this is a common place where performance takes a hit that is ignored by programmers.

    It you have a scenario like this, consider making the private method to have package access if it doesn't break your security, although it will mean that the method will also be accessible by other classes in the package.
  6. Use int where possible.

    Usually floating point usage is slower, noticeably slower in Android devices..In the terms of performance, double and float have the same speed, however double takes double the size of float, so if memory/space isn't an issue, go with double.

    So, it's mostly a choice between int or double if space is not a concern.

Comments