Friday 7 August 2015

How to Speed Up Android Studio

Google announced that they were working on Android Studio as the IDE to replace ADT. If you have just started using Android Studio or are new to Gradle, you may have noticed that any of the calls to Gradle perform very slow.  This is very noticeable while trying to build the project.  This is very discouraging. after some search over internet finally i got solutions to solve this problem and that is working for me :) Following are some ways for Android Studio and Gradle Build system to make them perform more faster. 


1. Android Studio 
-Launch: Notepad++ as administrator
-Open: C:\Program Files\Android\Android Studio\bin\studio64.exe.vmoptions
There are a lot of parameters, but we are interested in memory-related parameters:
-Xms: Specifies the initial size, in bytes, of the memory allocation pool.
-Xmx: Specifies the maximum size, in bytes, of the memory allocation pool.
-XX:MaxPermSize: Size of the Permanent Generation.
-XX:ReservedCodeCacheSize: Reserved code cache size
Now, I’m using the following values:
-Xms1024m
-Xmx2048m
-XX:MaxPermSize=1024m
-XX:ReservedCodeCacheSize=512m

2. Gradle
Gradle is the new build system used in Android Studio
Now, we will configure the global settings of Gradle:
  • Goto: C:\Users\<UserName>\.gradle
    make a new file called “gradle.properties”
  • Add the following lines to the file:
    org.gradle.parallel=true
    org.gradle.daemon=true
  • Launch Android Studio and goto File -> Settings -> Gradle then enable “Offline work”
  • Restart Android Studio and check if there is any Speed Improvement



Tuesday 9 June 2015

Send Email Via Gmail with Java Mail API in Android

To Android app that are capable of sending an automatic mail without prompting user for to address, from address, subject and body of mail. In a simple word, we need to create an Android app that sends from address of the user to a particular mail-ID.

For use of this you need to download the some jar file from Here.
1.  additional.jar
2. mail.jar
3. activation.jar

Download these jar file and make a java build path with your project. 

Below is sendEmail() method for sending email. use this method in your application and enjoy :)


public void sendEmail() throws AddressException, MessagingException {
String host = "smtp.gmail.com";
String address = "abc@gmail.com";
String from = "abc@gmail.com";
String pass = "123456789";
String to = "abc@live.com";
Multipart multiPart;
String finalString = "fsdfsdfdsfdsfdsf";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", address);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Log.i("Check", "done pops");
Session session = Session.getDefaultInstance(props, null);
DataHandler handler = new DataHandler(new ByteArrayDataSource(
finalString.getBytes(), "text/plain"));
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setDataHandler(handler);
Log.i("Check", "done sessions");
multiPart = new MimeMultipart();
InternetAddress toAddress;
toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
Log.i("Check", "added recipient");
message.setSubject("Send Auto-Mail");
message.setContent(multiPart);
message.setText("Demo For Sending Mail in Android Automatically \n");
Log.i("check", "transport");
Transport transport = session.getTransport("smtp");
Log.i("check", "connecting");
transport.connect(host, address, pass);
Log.i("check", "wana send");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
Log.i("check", "sent");

}

Monday 16 February 2015

Android Volley or AsyncTask which approach is better?

A new framework was introduced for Android in Google I/O called Android Volley. This new framework was introduced to reduce the complexity involved in fetching data over network. Being Android developers we all know that fetching data over network is one of the most common task that one has to perform in any Android app. Therefore this new Android Volley library could be a game changer in Android app development. This library pitches in a lot of new features, but the question here is, do we need all these features? Here in this sum up, we would discuss the features of Android Volley vs AsyncTask.
Whenever some new component or library is released, being a developer, first thoughts that come into my mind are:
  1. Why should I use it in my current code base?
  2. Would it ease my development?
  3. Would it improve my app’s functionally?
In short, the answer to all these questions would be: Maybe. As its not always possible to adapt to something new in your current code base. Unless you are planning to refactor the code completely. Although if you see things from a different perspective, new is always better. Since android volley is just a framework on top of existing android framework. This framework is utilizing the power of android in best possible way. Therefore it may turn out to be very helpful for developers.

Android Volley or AsyncTask

To start of we will discuss both the approaches individually. First lets begin with AsyncTask as its the older one.

AsyncTask

Usually in android, simply when one need to access a web API, an AsyncTask is used. Its the standard way to do it in android. You may have also observed that AsyncTask is used in many official code samples. Like whenever a new login activity is inserted through theADT in eclipse, it gives you a stub AsyncTask where you are supposed to write the code for accessing the login API.
The basic code structure used to access data through the AsyncTask is:
Main methods used to access the data from network by AsyncTask are:
  1. onPreExecute() – Usually used to start the loading bar, as this method can interact with UI thread.
  2. doInBackground(Void... params) – Main method in AsyncTask implementation where all the network access code is written. This method has a limitation that it cannot interact with UI thread.
  3. onPostExecute(Void aVoid) – This is the method where result from doInBackground is passed as an argument. Since this method can interact with UI thread. It is used to update the screen with results.
A point to note here is that, in the implementation of doInBackground, one would have to write code to access network, and use classes like HttpURLConnection etc. Although if you are trying to access a completely custom implementation of web APIs this may serve your purpose better. Using AsyncTask to fetch data from network is not a bad approach at all, its just that you don’t get out of the box features like volley has.

Android Volley

Moving forward with this Android Volley vs AsyncTask sum up, let’s have an overview of Android volley.
As mentioned volley is a framework on top of android framework. It simply utilizes the power of android to access the network in best possible way. One of the many advantages of volley is that, you don’t need to write code for accessing network. All of this is managed by volley itself. Whenever a new network request is created, a background thread is spawned for network processing. Whose life-cycle is maintained by volley itself. In default implementation of volley, at a time 4 network threads can do processing simultaneously. To learn in detail about volley, please have a look at this Android Volley example.
With Volley you also get many more out of the box features like:
  1. Retry Mechanism
  2. Caching
  3. Multiple Request Types
    1. JsonObjectRequest
    2. JsonArrayRequest
    3. StringRequest
    4. ImageRequest

Difference between Android Volley and AsyncTask

Like I have been saying new is always better. Using AsyncTask has been a good approach, but consider Android Volley as a 2.0 version of it. It has many improvements over AsyncTask, as volley is designed for the purpose of network access. A major advantage of Android Volley over AsyncTask is that you can do multiple requests simultaneously without the overhead of thread management. Also I believe retry mechanism is a great feature of volley which gives it an edge over AsynTask. Another advantage of volley over AsyncTask is that it provides you with multiple request types, through which complex requests can be made easily. On the other hand while using AsyncTasks one would have to create this type of request manually.
Although best approach differs from application to application. Like if you have less no of requests to make, you can use AsyncTask. As for volley, one has to import a library projectwhich increases your project size. Hence pick wisely between volley and AsyncTask. Hope this Android Volley vs AsyncTask sum up helped you choose. Please like our Facebook and Google+ page for more updates.