Skip to content

Commit

Permalink
Fix incorrect ProductFlavorAttr key in Hilt's compileOnly configurati…
Browse files Browse the repository at this point in the history
…on for the AggregatingTask.

RELNOTES=n/a
PiperOrigin-RevId: 377447733
  • Loading branch information
danysantiago authored and Dagger Team committed Jun 4, 2021
1 parent c1359a2 commit d568957
Show file tree
Hide file tree
Showing 11 changed files with 309 additions and 2 deletions.
Expand Up @@ -314,8 +314,8 @@ class HiltGradlePlugin @Inject constructor(
)
variant.productFlavors.forEach { flavor ->
attrContainer.attribute(
Attribute.of(flavor.name, ProductFlavorAttr::class.java),
project.objects.named(ProductFlavorAttr::class.java, flavor.getName())
Attribute.of(flavor.dimension!!, ProductFlavorAttr::class.java),
project.objects.named(ProductFlavorAttr::class.java, flavor.name)
)
}
}
Expand Down
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2021 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.
*/

plugins {
id 'com.android.application'
id 'dagger.hilt.android.plugin'
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"

flavorDimensions 'api', 'version'
productFlavors {
demo {
dimension 'version'
}
full {
dimension 'version'
}
minApi24 {
dimension 'api'
minSdkVersion '24'
versionNameSuffix "-minApi24"
}
minApi21 {
dimension "api"
minSdkVersion '21'
versionNameSuffix "-minApi21"
}
}

defaultConfig {
applicationId "simple.app"
minSdkVersion 21
targetSdkVersion 30
}

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(':feature')
}

hilt {
enableAggregatingTask = true
}
@@ -0,0 +1,20 @@
<!--
~ Copyright (C) 2021 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="simple.app">
<application android:name=".SimpleApp" android:label="Flavored App">
</application>
</manifest>
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2021 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.app.Application;
import dagger.hilt.android.HiltAndroidApp;

/** Just an application. */
@HiltAndroidApp
public class SimpleApp extends Application {
}
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2021 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.
*/

buildscript {
repositories {
google()
jcenter()
}
}

allprojects {
repositories {
mavenLocal()
google()
jcenter()
}
}
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2021 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.
*/

plugins {
id 'com.android.library'
id 'dagger.hilt.android.plugin'
}

android {
compileSdkVersion 30
buildToolsVersion "30.0.2"

flavorDimensions 'api', 'version'
productFlavors {
demo {
dimension 'version'
}
full {
dimension 'version'
}
minApi24 {
dimension 'api'
minSdkVersion '24'
versionNameSuffix "-minApi24"
}
minApi21 {
dimension "api"
minSdkVersion '21'
versionNameSuffix "-minApi21"
}
}

defaultConfig {
minSdkVersion 16
targetSdkVersion 30
}

compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}

dependencies {
implementation 'com.google.dagger:hilt-android:LOCAL-SNAPSHOT'
annotationProcessor 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT'
}
@@ -0,0 +1,18 @@
<!--
~ Copyright (C) 2021 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="simple.library">
</manifest>
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2021 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.library;

/** Just some 'Library' code */
public class LibraryCode {
}
@@ -0,0 +1 @@
android.useAndroidX=true
@@ -0,0 +1,3 @@
rootProject.name='Flavored Project'
include ':app'
include ':feature'
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2021 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.
*/

import com.google.common.truth.Expect
import java.io.File
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder

// Verifies the aggregated task is configured correctly in a multi-module flavored project.
class VariantsConfigurationTest {
@get:Rule
val testProjectDir = TemporaryFolder()

@get:Rule
val expect: Expect = Expect.create()

@Before
fun setup() {
val projectRoot = testProjectDir.root
File("src/test/data/flavored-project").copyRecursively(projectRoot)
}

@Test
fun verifyFlavorConfiguration_demoDebug() {
val result = runGradleTasks(":app:assembleMinApi21DemoDebug")
expect.that(result.task(":app:assembleMinApi21DemoDebug")!!.outcome)
.isEqualTo(TaskOutcome.SUCCESS)
}

@Test
fun verifyFlavorConfiguration_fullRelease() {
val result = runGradleTasks(":app:assembleMinApi24FullRelease")
expect.that(result.task(":app:assembleMinApi24FullRelease")!!.outcome)
.isEqualTo(TaskOutcome.SUCCESS)
}

private fun runGradleTasks(vararg args: String): BuildResult {
return GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments(*args)
.withPluginClasspath()
.forwardOutput()
.build()
}
}

0 comments on commit d568957

Please sign in to comment.