I'm working on a multi-module Android project and ...
# android
v
I'm working on a multi-module Android project and created a convention plugin to automatically generate namespaces based on the module path. The idea is to avoid manually setting the namespace in each
build.gradle.kts
file.
not kotlin but kotlin colored 2
Here's my convention plugin `(build-logic/convention/src/main/kotlin/AndroidNamespaceConventionPlugin.kt)`:
Copy code
class AndroidNamespaceConventionPlugin : Plugin<Project> {

    override fun apply(target: Project) {
        with(target) {
            plugins.withId("com.android.library") {
                configureNamespace()
            }
            plugins.withId("com.android.application") {
                configureNamespace()
            }
        }
    }

    private fun Project.configureNamespace() {
        val basePackage = "com.hub.vivek"

        val modulePath = project.path
        val namespaceSuffix = modulePath
            .split(":")
            .filter { it.isNotBlank() }
            .joinToString(".")

        val fullNamespace = "$basePackage.$namespaceSuffix"

        extensions.configure<com.android.build.gradle.BaseExtension> {
            namespace = fullNamespace
        }
    }
}
In
build-logic/build.gradle.kts:
Copy code
plugins {
    `kotlin-dsl`
}

group = "com.hub.vivek.buildlogic"

java {
    sourceCompatibility = JavaVersion.toVersion(libs.versions.jvmTarget.get())
    targetCompatibility = JavaVersion.toVersion(libs.versions.jvmTarget.get())
}

kotlin {
    compilerOptions {
        jvmTarget.set(JvmTarget.valueOf("JVM_${libs.versions.jvmTarget.get()}"))
    }
}

dependencies {
    compileOnly(libs.compose.gradlePlugin)
    compileOnly(libs.android.gradlePlugin)
    compileOnly(libs.kotlin.gradlePlugin)
}

gradlePlugin {
    plugins {
        register("androidNameSpace") {
            id = libs.plugins.vivek.android.namespace.get().pluginId
            implementationClass = "AndroidNamespaceConventionPlugin"
        }
    }
}
In
libs.versions.toml:
Copy code
[plugins]
vivek-android-namespace = { id = "vivek.android.namespace" }
And in
feature/auth/build.gradle.kts:
Copy code
plugins {
    alias(libs.plugins.vivek.android.namespace)
    // other plugins
}
Now the problem: when I try to access the generated
R
class in the
feature:auth
module like this:
Copy code
import com.hub.vivek.feature.auth.R
val name = R.string.name
I get an error:
Unresolved reference: R
The module compiles, but the resource references are not being recognized in the IDE.
The module compiles, but the resource references are not being recognized in the IDE. What I’ve tried: 1. Verified that resources exist in res/values/strings.xml. 2. Cleaned and rebuilt the project. 3. Invalidated caches and restarted Android Studio. My Question: 🔧 How can I make sure the namespace is correctly picked up by the Android Gradle Plugin so that the generated R class is recognized in IDE and during build? Is there something I’m missing in how the namespace is being applied via the plugin?
c
Ask in the Gradle community slack.
Or
👍 1