Showing posts with label Gradle. Show all posts
Showing posts with label Gradle. Show all posts

Thursday, 27 November 2014

Android project import error for source-set with updated gradle tools


This blog post is about a very frequent error occurring with the updated version of gradle tools in Android Studio and its resolution.


This issue mainly occurs while importing an android project in android studio with updated gradle tools.

When we create a gradle based project in android studio, it generates the project with a defined structure, so that build.gradle can access all the source sets while building project. 

In other words, a gradle based android project  when created in the Android Studio IDE represents a particular hierarchy in which various components(various modules, library modules, directories containing generated code, gradle system specific directories and files) of a particular project is defined.

Or more precisely we are talking about the SourceSets.

SourceSet
A source-set in a gradle build system is a logical grouping of sources and resources and that confirms/corresponds to a hierarchy when a new project is created in android IDE.

As long as the source-set is defined according to the default structure, it will never complain for the build failed due to missing source sets.

Well this logical grouping could be changed as per the development requirements but then it would require some changes in the build.gradle to provide the proper path for the source sets required for proper build. An example of modified SourceSets is as follows :


Now if a project was created with updated source-sets using the older gradle (0.9.1) the only change in build.gradle that got it working was :


android {

  sourceSets {
   main {
    manifest.srcFile 'AndroidManifest.xml'
    res.srcDirs = ['res']
    assets.srcDirs = ['assets']
    java.srcDirs = ['src']
   }

  .........
  }
  
  .........
}


Project use to get imported successfully and so did the build process.

But the same project when was imported into studio with the updated gradle (0.13.2 [at time of writing]) was failing with the following error for the source-set paths. To get the project imported and build successfully we were required to provide source-set path accurately.

android {

   sourceSets {
    main {
     // Complete path to the source was required here.
     
     manifest.srcFile 'AndroidManifest.xml'
     java.srcDirs = ['src/main/java']
     assets.srcDirs = ['assets']
     res.srcDirs = ['res']
    }
  
  .........
  }
  
  ..........
}

Just this change got the project imported and later build successfully. 

References :
http://www.gradle.org/docs/current/dsl/index.html

Monday, 27 October 2014

Gradle Configuration for an Android Studio project with Android 5.0 or L or Lollipop

This post is only about configuring an android project in Android Studio with compile and target versions set as  android L or 5.0 or Lollipop. 

    I am writing this post with the sample gradle as I faced a lot of problems while configuring Lollipop's preview version.
    I will write the sample gradle as well as the screen shot of android sdk manager to represent most basic requirements required for a project setup with target version as Lollipop.
   
    So to be able to use any feature of the Android 5.0, here is the sample gradle with support library for Android L features :



apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.0.1"

    defaultConfig {
        applicationId "your app's packagename"
        minSdkVersion 10
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.+'
 // Not compulsory just added 
    compile 'com.google.android.gms:play-services:+'
 // Support for L features for devices with lower OS versions
    compile 'com.android.support:cardview-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'
}

Also see the following attached screenshots to get the required SDK, various support dependencies, platforms etc to be downloaded :

Android Tools :


Android SDK 5.0(L or Lollipop)

 Minimum Android SDK (as per above gradle 2.3.3 or API level 10)

Support Libraries for L features for older devices

Please provide your comment in case of any issues.