R file not generated for androidTests when using AGP 4.2.0 or 7.0

Viewed 330

I have tried migrating to a new Gradle Plugin, but since we have some custom layouts for AndroidTests, it fails with an error saying "R class was not found" for the android tests. I could not find any source saying about any behaviour changes in this area.

The error looks like this and seems to be related to view inding or simply to the fact the R class is not generated.

/pathToTheApp/build/generated/data_binding_base_class_source_out/debugAndroidTest/out/com/package1/test/databinding/CustomViewActivityBinding.java:12: error: cannot find symbol
import package.name.test.R;
                             ^
  symbol:   class R
  location: package package.name.test
1 Answers

I had this problem in our Android tests after upgrading to AGP 7.0. Our test R class could not be found. I managed to fix it by duplicating the testApplicationId into the seemingly new testNamespace field.

android {

    testNamespace = "com.example.tests"

    defaultConfig {
        testApplicationId "com.example.tests"
    }

}

I think it should work without it, so maybe this is a plugin bug, but the docs say:

The namespace used by the android test and unit test components for the generated R and BuildConfig classes

Related