Skip to content

Commit aead3c5

Browse files
author
Said Tahsin Dane
authored
Fix small false-positive for ViewBinding (#711)
* Reproduce false-positive result for ViewBinding * Simple fix to always include main * Remove unneeded import
1 parent d10137d commit aead3c5

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

modulecheck-core/src/test/kotlin/modulecheck/core/DisableViewBindingTest.kt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,85 @@ class DisableViewBindingTest : RunnerTest() {
278278
logger.parsedReport() shouldBe listOf()
279279
}
280280

281+
@Test
282+
fun `ViewBinding from debug & release is used in main source set`() {
283+
284+
val lib1 = androidLibrary(":lib1", "com.modulecheck.lib1") {
285+
buildFile {
286+
"""
287+
plugins {
288+
id("com.android.library")
289+
kotlin("android")
290+
}
291+
292+
android {
293+
buildFeatures.viewBinding = true
294+
}
295+
"""
296+
}
297+
addLayoutFile(
298+
"fragment_lib1.xml",
299+
"""<?xml version="1.0" encoding="utf-8"?>
300+
<androidx.constraintlayout.widget.ConstraintLayout
301+
xmlns:android="https://schemas.android.com/apk/res/android"
302+
android:id="@+id/fragment_container"
303+
android:layout_width="match_parent"
304+
android:layout_height="match_parent"
305+
/>
306+
""",
307+
SourceSetName.DEBUG
308+
)
309+
addLayoutFile(
310+
"fragment_lib1.xml",
311+
"""<?xml version="1.0" encoding="utf-8"?>
312+
<androidx.constraintlayout.widget.ConstraintLayout
313+
xmlns:android="https://schemas.android.com/apk/res/android"
314+
android:id="@+id/fragment_container"
315+
android:layout_width="match_parent"
316+
android:layout_height="match_parent">
317+
318+
<!-- Slightly different XML layout for release-->
319+
</androidx.constraintlayout.widget.ConstraintLayout>
320+
""",
321+
SourceSetName.RELEASE
322+
)
323+
324+
// Setting a debug base package, but it's never used. The inferred FqName for the generated
325+
// binding should still reflect the main source set even though it's evaluating a file in
326+
// debug.
327+
platformPlugin.manifests[SourceSetName.DEBUG] = projectDir
328+
.child("src/debug/AndroidManifest.xml")
329+
.createSafely("<manifest package=\"com.modulecheck.lib1.debug\" />")
330+
331+
addKotlinSource(
332+
"""
333+
package com.modulecheck.lib1
334+
335+
import com.modulecheck.lib1.databinding.FragmentLib1Binding
336+
337+
val binding = FragmentLib1Binding()
338+
"""
339+
)
340+
}
341+
342+
run(
343+
autoCorrect = false
344+
).isSuccess shouldBe true
345+
346+
lib1.buildFile shouldHaveText """
347+
plugins {
348+
id("com.android.library")
349+
kotlin("android")
350+
}
351+
352+
android {
353+
buildFeatures.viewBinding = true
354+
}
355+
"""
356+
357+
logger.parsedReport() shouldBe listOf()
358+
}
359+
281360
@Test
282361
fun `ViewBinding from debug with different base package is used in debug source set`() {
283362

modulecheck-rule/impl/src/main/kotlin/modulecheck/rule/impl/DisableViewBindingRule.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import modulecheck.api.context.referencesForSourceSetName
2121
import modulecheck.config.ModuleCheckSettings
2222
import modulecheck.finding.FindingName
2323
import modulecheck.finding.android.DisableViewBindingGenerationFinding
24+
import modulecheck.parsing.gradle.model.SourceSetName
2425
import modulecheck.project.McProject
2526
import modulecheck.project.isAndroid
2627
import modulecheck.project.project
@@ -52,7 +53,7 @@ class DisableViewBindingRule @Inject constructor() : DocumentedRule<DisableViewB
5253
.takeIf { it.isNotEmpty() }
5354
?: return@forEach
5455

55-
val usedInProject = sourceSetName.withDownStream(project)
56+
val usedInProject = sourceSetName.withDownStream(project).plus(SourceSetName.MAIN)
5657
.any { sourceSetNameOrDownstream ->
5758

5859
generatedBindings.any { generated ->

0 commit comments

Comments
 (0)