build.gradle feature, unresolved references

1.2k Views Asked by At

I've been trying to use some gradle build features several times without success. When I try to add steps and builds in build.gradle.ktl I always get "unresolve reference to ... "

Here I'm trying to publish to a maven repo, reading this documentation page : https://docs.gradle.org/current/userguide/publishing_setup.html#sec:basic_publishing

I've added

plugins {
    id("java-library")
    id("maven-publish")
}

and

publishing {
    publications {             // <= Here I get "Unresolved reference: publications"
        create<MavenPublication>("myLibrary") {
            from(components["java"])
        }
    }
}

In the sample above, "Unresolved reference: publications" As this is not the first time I see this playing around the gradle.build file, I'm pretty sure I forgot to import some dependency or something ?

thanks for the help

Edit: both blocks are in the same build script ("build.gradle.kts"). here is the full version :

plugins {
    id("com.android.library")
    id("org.jetbrains.kotlin.android")
    id("org.jetbrains.kotlin.plugin.serialization")
    id("java-library")
    id("maven-publish")
}

android {
    namespace = "com.domain.projectname"
    compileSdk = 33

    defaultConfig {
        minSdk = 24
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = true
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

    publishing {
        publications {
            create<MavenPublication>("myLibrary") {
                from(components["java"])
            }
        }

        repositories {
            maven {
                name = "myRepo"
                url = uri(layout.buildDirectory.dir("repo"))
            }
        }
    }
}

dependencies {
    implementation("androidx.core:core-ktx:1.8.0")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.8.0")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
1

There are 1 best solutions below

0
Vampire On

You intend to configure the maven-publish publication.
But the error you made is, to try to do it within the android { ... } block.
If the android extension would not have a plublishing { ... } block itself, this would just be unclean but nevertheless work.
But as the android plugin does have its own publishing { ... } block, you instead configure that one and there the inner publications { ... } block is not valid.

I have no idea about Android developement and whether what you do is appropriate or not. But syntax-wise, move the publishing { ... } block to the top-level where it belongs, and the "Unresolved reference: publications" will be gone:
publishing blocks

Of course you will next get an error that Android plugin and Java plugin cannot be combined in one project. :-)