GoogleTest, Google’s C++ test framework!

Tests are one of the most important parts of any CD/CI system. It helps you to ensure that a section of an application or your new codes(the unit) meet their design and behave as intended. By writing tests first for the smallest testable units, then the compound behaviors between those, one can build up comprehensive tests for complex applications. You may ask what makes a good test? Tests should be independent, repeatable, portable, reusable, well organized, provide as much information about the problem as possible, and finally, tests should be fast. Google’s C++ test framework, has it all!

Read more…

Compile c/c++ for android and make a native executable

Most of us are familiar with android studio or Android SDK to develop an Android App but what about binary executables?

Let say you have a C code like this and you what to compile it and run it on your Android phone just like a console application.

#include <stdio.h>
int main()
 printf("Hello world \n");
 printf("I'm running on Android\n");
 return 0;

Continue reading…

Concurrency in Android: Race condition

In this part, we investigate one of the multi-threading or concurrency issues. A common example of a correctness problem occurs when two threads need to modify the value of the same variable based on its current value. Let’s consider that we have a myInt integer variable with the current value of 2.

In order to increment myInt, we first need to read its current value and then add 1 to it. In a single-threaded world, the two increments would happen in a strict sequence—we will read the initial value 2, add 1 to it, set the new value back to the variable, and then repeat the sequence. After the two increments, myInt holds the value 4.

Continue reading…

Concurrency in Android: The Code

As mentioned before, in order to achieve a scalable application we need to create concurrent lines of execution to access the resources and calculate the results. Let say we want to download a file while the user has some interaction with the UI or we are trying to analyze data while we are publishing its real-time results to the user interface.

In order to achieve concurrency, Android SDK uses a subset of JAVA SDK, derived from the Apache Harmony project (although since Android 7 “Nougat” or API 24 it increasingly relies on OpenJDK libraries), gives developers access to low-level concurrency constructs such as java.lang.Thread, java.lang.Runnable, and the synchronized and volatile keywords.

The most basic one, java.lang.Thread, is the class that is mostly used and is the construct that creates a new independent line of execution in a Java program:

public class MyThread extends Thread {
    public void run() {
        Log.d("MyThread ", "Hello concurrent world!");

We can run our thread using this line of code, Let say in a button click listener or anywhere else:

MyThread myThread = new MyThread();

At this time, we will create an instance of our MyThread, and when we start it in the second line, the system creates a thread inside the process and executes the run() method.

We can give our thread a name(for debugging purposes ) or call the sleep method in our thread without interfering with the main UI thread. (Normally, if you call the sleep method in the main thread it hangs the UI)

public class MyThread extends Thread {
    public void run() {
        Thread.currentThread().setname("MyThread ")
        Log.d("MyThread ", "First Message!");
        Log.d("MyThread ", "Seconde Message!");

And also, we can call Thread.join() to block the current thread and wait for the other thread to finish or cancel.

There is another important class here, Runnable interface, and this is how we use it:

public class MyRunnable implements Runnable {

    public void run(){
        Log.d("MyRunnable", "Runnable Interface!");
        // Do the work here

And same as the Thread class, we can run our runnable using these lines of the code:

Thread thread = new Thread(new MyRunnable());

It seems pretty easy, isn’t it? While starting new threads is easy, concurrency is actually a very difficult thing to do. Concurrent software faces many issues that fall into two broad categories: correctness (producing consistent and correct results) and liveness (making progress towards completion). In the following posts, we are going to investigate them and use some examples to describe them.

Stay tuned and don’t be a stranger! 😉

Concurrency in Android: Intro

When we talk about concurrency in Android the first thing that may come to our mind is AsyncTask; If so, think again. An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread.

That was just before that Google announced that the famous AsyncTask class is deprecated!

In the description Google says:

AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks or crashes on configuration changes. It also has inconsistent behavior on different versions of the platform, swallows exceptions from doInBackground, and does not provide much utility over using Executors directly.

Which was completely true! everybody who works with AsyncTask can talk about their experiences with AsyncTask and some unexplainable behavior that AsyncTask does. So maybe to deprecate it was the only solution!

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

In the following posts, we are going to take a lock into the alternative(or the original) methods to implement concurrent tasks in Android os. We are going to solve some samples using the original java.util.concurrent package! Stay tuned!