Tuesday, December 24, 2013

Should AndroidManifest.xml for instrumentTest be customizable?

I've been trying to test authenticated GAE endpoints deployed on local Dev Server.  Authenticated endpoints require dependency on Google Play services.  I added it to my build.gradle:
dependencies

{
    // ...
    instrumentTestCompile 'com.google.android.gms:play-services:4.0.30'
    // ...
}

Now, the following error occurs when running instrumentation tests:

java.lang.IllegalStateException: A required meta-data tag in your app's AndroidManifest.xml does not exist.
You must have the following declaration within the <application/> element:
  <meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version"/>

There seems to be no way to add the required <meta-data/> element to AndoidManifest.xml file in instrumentTest because the file is not customizable.  Just read this documentation:

The [src/instrumentTest] sourceSet should not contain an AndroidManifest.xml as it is automatically generated.

This is a big problem.  Instrumentation tests can have metadata, permissions, activities, etc, that are different from the application.  In fact, there is a Stackoverflow post that asks exactly this.

It is really surprising that this has not been brought yet to attention of the development team that works on Gradle build system for Android Studio.  This is a deal breaker.  I hope there is a workaround that I am simply missing...

Update

After adding a dependency on com.google.android.gms:play-services:4.0.30 to build.gradle, I started getting IllegalStateException described earlier.   Since Project-endpoints/src/instrumentTest/AndroidManifest.xml is not customizable, I added the required <meta-data/> tag to Project-endpoints/src/main/AndroidManifest.xml.  When I re-ran the test, I got the same error.
Fortunately, the error was easy to resolve: all that was required is to clean the project.
  • From command line: gradlew clean
  • From Android Studio: Build -> Clean Project or Build -> Rebuild Project
When gradle build system generates AndroidManifest.xml for Project-endpoints/src/instrumentTest, it takes into account Project-endpoints/src/main/AndroidManifest.xml.  All that is required is to clean the project before re-running the tests.  I haven't seen any documentation on what exactly goes from the main/AndroidManifest.xml to instrumentTest/AndroidManifest.xml, but at least <meta-data/> tags seem to be picked up.

No comments:

Post a Comment