Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AndroidManifest.xml not found when using with Android Gradle plugin 1.1.0 #142

Closed
denisk20 opened this issue Mar 7, 2015 · 13 comments
Closed

Comments

@denisk20
Copy link

denisk20 commented Mar 7, 2015

Hello,
I've been successfully using Robolectric with gradle with Android Gradle plugin 1.0.0 without robolectric plugin. However, after updating to 1.1.0 my tests are unable to find the manifest:

WARNING: No manifest file found at ./AndroidManifest.xml. Falling back to the Android OS resources only.

Interesting thing that using both @Config and org.robolectric.Config.properties seem to be ineffective in solving it.
After incorporating your plugin into the project (with 1.1.0 plugin activated) the behavior of my test diverged. In the Android Studio I still get the same errors related to AndroidManifest.xml. When I run tests from console however I get android.content.res.Resources$NotFoundException.
I have 2 build types and 2 flavors. Tests are located under src/test. The manifest is under src/main. I have also tried to add a copy of the manifest just next to my test class, but to no avail.

I know that these issues are only partly related to the robolectric plugin itself, but would still appreciate any insights.

@erd
Copy link
Member

erd commented Mar 7, 2015

The plugin does not work with Android Studio due to how studio runs tests.

With version 1.0.1 of the plugin, you shouldn't have to use @config, Config.properties or any other configuration to run your tests on the command line.

@denisk20
Copy link
Author

denisk20 commented Mar 7, 2015

@erd Thanks for the clarification! Is there a chance that the plugin will work with Studio 1.2 (which is going to be based on IntelliJ 14) ?

@nenick
Copy link

nenick commented Mar 8, 2015

This plugin works for me with AS when using custom RobolectricTestRunner and provide the missing system properties.

public class CustomRobolectricTestRunner extends RobolectricTestRunner {

  public CustomRobolectricTestRunner(Class<?> testClass) throws InitializationError {
    super(testClass);
    String buildVariant = BuildConfig.BUILD_TYPE + (BuildConfig.FLAVOR.isEmpty()? "" : "/" + BuildConfig.FLAVOR);
    System.setProperty("android.package", BuildConfig.APPLICATION_ID);
    System.setProperty("android.manifest", "build/intermediates/manifests/full/" + buildVariant + "/AndroidManifest.xml");
    System.setProperty("android.resources", "build/intermediates/res/" + buildVariant);
    System.setProperty("android.assets", "build/intermediates/assets/" + buildVariant);
  }
}

Edit:
You must also use a different working directory at your unit test run configuration. Best is to change the default configuration and use $MODULE_DIR$ https://code.google.com/p/android/issues/detail?id=158015#c5

@denisk20
Copy link
Author

denisk20 commented Mar 8, 2015

@nenick Thanks, it works this way, just needed so swap build type and build flavor for build variant. Also checked your examples from https://github.com/nenick/AndroidStudioAndRobolectric, very useful.

@nealsanche
Copy link

@nenick Thanks, I also had to swap the build type and flavor, so my variant ended up looking like this:

import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricTestRunner;

public class CustomRobolectricTestRunner extends RobolectricTestRunner {

    public CustomRobolectricTestRunner(Class<?> testClass) throws InitializationError {
        super(testClass);
        String buildVariant = (BuildConfig.FLAVOR.isEmpty() ? "" : BuildConfig.FLAVOR + "/") + BuildConfig.BUILD_TYPE;
        System.setProperty("android.package", BuildConfig.APPLICATION_ID);
        System.setProperty("android.manifest", "build/intermediates/manifests/full/" + buildVariant + "/AndroidManifest.xml");
        System.setProperty("android.resources", "build/intermediates/res/" + buildVariant);
        System.setProperty("android.assets", "build/intermediates/assets/" + buildVariant);
    }
}

@ypresto
Copy link

ypresto commented Jun 16, 2015

We have build flavors with different applicationIds (such like "com.example" and "com.example.development").
We also had to change from BuildConfig.APPLICATION_ID to default application id.

https://github.com/robolectric/robolectric-gradle-plugin/blob/master/src/main/groovy/org/robolectric/gradle/RobolectricPlugin.groovy#L24

public class CustomRobolectricTestRunner extends RobolectricTestRunner {
    public CustomRoblectricTestRunner(Class<?> testClass) throws InitializationError {
        super(testClass);
        String buildVariant = (BuildConfig.FLAVOR.isEmpty() ? "" : BuildConfig.FLAVOR + "/") + BuildConfig.BUILD_TYPE;
        System.setProperty("android.package", BuildConfig.DEFAULT_APPLICATION_ID);
        System.setProperty("android.manifest", "build/intermediates/manifests/full/" + buildVariant + "/AndroidManifest.xml");
        System.setProperty("android.resources", "build/intermediates/res/" + buildVariant);
        System.setProperty("android.assets", "build/intermediates/assets/" + buildVariant);
    }
}

build.gradle:

def defaultApplicationId = "com.example"

android {
        applicationId defaultApplicationId
        buildConfigField "String", "DEFAULT_APPLICATION_ID", "\"${defaultApplicationId}\"" // for robolectric
        ...
}

(EDITED: at least with android gradle plugin 1.5.0, "build/intermediates/res/" must be "build/intermediates/res/merged/")

@ypresto
Copy link

ypresto commented Jun 16, 2015

Above test runner works well on both Android Studio unit test and Gradle test even without robolectric-gradle-plugin. (Also setting $MODULE_DIR$ #142 (comment))

@DanielGrech
Copy link

For me the problem was because my class package (com.my.class.package) was different from my applicationId (com.my.app)

I fixed by adding packageName = "com.my.class.package" to my @Config

@xtien
Copy link

xtien commented Jun 24, 2015

I had to add my build variant "app" to the string: "app/build/.... "

@davidgoli
Copy link

The current correct solution is to use the RobolectricGradleTestRunner like so:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants=BuildConfig.class, sdk=21) // robolectric currently only supports up to API 21, sadly
public class CustomTest {
...

@chetbox
Copy link

chetbox commented Jan 23, 2017

I struggled with this for a while but eventually came up with a RobolectricTestRunner implementation that works for our setup:

  • We have two flavours, release for production and debug for internal testing. The latter is what we test against.
  • The debug flavour has an applicationIdSuffix of .debug
public class RobolectricTestRunnerWithResources extends RobolectricTestRunner {

    @Override
    protected Config buildGlobalConfig() {
        return Config.Builder.defaults()
                .setPackageName("co.beeline")
                .setManifest("build/intermediates/manifests/full/debug/AndroidManifest.xml")
                .setResourceDir("../../../res/merged/debug") // relative to manifest
                .setAssetDir("../../../assets/debug") // relative to manifest
                .build();
    }

    public RobolectricTestRunnerWithResources(Class<?> testClass) throws InitializationError {
        super(testClass);
    }
}

@stanmots
Copy link

stanmots commented Mar 6, 2017

@chetbox How did you manage to set the manifest correctly? For me it just appends the given string to the default prefix so that the resulting path is

build/intermediates/bundles/debug/build/intermediates/manifests/full/debug/AndroidManifest.xml

@chetbox
Copy link

chetbox commented Mar 6, 2017

This works for us. It might be that a different version of the Android SDK or Robolectric behave differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants