yshrsmz / BuildKonfig

BuildConfig for Kotlin Multiplatform Project
Apache License 2.0
800 stars 33 forks source link

How to solve "Unresolved reference: BuildKonfig" error? #154

Closed yuya-h-29 closed 2 months ago

yuya-h-29 commented 5 months ago

Hello, @yshrsmz. I'm new to Kotlin Multiplatform and Android development, and I'm trying to use BuildKonfig in my project. However, I'm having trouble importing it.

Screenshot 2024-06-18 at 12 15 40

I've already posted my question on my stack overflow (here is the link), but I wanted to ask if there are any additional steps I'm missing when using BuildKonfig in my project.

Here are the steps I've taken:

  1. Added the BuildKonfig plugin (in shared/build.grade.kts):
    
    import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties
    import com.codingfeline.buildkonfig.compiler.FieldSpec.Type.STRING

plugins { alias(libs.plugins.kotlinMultiplatform) alias(libs.plugins.androidLibrary) id("co.touchlab.skie") version "0.6.1" kotlin("plugin.serialization") version "1.9.20" id("com.codingfeline.buildkonfig") version "0.15.1" }


2. Configured buildkonfig block (in shared/build.grade.kts):

buildkonfig { packageName = "com.example.myapp"

defaultConfigs {
    val apiKey: String = gradleLocalProperties(rootDir, providers).getProperty("API_KEY")
    buildConfigField(STRING, "API_KEY", apiKey)
}

}


3. Synchronized the project:
Clicked on "Sync Now"

4. Error Encountered:
Despite this, I can't import BuildKonfig and get the "Unresolved reference: BuildKonfig" error in my project:

<img width="584" alt="Screenshot 2024-06-18 at 12 15 40" src="https://github.com/yshrsmz/BuildKonfig/assets/54800510/4569f125-adfd-435f-8a59-f7cca632b33e">

package com.example.myapp

import io.ktor.client.HttpClient import io.ktor.client.call.body import io.ktor.client.request.get

class ArticlesService(private val httpClient: HttpClient) {

private val country = "us"
private val category = "sports"
private val apiKey = BuildKonfig.API_KEY // I get error in here

suspend fun fetchArticles(): List<ArticleRaw> {
    val response: ArticlesResponse = httpClient.get("https://newsapi.org/v2/top-headlines?country=$country&category=$category&apiKey=$apiKey").body()
    return response.articles
}

}



Is there something I'm missing or any additional steps I need to take to use BuildKonfig correctly in my project?

---

I use Android Studio Jellyfish | 2023.3.1 Patch 1
yshrsmz commented 5 months ago

@yuya-h-29

Hi, can you show us your complete build.gradle.kts? Specifically, I'd like to see how you configure the sourcesets.

yuya-h-29 commented 5 months ago

@yshrsmz Thank you for the quick response. Here is my build.gradle.kts in the shared module.

import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties
import com.codingfeline.buildkonfig.compiler.FieldSpec.Type.STRING

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidLibrary)
    id("co.touchlab.skie") version "0.6.1"
    kotlin("plugin.serialization") version "1.9.20"
    id("com.codingfeline.buildkonfig") version "0.15.1"
}

kotlin {
    androidTarget {
        compilations.all {
            kotlinOptions {
                jvmTarget = "1.8"
            }
        }
    }

    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
            isStatic = true
        }
    }

    sourceSets {
        commonMain.dependencies {
            //put your multiplatform dependencies here
            implementation(libs.kotlinx.coroutines.core)
            implementation(libs.ktor.client.core)
            implementation(libs.ktor.client.content.negotiation)
            implementation(libs.ktor.serialization.kotlinx.json)
            implementation(libs.kotlinx.datetime)
        }

        androidMain.dependencies {
            implementation(libs.androidx.lifecycle.viewmodel.ktx)
            implementation(libs.ktor.client.android)
        }

        iosMain.dependencies {
            implementation(libs.ktor.client.darwin)
        }

        commonTest.dependencies {
            implementation(libs.kotlin.test)
        }
    }
}

buildkonfig {
    packageName = "com.example.myApp"

    defaultConfigs {
        val apiKey: String = gradleLocalProperties(rootDir).getProperty("API_KEY")
        buildConfigField(STRING, "API_KEY", apiKey)
    }
}

android {
    namespace = "com.example.myApp"
    compileSdk = 34
    defaultConfig {
        minSdk = 24
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}
yuya-h-29 commented 5 months ago

Now I figured @yshrsmz documented run generateBuildKonfig task. Could you tell me what and how should I run generateBuildKonfig task please? (I haven't done this in my project, so this might be the reason why I get Unresolved reference error.)

Mina-1316 commented 5 months ago

can you try adding this to top of your code and rebuild your project?

import com.example.myApp.BuildKonfig

BuildKonfig file is created at compile time, not gradle configure time, and maybe problem comes from here... IntelliJ provided Gradle sync task won't run generateBuildKonfig task.

yshrsmz commented 3 months ago

Could you tell me what and how should I run generateBuildKonfig task please? (I haven't done this in my project, so this might be the reason why I get Unresolved reference error.)

Basically, you don't need to call it manually, as it should be hooked to the main compile tasks.

Once you build the project, BuildKonfig objects should be populated automatically.

yshrsmz commented 2 months ago

Feel free to re-open the issue if you still have some issue/update