1 /* 2 * Copyright (C) 2020 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import java.io.File 18 import org.gradle.testkit.runner.TaskOutcome 19 import org.junit.Assert.assertEquals 20 import org.junit.Before 21 import org.junit.Rule 22 import org.junit.Test 23 import org.junit.rules.TemporaryFolder 24 25 class CompileClasspathTest { 26 @get:Rule 27 val testProjectDir = TemporaryFolder() 28 29 lateinit var gradleRunner: GradleTestRunner 30 31 @Before setupnull32 fun setup() { 33 gradleRunner = GradleTestRunner(testProjectDir) 34 gradleRunner.addAndroidOption( 35 "lintOptions.checkReleaseBuilds = false" 36 ) 37 gradleRunner.addHiltOption( 38 "enableExperimentalClasspathAggregation = true" 39 ) 40 gradleRunner.addDependencies( 41 "implementation 'androidx.appcompat:appcompat:1.1.0'", 42 "implementation 'com.google.dagger:hilt-android:LOCAL-SNAPSHOT'", 43 "annotationProcessor 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT'", 44 "implementation project(':libraryA')", 45 ) 46 gradleRunner.addSrc( 47 srcPath = "minimal/MyApp.java", 48 srcContent = 49 """ 50 package minimal; 51 52 import android.app.Application; 53 import liba.LibraryA; 54 55 @dagger.hilt.android.HiltAndroidApp 56 public class MyApp extends Application { 57 @javax.inject.Inject 58 LibraryA libraryA; 59 } 60 """.trimIndent() 61 ) 62 gradleRunner.setAppClassName(".MyApp") 63 } 64 65 // Verifies that library B and library C injected classes are available in the root classpath. 66 @Test test_injectClassesnull67 fun test_injectClasses() { 68 File("src/test/data/android-libraryA") 69 .copyRecursively(File(testProjectDir.root, "libraryA")) 70 File("src/test/data/java-libraryB") 71 .copyRecursively(File(testProjectDir.root, "libraryB")) 72 File("src/test/data/android-libraryC") 73 .copyRecursively(File(testProjectDir.root, "libraryC")) 74 75 testProjectDir.newFile("settings.gradle").apply { 76 writeText( 77 """ 78 include ':libraryA' 79 include ':libraryB' 80 include ':libraryC' 81 """.trimIndent() 82 ) 83 } 84 85 val result = gradleRunner.build() 86 val assembleTask = result.getTask(":assembleDebug") 87 assertEquals(TaskOutcome.SUCCESS, assembleTask.outcome) 88 } 89 90 // Verifies that library B Hilt module is available in the root classpath. 91 @Test test_injectClassesFromModulesnull92 fun test_injectClassesFromModules() { 93 File("src/test/data/java-libraryA") 94 .copyRecursively(File(testProjectDir.root, "libraryA")) 95 File("src/test/data/java-libraryB") 96 .copyRecursively(File(testProjectDir.root, "libraryB")) 97 98 testProjectDir.newFile("settings.gradle").apply { 99 writeText( 100 """ 101 include ':libraryA' 102 include ':libraryB' 103 """.trimIndent() 104 ) 105 } 106 107 val result = gradleRunner.build() 108 val assembleTask = result.getTask(":assembleDebug") 109 assertEquals(TaskOutcome.SUCCESS, assembleTask.outcome) 110 } 111 } 112