Skip to content

Fix small false-positive for ViewBinding #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,85 @@ class DisableViewBindingTest : RunnerTest() {
logger.parsedReport() shouldBe listOf()
}

@Test
fun `ViewBinding from debug & release is used in main source set`() {

val lib1 = androidLibrary(":lib1", "com.modulecheck.lib1") {
buildFile {
"""
plugins {
id("com.android.library")
kotlin("android")
}

android {
buildFeatures.viewBinding = true
}
"""
}
addLayoutFile(
"fragment_lib1.xml",
"""<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
""",
SourceSetName.DEBUG
)
addLayoutFile(
"fragment_lib1.xml",
"""<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Slightly different XML layout for release-->
</androidx.constraintlayout.widget.ConstraintLayout>
""",
SourceSetName.RELEASE
)

// Setting a debug base package, but it's never used. The inferred FqName for the generated
// binding should still reflect the main source set even though it's evaluating a file in
// debug.
platformPlugin.manifests[SourceSetName.DEBUG] = projectDir
.child("src/debug/AndroidManifest.xml")
.createSafely("<manifest package=\"com.modulecheck.lib1.debug\" />")

addKotlinSource(
"""
package com.modulecheck.lib1

import com.modulecheck.lib1.databinding.FragmentLib1Binding

val binding = FragmentLib1Binding()
"""
)
}

run(
autoCorrect = false
).isSuccess shouldBe true

lib1.buildFile shouldHaveText """
plugins {
id("com.android.library")
kotlin("android")
}

android {
buildFeatures.viewBinding = true
}
"""

logger.parsedReport() shouldBe listOf()
}

@Test
fun `ViewBinding from debug with different base package is used in debug source set`() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import modulecheck.api.context.referencesForSourceSetName
import modulecheck.config.ModuleCheckSettings
import modulecheck.finding.FindingName
import modulecheck.finding.android.DisableViewBindingGenerationFinding
import modulecheck.parsing.gradle.model.SourceSetName
import modulecheck.project.McProject
import modulecheck.project.isAndroid
import modulecheck.project.project
Expand Down Expand Up @@ -52,7 +53,7 @@ class DisableViewBindingRule @Inject constructor() : DocumentedRule<DisableViewB
.takeIf { it.isNotEmpty() }
?: return@forEach

val usedInProject = sourceSetName.withDownStream(project)
val usedInProject = sourceSetName.withDownStream(project).plus(SourceSetName.MAIN)
.any { sourceSetNameOrDownstream ->

generatedBindings.any { generated ->
Expand Down