Getting “Can't find symbol” for UI elements in TestCases after upgrading to Gradle 5.6.4

We are upgrading Gradle for our android app. Following things we have upgraded

  1. gradle-4.10.1-all => gradle-5.6.4-all
  2. com.android.tools.build:gradle:3.3.2 => com.android.tools.build:gradle:3.6.4

With that, few other changes have been made in build.gradle

We have a Espresso Test Folder in /app/src folder, named androidIntegrationTest

androidIntegrationTest >> java >> com.xyz.abc >> TestClasses

We have registered androidIntegrationTest in the build.gradle as below (src/androidTest/java is unit test folder)

androidTest {
    if (project.hasProperty("espressoflag")) {
        java.srcDirs = ['src/androidIntegrationTest/java']
        resources.srcDirs = ['src/androidIntegrationTest/res']
        assets.srcDirs = ['src/androidIntegrationTest/assets']
    } else if (project.hasProperty("functionalflag")) {
        java.srcDirs = ['src/androidTest/java']
    } else {
        java.srcDirs = ['src/androidIntegrationTest/java', 'src/androidTest/java']
        resources.srcDirs = ['src/androidIntegrationTest/res']
        assets.srcDirs = ['src/androidIntegrationTest/assets']
    }
}

With the above setup, we are able to build every type of apk, except TestAPKs because we were getting error of Can’t find Symbol for added UI elements(R.id.anyElement).

So to check, we removed all the Test classes, added a SampleTestClass file with a SampleTest without any UI elements, and tried to build the testAPK. And we were able to create the TestAPK!!

So the only issue, that we see is related to R files. We are getting “Can’t find symbol” on Gradle Upgrade.

Any help would be appreciated.

Note: put comments if you need more information, will edit the question.