forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use runtime classpath at root to workaround Dagger/Hilt API vs Impl i…
…ssue. This CL adds a new Hilt option called 'enableClasspathAggregation' that will configure the compile classpath of the project (for app modules and tests) to use the runtime classpath. This means that transitive dependencies will be available during compilation which in turn will allow Dagger to traverse the classes along the dependency tree and will allow for Hilt's aggregating classes to be discovered. The classpath configuration is done by resolving the runtime configuration with an artifact view and adding it to the 'CompileOnly' config. This solution is inefficient and will cause build performance impact, but it is a starting point that can be further optimized by using a smarter transform that can extract the necessary classes required by Dagger and Hilt. Fixes: #1991 RELNOTES=Use runtime classpath at root to workaround Dagger/Hilt API vs Impl issue. PiperOrigin-RevId: 350239023
- Loading branch information
1 parent
5959306
commit 239768b
Showing
44 changed files
with
1,399 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...gger/hilt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/util/CopyTransform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package dagger.hilt.android.plugin.util | ||
|
||
import org.gradle.api.artifacts.transform.CacheableTransform | ||
import org.gradle.api.artifacts.transform.InputArtifact | ||
import org.gradle.api.artifacts.transform.TransformAction | ||
import org.gradle.api.artifacts.transform.TransformOutputs | ||
import org.gradle.api.artifacts.transform.TransformParameters | ||
import org.gradle.api.file.FileSystemLocation | ||
import org.gradle.api.provider.Provider | ||
import org.gradle.api.tasks.Classpath | ||
|
||
// A transform that registers the input jar file as an output and thus changing from one artifact | ||
// type to another. | ||
// TODO: Improve to only copy classes that need to be visible by Hilt & Dagger. | ||
@CacheableTransform | ||
abstract class CopyTransform : TransformAction<TransformParameters.None> { | ||
@get:Classpath | ||
@get:InputArtifact | ||
abstract val inputArtifactProvider: Provider<FileSystemLocation> | ||
|
||
override fun transform(outputs: TransformOutputs) { | ||
val input = inputArtifactProvider.get().asFile | ||
when { | ||
input.isDirectory -> outputs.dir(input) | ||
input.isFile -> outputs.file(input) | ||
else -> error("File/directory does not exist: ${input.absolutePath}") | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
java/dagger/hilt/android/plugin/src/test/data/android-libraryA/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
plugins { | ||
id 'com.android.library' | ||
id 'dagger.hilt.android.plugin' | ||
} | ||
|
||
android { | ||
compileSdkVersion 30 | ||
buildToolsVersion "30.0.2" | ||
|
||
defaultConfig { | ||
minSdkVersion 21 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
compileOptions { | ||
sourceCompatibility 1.8 | ||
targetCompatibility 1.8 | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation 'com.google.dagger:hilt-android:LOCAL-SNAPSHOT' | ||
annotationProcessor 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT' | ||
|
||
implementation project(':libraryB') | ||
implementation project(':libraryC') | ||
} |
4 changes: 4 additions & 0 deletions
4
java/dagger/hilt/android/plugin/src/test/data/android-libraryA/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="liba"> | ||
</manifest> |
27 changes: 27 additions & 0 deletions
27
...agger/hilt/android/plugin/src/test/data/android-libraryA/src/main/java/liba/LibraryA.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (C) 2020 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package liba; | ||
|
||
import javax.inject.Inject; | ||
import libb.LibraryB; | ||
import libc.LibraryC; | ||
|
||
/** Test LibA */ | ||
public class LibraryA { | ||
@Inject | ||
public LibraryA(LibraryB b, LibraryC c) {} | ||
} |
25 changes: 25 additions & 0 deletions
25
java/dagger/hilt/android/plugin/src/test/data/android-libraryC/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
plugins { | ||
id 'com.android.library' | ||
id 'dagger.hilt.android.plugin' | ||
} | ||
|
||
android { | ||
compileSdkVersion 30 | ||
buildToolsVersion "30.0.2" | ||
|
||
defaultConfig { | ||
minSdkVersion 21 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
compileOptions { | ||
sourceCompatibility 1.8 | ||
targetCompatibility 1.8 | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation 'com.google.dagger:hilt-android:LOCAL-SNAPSHOT' | ||
annotationProcessor 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT' | ||
} |
4 changes: 4 additions & 0 deletions
4
java/dagger/hilt/android/plugin/src/test/data/android-libraryC/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="libc"> | ||
</manifest> |
25 changes: 25 additions & 0 deletions
25
...agger/hilt/android/plugin/src/test/data/android-libraryC/src/main/java/libc/LibraryC.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (C) 2020 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package libc; | ||
|
||
import javax.inject.Inject; | ||
|
||
/** Test LibC */ | ||
public class LibraryC { | ||
@Inject | ||
public LibraryC() {} | ||
} |
15 changes: 15 additions & 0 deletions
15
java/dagger/hilt/android/plugin/src/test/data/java-libraryA/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
plugins { | ||
id 'java-library' | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
dependencies { | ||
implementation 'com.google.dagger:hilt-core:LOCAL-SNAPSHOT' | ||
annotationProcessor 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT' | ||
|
||
implementation project(':libraryB') | ||
} |
Oops, something went wrong.