Tips for improving the coding of Android applications

In coding, choosing the right algorithm and the right data is very important. Here are some tips to strengthen your coding skills.

Applications, There are two basic rules for writing good code:

  • Do not do what you do not need to do
  • Avoid allocating memory if possible

One of the problems that Android apps suffer from is that the app runs on multiple hardware. Different versions run at different speeds. In general, you can not say that device x is faster than device y. There is a big difference between a device that has a JIT and a device that does not have a JIT. Code that is appropriate for a device that contains JIT Sometimes it is not appropriate for a device that does not have JIT. To make sure that the program you create works on most devices, you should evaluate your code in all respects.

Free download 3 of the best PDF programming training booklets for Android

Avoid unnecessary topics

  • Creating themes is not free. Using memory always costs more than not using memory.
  • If your method returns a string, and you are sure that this method causes a string to be appended, change your method so that your function is appended directly.
  • When trying to extract a string from the input data, try to return a subset of the original data instead of copying it.
  • The int array is much better than the integer array. Two parallel arrays int are much more efficient than arrays (int, int).
  • When you want to run a container that contains multiple (Foo, Bar), keep in mind that using [] Foo and [] Bar will be much more effective than an array (foo, bar).

Using static instead of virtual (virtual)

Use the static method if you do not want to access the subject field. This method will be 15 to 20% faster.

Use statics for constants in applications

Suppose the following sentence is placed at the top of a class:

static int intVal = 42;
static String strVal = “Hello, world!”;

This creates a method called clinit. This method stores the number 42 in intval. You can use the word final to improve the above sentence.

static final int intVal = 42;
static final String strVal = “Hello, world!”;

Use incremental for loops in applications

This loop can be used to execute arrays

static class Foo {
int splat;

Foo [] array = ar

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

public void one () {
int sum = 0;
Foo [] localArray = array;
int len ​​= localArray.length;

for (int i = 0; i <len; ++ i) {
sum + = localArray [i] .splat;

public void two () {
int sum = 0;
for (Foo a: array) {
sum + = a.splat;

Zero () is the slowest because JIT cannot optimize the array length for each iteration in the loop.

One () is faster, it puts everything into local variables.

Two () For the device that does not have JIT, the fastest possible mode.

Consider a package with private indoor classes instead of private applications access

Consider the class defined below:

public class Foo {
private class Inner {
void stuff () {
Foo.this.doStuff (Foo.this.mValue);

private int mValue;

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

private void doStuff (int value) {
System.out.println (“Value is” + value);

Here we define a private internal class that has access to a private internal method and a private field in the external class. This code will print the number 27.

The problem is that the VM considers this code illegal. Use the following code to solve this problem

/ * package * / static int Foo.access $ 100 (Foo foo) {
return foo.mValue;
/ * package * / static void Foo.access $ 200 (Foo foo, int value) {
foo.doStuff (value);

Do not use a floating point

Running floating-points on Android devices is twice as slow. In modern hardware, there is no difference in speed between float and double. In terms of space, double is twice as big. If space is not a limiting issue, you should use double.

Get to know libraries and use them

Always keep in mind that code in libraries can be better than the best JIT code for Java. The System.arraycopy method, for example, can be 9 times faster than the code written by JIT on the Nexus.

Use private methods very carefully

Using Android NDK code is not necessarily more effective than Java code. Sometimes JIT is not able to optimize private content. For example, the special code used for the ARM processor in the G1 does not run well on the Nexus, and the special ARM code in the Nexus does not run on the G1.

You should only use this code when you have a code and want to port it to Android, and your goal should not be to speed up your Android application, which is written in Java.

What programming language should we use to create and develop Android applications?

Always measure

Before you start optimizing, first make sure there is a problem that you need to solve. Make sure you can evaluate your current performance.

Traceview can help you, but keep in mind that this will disable JIT. It is very important to make sure that the code executes faster after applying the changes suggested by Traceview than not using Traceview.

Leave a Reply

Your email address will not be published. Required fields are marked *