Showing posts with label gradle. Show all posts
Showing posts with label gradle. Show all posts

Saturday, December 26, 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.httpcomponents', module: 'httpclient'
}
compile 'commons-io:commons-io:1.3.2'

In Top level build.gradle - /build.gradle add the following by replacing earlier statement:

buildscript {
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
    }
}

Now gradle sync your project and its done.

Friday, November 13, 2015

Configuring Gradle

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 to replace java or asset files depending upon the product flavor.
As mentioned earlier, project uses two flavors namely "devel" and "prod". Our objective is to replace Constants.java and "ic_launcher.png". Each flavor will have different files. Prepare folder structure as shown:

Copy "ic_launcher.png" file in "devel.res.drawable". Put Constants.java file mentioned below in "devel.java.com.poc.gradleconfiguration".


package com.poc.gradleconfiguration;

public class Constants {

    public static String NAME = "This is development.";
}

Copy "ic_launcher.png" file in "prod.res.drawable". Put Constants.java file mentioned below in "prod.java.com.poc.gradleconfiguration".
package com.poc.gradleconfiguration;

public class Constants {

    public static String NAME = "This is prod.";
}
Don't put Constants.java file mentioned below in "main.java.com.poc.gradleconfiguration", as gradle will copy one of the above files here.
To use the constant use the following code.
String name = Constants.NAME;
Now simply select type build you want to make from build variants view and gradle will make sure you have correct copy of the Constants.java.

Automate Library Integration with Cursor's Agent Mode

Automate Android Library Integration with Cursor's Agent Mode Automate Android Library Integration with Cursor...