Posts

Showing posts from 2015

How to fix httpcomponents library in Android Studio

I had a project in eclipse which uses httpcomponents . The jars used in eclipse did not work in Android studio and I am using compileSdkVersion 23 with buildToolsVersion '23.0.2'. I have tried out several solutions and wasted 3 hours figuring out how import the httpcomponents libraries. To avoid wasting your time, here is how I fixed the issue: Changes in Module specific build.gradle i.e. /app/build.gradle Add following under android {...} useLibrary 'org.apache.http.legacy' packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' } Add following under dependencies {..} compile ('org.apache.httpcomponents:httpmime:4.3'){ exclude group: 'org.apache.httpcomponents', module: 'httpclient' } compile ('org.apache.httpcomponents:httpcore:4.4.1'){ exclude group: 'org.apache.htt...

Configuring Gradle

Image
Android studio uses new build system called Gradle. We can use it to build project and configure server urls, settings or replace assets. 1. Following example shows build.gradle file to change server url depending upon the build flavor. I am using two flavors 1. devel 2. prod. For simplicity I shall keep it simple without configuring it for signing. Both flavors define a config variable "HOST" with different value to be used, when apk is built. If developer chooses, to have "devel" build, it will come with "http://192.168.1.34:3000" as HOST. productFlavors { devel { buildConfigField 'String', 'HOST', '"http://192.168.1.34:3000"' } prod { buildConfigField 'String', 'HOST', '"http://api.zuul.com"' } } To access the config constant use following code. String string = BuildConfig.HOST; 2. Following example shows how t...

Restoring activity state after changing configuration

Following are the techniques to save data so that activity can resume from where it had destroyed. 1. onSaveInstanceState() You can save your serializable data in a bundle and android will save this bundle for you. The same bundle will be returned to you on on in onCreate() and onRestoreInstanceState() callback methods. onRestoreInstanceState() is called after onStart(), whereas onCreate() is called before onStart(). These methods are only called when needed, that is when an activity has been destroyed (because of lack of resources) and needs to be recreated by the system. Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed. e.g. @Override protected void onCreate(Bundle savedInstanceState) { ...

Test Android app by installing app wirelessly on device.

I wanted to try out installing the app wirelessly on device. I followed some links given below: 1. http://developer.android.com/tools/help/adb.html 2. http://stackoverflow.com/questions/2604727/how-can-i-connect-to-android-with-adb-over-tcp 3. https://docs.google.com/document/d/1-wMYWpljgHolnVHCsV2VGeMmBcc29Id9rMbHrZoCelE/edit# All of thes mentioned similar steps however none worked for me. Problem arised when following command was executed. d:\sdk\platform-tools>adb tcpip 5555 restarting in TCP mode port: 5555 The command seemed to be stuck and did not finish executing. It took 4-6 hours to figure out the correct procedure by trial and error. Here's the correct procedure. 1. Connect your phone to PC. 2. Open command prompt and go to /sdk/platform-tools/ directory. 3. Now type the following command " adb tcpip 5555". 4. Once you see "restarting ..." message, remove device from PC. This will finish execution of above command. D:\sdkForEclipse...