Java in Android is a variant of Java

Android app development uses Java platform and you can consume JDK (java development toolkit) in Android.

This above statement is partially true however the fact is that some part of Java SDK is being pulled out and packaged it in Android SDK. Second fact is that we can use Java language to consume Android SDK. However today, it is recommended to use Java 6 for Android projects. (I will come to answer that why)

Notice If you expand the android.jar you can view that it is composed of several different packages of open source libraries. So Android SDK is actually a collection of snapshot of several open source libraries at a particular time such as Java SDK/OpenJDK, Apache HttpClient, json.org, w3c dom etc. and of course the android core APIs and some of them are even uniquely modified for Android.

This all leads to an interesting question in the community I read some days ago on Reddit

Can we leverage Java 8 features in Android?

The answer is not yet.

Why? (you ask!)

Because remember we read that Android run on its own Dalvrik VM instead of Java standard VM (JVM) to convert byte code to executable.Therefore if any feature has been added in Java 8, its written to produce certain byte code by Java compiler which is then worked out by updated JVM. However that features needs to be supported in Dalvrik VM as well. Plus authors of Android needs to pull latest version of Java packages into the android.jar.

Hope this clears out confusion for some android developers!

Further readings:

  1. Java vs. Android APIs
  2. Will Android get Java 7 or 8 in a forseeable future?

Android NetworkOnMainThread Exception

Hi folks,

Like me, if you are new to android platform and working with network requests such as using HttpRequest, HttpGet, or HttpPost etc. YOU may come across a “NetworkOnMainThread” exception or your application stopped responding. This really annoys in a way that your code complies nicely and smells like *it should work* but on runtime it throws an exception which result in application termination.

The other day I was working with RESTful services and was developing a sample code to send simple HttpPut request to my services and got this error.

Solution:

Most likely the root cause will be either you are calling some network activity on your main thread and it has restricted thread policy and/or your app does not have enough permission. It can be solved by taking following steps:

  1. In AndroidManifest, make sure your application has permission to access network/internet.

  2. Use AsyncTask to enable network request to run asynchronously on another thread. Following is the code snippet that demonstrates use of HttpPut request using AsyncTask

Disclaimer:The code below is not optimized to be used in production environment, it’s just a sample to give idea about the solution. All views expressed are mine only

This would solve the problem!

For further reading, I would suggest to go through an article on performance “Android Threads, Handlers, and AsyncTask – Tutorial