Robolectric Resources$NotFoundException can’t find library (aar) resources

I have looked quite a bit for a solution for this (few days) and have not come to a solution yet. Basically, for my unit tests using Robolectric, I keep getting Resources$NotFoundException when trying to inflate resources from a dependency (pulled in as an aar). Heres the setup:

build.gradle (project level)

// Top Level
buildscript {
    apply from: "${System.getenv('MY_REPOS')}", to: buildscript
    println("REPOS: ${repositories.names}")
    dependencies {
        classpath ""

allprojects {
    repositories {

task clean(type: Delete) {
    delete rootProject.buildDir

build.gradle (app)

plugins {
    id ''

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId ""
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    testOptions {
        animationsDisabled true
        unitTests {
            includeAndroidResources = true

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), ''
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8

dependencies {

    implementation '' // pulled from private repo

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation ''
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.2.2'
    implementation 'androidx.navigation:navigation-ui:2.2.2'

    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation("") {
        exclude group: '', module: 'failureaccess'

    testImplementation 'junit:junit:4.13'
    testImplementation 'org.robolectric:robolectric:4.3.1'
    testImplementation 'org.robolectric:android-all:9-robolectric-4913185-2'

I am using distributionUrl= in my

and my gradle properties:

org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.enableUnitTestBinaryResources = true //tried with and without this set
android.enableAapt2 = false // same here with and without this here

Then its the default blank activity template that comes in Android Studio 4.1.2. So the only thing touched is the main main activity:


import android.os.Bundle;


import androidx.appcompat.widget.Toolbar;

import android.view.View;

import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        Toolbar toolbar = findViewById(;

        FloatingActionButton fab = findViewById(;
        fab.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

    public void testDialogSetup() {
        MyOtherView myView = new MyOtherView(this);

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == {
            return true;

        return super.onOptionsItemSelected(item);

So the issue is with the MyOtherView and when it inflates the view inside this class. So in MyOtherView it throws the exception in the test when it hits this line:

inflater.inflate(layout.my_view, this);

As for the test itself:


import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import static org.junit.Assert.*;

 * Example local unit test, which will execute on the development machine (host).
 * @see <a href="">Testing documentation</a>
public class ExampleUnitTest {

    MainActivity activity;

    public void setup() {
        this.activity = Robolectric.buildActivity(MainActivity.class).get();

    public void simpleTest() {
        activity.testDialogSetup(); // not doing anything yet since it throws the error here

I’ve tried invalidating/restarting, clearing cache, I’ve tried different versions of gradle build tools and with the distribution. Everything I read shows to just have the includeAndroidResources flag set to true but I have that, and I have already tried with/without enableUnitTestBinaryResources as well as enableAapt2

ANY help is appreciated since I have been killing myself trying to get this working. I would think its something simple but… I’m lost here. Its like it can’t see the dependency (, MyOtherView) resources at all.


Alright all, I figured it out (actually a colleague did). I added the file to the src/test/resources area. in the file I had to put:


I hope this helps someone down the road.

Source: stackoverflow