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 24 November 2014

Setting up GPE and running an AppEngine Connected Android Project on Emulators and Devices

This is a simple post that provides a quick walk-through to setup Google Plugin for Eclipse (GPE) and then running  the sample App Engine connected android project on the real devices and on emulators.

Actually this post is more about getting the project running on the android device and emulators rather than configuration of Google Plugin for Eclipse because there will not be any changes in the steps to configure it from the official documentation.

Apart from setting up GPE, there is one more important configuration step that is required to carry on with the a project i.e creating project entry in the Google Api console.

So now we will go through in steps to setup GPE to create an App Engine Connected android project.


1. Setting Up GPE :

    Simplest way to setup the plugin is from this link
    https://developers.google.com/eclipse/docs/updating_the_plugin

    Choose the plugin download and install link based upon the version running on your system ie. out of the following supported eclipse versions
        a. Google Plugin for Eclipse 4.3 (Kepler).
        b. Google Plugin for Eclipse 4.2 (Juno).   
        c. Google Plugin for Eclipse 3.7 (Indigo).


2. Create Project on API Console

    Apart from setting up GPE, there is one more important configuration step that is required to carry on with the a project i.e creating project entry in the Google Api console(https://code.google.com/apis/console).

    As project is created you will need to create a server key by navigating to API's and Auth menu under your project. Then choose Credentials sub-menu where you can find an option to create keys. Click on it and choose Server key to proceed which will then create a server key. 

Current sample requirement : You will need to enable Google Cloud Messaging for Android service from the API's section as it is a part of our sample app.
      
    Keep an account of both the project number and this created server key which will be used when you will begin with creation of project in eclipse.


3. Create New Project in Eclipse
    Create a new app engine connected android project in eclipse by choosing
    File -> New Project -> Android -> App Engine Connected Android Project .
    https://developers.google.com/eclipse/docs/endpoints-androidconnected-gae

    Note :
    Since here the project created from the New Project wizard is an android project which has an app engine backend.

    The sample Android app registers with the backend via an Endpoints API and receives messages from the backend via Google Cloud Messaging for Android. The sample app consists of required boilerplate code along with sample code that can be easily replaced by your own app code.

    The App Engine backend provides an API to register Android clients and a Web UI to broadcast messages to clients.

 Above wizard creates two projects in the workspace, one is android client and other a web application (Backend app) which provides an api for the clients.

Please note that we will not be covering how Cloud EndPoints api is used to register with the backends.

Now you can see that there are two projects in the workspace. Clean and build the complete workspace or you can simply do for the two projects.

The major tasks that we will be covering in this project are :
1. Running app engine server project on local machine. You can also deploy this application on cloud and  then make use of it. In our case we will be running it on our local machine.

2. Running android client both on android devices and emulators.

Once done with the build process successfully, you are good to go with the launch of the android app and app engine based project.

Here we go with the required changes in the two projects to get them running on the real android device and emulators.

Case 1: Using emulators to run android client.
        Web Project :
         Clean and build the web project and run it as web application.

        Android Project :
        In CloudEndpointUtils.java file update the value of constants :
                    -> LOCAL_ANDROID_RUN = true;
      
Clean and build the android project and run it as web application.



Case 2: Using android devices to run android client
          Web Project :
         a. Update the arguments value under Run as -> Run Configurations -> WebApplication ->   Select the Project -> Select Arguments -> Add this ahead --address=0.0.0.0 ahead of  --port=8.8.8.8
                and final value will something like
                --address=0.0.0.0 --port=8888 F:\WorkSpaceDroid\PlayCloudEPoints-AppEngine\war
          b.
Clean, build and run the project again

         Android Project :      
       
In CloudEndpointUtils.java file update the value of constants :
                    -> LOCAL_ANDROID_RUN = true;
                    -> LOCAL_APP_ENGINE_SERVER_URL_FOR_ANDROID ="your_machine_ipv4_address:8888";



Please add comments if you face any issues.