Skip to content

Commit

Permalink
Fix FileNotFoundException in AGP 8.3
Browse files Browse the repository at this point in the history
RELNOTES=N/A
PiperOrigin-RevId: 579237165
  • Loading branch information
kuanyingchou authored and Dagger Team committed Nov 3, 2023
1 parent 8801fd2 commit 6018cd2
Show file tree
Hide file tree
Showing 30 changed files with 209 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/actions/test-gradle-plugin/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ description: 'Tests the Hilt Gradle plugin.'
runs:
using: "composite"
steps:
- name: 'Install Java ${{ env.USE_JAVA_VERSION }}'
- name: 'Install Java ${{ env.USE_JAVA_VERSION_FOR_PLUGIN }}'
uses: actions/setup-java@v3
with:
distribution: '${{ env.USE_JAVA_DISTRIBUTION }}'
java-version: '${{ env.USE_JAVA_VERSION }}'
java-version: '${{ env.USE_JAVA_VERSION_FOR_PLUGIN }}'
- name: 'Check out repository'
uses: actions/checkout@v3
- name: 'Cache local Maven repository'
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ on:
env:
USE_JAVA_DISTRIBUTION: 'zulu'
USE_JAVA_VERSION: '11'
# This is required by AGP 8.3+.
USE_JAVA_VERSION_FOR_PLUGIN: '17'
# Our Bazel builds currently rely on 6.3.2. The version is set via
# baselisk by USE_BAZEL_VERSION: https://github.com/bazelbuild/bazelisk.
USE_BAZEL_VERSION: '6.3.2'
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ on:
env:
USE_JAVA_DISTRIBUTION: 'zulu'
USE_JAVA_VERSION: '11'
# This is required by AGP 8.3+.
USE_JAVA_VERSION_FOR_PLUGIN: '17'
# Our Bazel builds currently rely on 6.3.2. The version is set via
# baselisk by USE_BAZEL_VERSION: https://github.com/bazelbuild/bazelisk.
USE_BAZEL_VERSION: '6.3.2'
Expand Down
6 changes: 4 additions & 2 deletions java/dagger/hilt/android/plugin/agp-wrapper-7-0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ plugins {
id 'org.jetbrains.kotlin.jvm'
}

kotlin {
jvmToolchain(11)
compileKotlin {
kotlinOptions {
jvmTarget = 11
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import com.android.build.api.instrumentation.FramesComputationMode
import com.android.build.api.instrumentation.InstrumentationParameters
import com.android.build.api.instrumentation.InstrumentationScope
import com.android.build.api.variant.Component
import java.io.File
import org.gradle.api.Project

internal class ComponentCompatApi70Impl(private val component: Component) : ComponentCompat() {

Expand All @@ -40,4 +42,7 @@ internal class ComponentCompatApi70Impl(private val component: Component) : Comp
override fun setAsmFramesComputationMode(mode: FramesComputationMode) {
component.setAsmFramesComputationMode(mode)
}

override fun getJavaCompileClassesDir(project: Project): File =
project.layout.buildDirectory.dir("intermediates/javac/${component.name}/classes").get().asFile
}
6 changes: 4 additions & 2 deletions java/dagger/hilt/android/plugin/agp-wrapper-7-1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ plugins {
id 'org.jetbrains.kotlin.jvm'
}

kotlin {
jvmToolchain(11)
compileKotlin {
kotlinOptions {
jvmTarget = 11
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import com.android.build.api.instrumentation.FramesComputationMode
import com.android.build.api.instrumentation.InstrumentationParameters
import com.android.build.api.instrumentation.InstrumentationScope
import com.android.build.api.variant.Component
import java.io.File
import org.gradle.api.Project

internal class ComponentCompatApi71Impl(private val component: Component) : ComponentCompat() {

Expand All @@ -38,4 +40,7 @@ internal class ComponentCompatApi71Impl(private val component: Component) : Comp
override fun setAsmFramesComputationMode(mode: FramesComputationMode) {
component.setAsmFramesComputationMode(mode)
}

override fun getJavaCompileClassesDir(project: Project): File =
project.layout.buildDirectory.dir("intermediates/javac/${component.name}/classes").get().asFile
}
6 changes: 4 additions & 2 deletions java/dagger/hilt/android/plugin/agp-wrapper-7-2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ plugins {
id 'org.jetbrains.kotlin.jvm'
}

kotlin {
jvmToolchain(11)
compileKotlin {
kotlinOptions {
jvmTarget = 11
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import com.android.build.api.instrumentation.FramesComputationMode
import com.android.build.api.instrumentation.InstrumentationParameters
import com.android.build.api.instrumentation.InstrumentationScope
import com.android.build.api.variant.Component
import java.io.File
import org.gradle.api.Project

internal class ComponentCompatApi72Impl(private val component: Component) : ComponentCompat() {

Expand All @@ -42,4 +44,7 @@ internal class ComponentCompatApi72Impl(private val component: Component) : Comp
override fun setAsmFramesComputationMode(mode: FramesComputationMode) {
component.instrumentation.setAsmFramesComputationMode(mode)
}

override fun getJavaCompileClassesDir(project: Project): File =
project.layout.buildDirectory.dir("intermediates/javac/${component.name}/classes").get().asFile
}
15 changes: 15 additions & 0 deletions java/dagger/hilt/android/plugin/agp-wrapper-8-3/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
}

compileKotlin {
kotlinOptions {
jvmTarget = 11
}
}

dependencies {
implementation project(':agp-wrapper')
compileOnly gradleApi()
compileOnly "com.android.tools.build:gradle:8.3.0-alpha11"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2023 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 dagger.hilt.android.plugin.util

import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.api.variant.HasAndroidTest
import org.gradle.api.Project

class AndroidComponentsExtensionCompatApi83Impl(private val project: Project) :
AndroidComponentsExtensionCompat {

override fun onAllVariants(block: (ComponentCompat) -> Unit) {
val actual = project.extensions.getByType(AndroidComponentsExtension::class.java)
actual.onVariants { variant ->
block.invoke(ComponentCompatApi83Impl(variant))

(variant as? HasAndroidTest)?.androidTest?.let { block.invoke(ComponentCompatApi83Impl(it)) }

variant.unitTest?.let { block.invoke(ComponentCompatApi83Impl(it)) }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2023 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 dagger.hilt.android.plugin.util

import com.android.build.api.instrumentation.AsmClassVisitorFactory
import com.android.build.api.instrumentation.FramesComputationMode
import com.android.build.api.instrumentation.InstrumentationParameters
import com.android.build.api.instrumentation.InstrumentationScope
import com.android.build.api.variant.Component
import java.io.File
import org.gradle.api.Project

internal class ComponentCompatApi83Impl(private val component: Component) : ComponentCompat() {

override val name: String
get() = component.name

override fun <ParamT : InstrumentationParameters> transformClassesWith(
classVisitorFactoryImplClass: Class<out AsmClassVisitorFactory<ParamT>>,
scope: InstrumentationScope,
instrumentationParamsConfig: (ParamT) -> Unit
) {
component.instrumentation.transformClassesWith(
classVisitorFactoryImplClass,
scope,
instrumentationParamsConfig
)
}

override fun setAsmFramesComputationMode(mode: FramesComputationMode) {
component.instrumentation.setAsmFramesComputationMode(mode)
}

override fun getJavaCompileClassesDir(project: Project): File {
// TODO(kuanyingchou): replace hardcoded path with Artifacts API:
// https://developer.android.com/reference/tools/gradle-api/8.1/com/android/build/api/artifact/Artifacts
val task = "compile${component.name.replaceFirstChar { it.uppercase() }}JavaWithJavac"
return project.layout.buildDirectory.dir(
"intermediates/javac/${component.name}/${task}/classes"
).get().asFile
}
}
7 changes: 5 additions & 2 deletions java/dagger/hilt/android/plugin/agp-wrapper-impl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ plugins {
id 'org.jetbrains.kotlin.jvm'
}

kotlin {
jvmToolchain(11)
compileKotlin {
kotlinOptions {
jvmTarget = 11
}
}

dependencies {
api project(':agp-wrapper')
implementation project(':agp-wrapper-7-0')
implementation project(':agp-wrapper-7-1')
implementation project(':agp-wrapper-7-2')
implementation project(':agp-wrapper-8-3')
compileOnly gradleApi()
compileOnly "com.android.tools.build:gradle:$agp_version"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import org.gradle.api.Project
fun getAndroidComponentsExtension(project: Project): AndroidComponentsExtensionCompat {
val version = SimpleAGPVersion.ANDROID_GRADLE_PLUGIN_VERSION
return when {
version >= SimpleAGPVersion(8, 3) -> {
AndroidComponentsExtensionCompatApi83Impl(project)
}
version >= SimpleAGPVersion(7, 2) -> {
AndroidComponentsExtensionCompatApi72Impl(project)
}
Expand Down
6 changes: 4 additions & 2 deletions java/dagger/hilt/android/plugin/agp-wrapper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ plugins {
id 'org.jetbrains.kotlin.jvm'
}

kotlin {
jvmToolchain(11)
compileKotlin {
kotlinOptions {
jvmTarget = 11
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import com.android.build.api.instrumentation.AsmClassVisitorFactory
import com.android.build.api.instrumentation.FramesComputationMode
import com.android.build.api.instrumentation.InstrumentationParameters
import com.android.build.api.instrumentation.InstrumentationScope
import java.io.File
import org.gradle.api.Project

/**
* Compatibility version of [com.android.build.api.variant.Component]
Expand All @@ -40,4 +42,7 @@ abstract class ComponentCompat {

/** Redeclaration of [com.android.build.api.variant.Component.setAsmFramesComputationMode] */
abstract fun setAsmFramesComputationMode(mode: FramesComputationMode)

/** Return the directory that contains the classes from JavaCompile task */
abstract fun getJavaCompileClassesDir(project: Project): File
}
5 changes: 1 addition & 4 deletions java/dagger/hilt/android/plugin/main/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,11 @@ tasks.withType(PluginUnderTestMetadata.class).named("pluginUnderTestMetadata").c
it.pluginClasspath.from(configurations.testPluginCompile)
}

kotlin {
jvmToolchain(11)
}

compileKotlin {
kotlinOptions {
allWarningsAsErrors = true
freeCompilerArgs += [ "-opt-in=kotlin.ExperimentalStdlibApi" ]
jvmTarget = 11
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import dagger.hilt.android.plugin.util.getKaptConfigName
import dagger.hilt.android.plugin.util.getKspConfigName
import dagger.hilt.android.plugin.util.isKspTask
import dagger.hilt.processor.internal.optionvalues.GradleProjectType
import java.io.File
import javax.inject.Inject
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
Expand Down Expand Up @@ -248,8 +247,7 @@ class HiltGradlePlugin @Inject constructor(
classVisitorFactoryImplClass = AndroidEntryPointClassVisitor.Factory::class.java,
scope = InstrumentationScope.PROJECT
) { params ->
val classesDir =
File(project.buildDir, "intermediates/javac/${androidComponent.name}/classes")
val classesDir = androidComponent.getJavaCompileClassesDir(project)
params.additionalClassesDir.set(classesDir)
}
androidComponent.setAsmFramesComputationMode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ plugins {

android {
compileSdkVersion 32
buildToolsVersion "32.0.0"
namespace "simple.app"

defaultConfig {
applicationId "simple.app"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="simple.app">
<application android:name=".SimpleApp" android:label="Flavored App">
<receiver android:name=".SimpleReceiver" android:exported="false">
</receiver>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (C) 2023 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 simple.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
class SimpleReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ plugins {

android {
compileSdkVersion 32
buildToolsVersion "32.0.0"
namespace "simple.library"

defaultConfig {
minSdkVersion 16
Expand Down
Loading

0 comments on commit 6018cd2

Please sign in to comment.