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

No comments:

Post a Comment