Merge cherrypicks of [12986203, 12985811] into studio-4.1-release

Change-Id: If35f32983cf3f28f747a15248e962e61d043bbf9
diff --git a/build-system/gradle-core/src/main/java/com/android/build/api/component/impl/ComponentImpl.kt b/build-system/gradle-core/src/main/java/com/android/build/api/component/impl/ComponentImpl.kt
index d6460e2..492ee88 100644
--- a/build-system/gradle-core/src/main/java/com/android/build/api/component/impl/ComponentImpl.kt
+++ b/build-system/gradle-core/src/main/java/com/android/build/api/component/impl/ComponentImpl.kt
@@ -46,4 +46,4 @@
     fun executePropertiesActions(target: PropertiesT) {
         propertiesActions.executeActions(target)
     }
-}
\ No newline at end of file
+}
diff --git a/build-system/gradle-core/src/main/java/com/android/build/api/component/impl/ComponentPropertiesImpl.kt b/build-system/gradle-core/src/main/java/com/android/build/api/component/impl/ComponentPropertiesImpl.kt
index 391654b..e881f42 100644
--- a/build-system/gradle-core/src/main/java/com/android/build/api/component/impl/ComponentPropertiesImpl.kt
+++ b/build-system/gradle-core/src/main/java/com/android/build/api/component/impl/ComponentPropertiesImpl.kt
@@ -502,7 +502,11 @@
                         artifacts.get(COMPILE_AND_RUNTIME_NOT_NAMESPACED_R_CLASS_JAR)
                     )
                 } else {
-                    internalServices.fileCollection(variantScope.rJarForUnitTests)
+                    if (buildFeatures.androidResources) {
+                        internalServices.fileCollection(variantScope.rJarForUnitTests)
+                    } else {
+                        internalServices.fileCollection()
+                    }
                 }
             }
         }
diff --git a/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/TaskManager.java b/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/TaskManager.java
index e01ebe3..8c678e8 100644
--- a/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/TaskManager.java
+++ b/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/TaskManager.java
@@ -1733,7 +1733,8 @@
                                 testConfigInputs.getPackageNameOfFinalRClass());
                     });
         } else {
-            if (testedVariant.getVariantType().isAar()) {
+            if (testedVariant.getVariantType().isAar()
+                    && testedVariant.getBuildFeatures().getAndroidResources()) {
                 // With compile classpath R classes, we need to generate a dummy R class for unit tests
                 // See https://issuetracker.google.com/143762955 for more context.
                 taskFactory.register(
diff --git a/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/factory/AndroidUnitTest.java b/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/factory/AndroidUnitTest.java
index 36f53b7..44978ff 100644
--- a/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/factory/AndroidUnitTest.java
+++ b/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/factory/AndroidUnitTest.java
@@ -195,7 +195,8 @@
                                     RUNTIME_CLASSPATH, ALL, ArtifactType.JAVA_RES));
 
             // 4. The separately compile R class, if applicable.
-            if (!globalScope.getExtension().getAaptOptions().getNamespaced()) {
+            if (creationConfig.getBuildFeatures().getAndroidResources()
+                    && !globalScope.getExtension().getAaptOptions().getNamespaced()) {
                 collection.from(component.getVariantScope().getRJarForUnitTests());
             }
 
diff --git a/build-system/integration-test/application/src/test/java/com/android/build/gradle/integration/resources/DisableLibraryResourcesTest.kt b/build-system/integration-test/application/src/test/java/com/android/build/gradle/integration/resources/DisableLibraryResourcesTest.kt
index 4e27a72..f1e54cf 100644
--- a/build-system/integration-test/application/src/test/java/com/android/build/gradle/integration/resources/DisableLibraryResourcesTest.kt
+++ b/build-system/integration-test/application/src/test/java/com/android/build/gradle/integration/resources/DisableLibraryResourcesTest.kt
@@ -23,6 +23,7 @@
 import com.android.build.gradle.integration.common.truth.TruthHelper
 import com.android.build.gradle.integration.common.utils.TestFileUtils
 import com.android.build.gradle.options.BooleanOption
+import com.android.testutils.truth.PathSubject
 import com.google.common.truth.Truth.assertThat
 import org.junit.Rule
 import org.junit.Test
@@ -45,6 +46,27 @@
                 </resources>""".trimIndent()
         )
         .withFile("src/main/res/raw/raw_file", "leafLib")
+        .withFile("src/test/java/com/example/MyTest.java",
+            //language=java
+            """
+                package com.example;
+
+                import org.junit.Test;
+
+                public class MyTest {
+                    @Test
+                    public void check() {
+                        System.out.println("ExampleTest has some output");
+                    }
+                }
+                """.trimIndent())
+        .appendToBuild("""
+
+            dependencies {
+                testImplementation("junit:junit:4.12")
+            }
+
+        """.trimIndent())
 
     private val localLib = MinimalSubProject.lib("com.example.localLib")
         .withFile(
@@ -195,4 +217,14 @@
         assertThat(result.didWorkTasks).contains(":localLib:parseDebugLocalResources")
         assertThat(result.didWorkTasks).contains(":leafLib:parseDebugLocalResources")
     }
-}
\ No newline at end of file
+
+    @Test
+    fun testAndroidAndUnitTests() {
+        project.executor().with(BooleanOption.BUILD_FEATURE_ANDROID_RESOURCES, false)
+            .run(":leaflib:assembleDebugAndroidTest", ":leaflib:test")
+        assertThat(project.file("leafLib/build/reports/tests/testReleaseUnitTest/classes/com.example.MyTest.html").readText())
+            .contains("ExampleTest has some output")
+        assertThat(project.file("leafLib/build/reports/tests/testDebugUnitTest/classes/com.example.MyTest.html").readText())
+            .contains("ExampleTest has some output")
+    }
+}