You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Groovy迁移至Kotlin后BuildConfig变量缺失的问题及解决

问题:迁移至Kotlin构建脚本后BuildConfig自定义变量无法访问

迁移到Kotlin构建脚本前,项目通过BuildConfig共享环境变量:根据release/debug构建变体,自动读取对应properties文件中的值。

  • release.properties内容:
apiUrl="https://api.url.com"
  • debug.properties内容:
apiUrl="https://api.url.test"

以debug变体为例,生成的BuildConfig会包含:

public final class BuildConfig {
    ...
    public static final String apiUrl = "https://api.url.test";
}

迁移后,代码中执行val apiUrl: String = BuildConfig.apiUrl时,提示错误:Unresolved reference: apiUrl。已在gradle.properties中添加配置开启BuildConfig:

android.defaults.buildfeatures.buildconfig=true

当前项目的build.gradle.kts配置如下:

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("kotlin-kapt")
    id("com.google.dagger.hilt.android")
}

android {
    namespace = "com.ase.app"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.ase.app"
        minSdk = 26
        targetSdk = 33
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isDebuggable = false
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
        debug {
            isDebuggable = true
            applicationIdSuffix = ".debug"
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.4.3"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

解决方法(2023年9月15日)

步骤1:修改build.gradle.kts配置

在android代码块外添加androidComponents配置,同时实现读取properties文件的工具函数:

androidComponents {
    onVariants(selector().withBuildType("release")) { variant ->
        variant.buildConfigFields.put("apiUrl", BuildConfigField("String", getProperty("release.properties", "API_URL"), ""))
    }
    onVariants(selector().withBuildType("debug")) { variant ->
        variant.buildConfigFields.put("apiUrl", BuildConfigField("String", getProperty("debug.properties", "API_URL"), ""))
    }
}

fun getProperty(fileName: String, propertyName: String): String {
    val propertiesFile = File(fileName)
    if (propertiesFile.exists()) {
        val properties = Properties()
        properties.load(FileInputStream(propertiesFile))
        return properties.getProperty(propertyName) ?: ""
    } else {
        throw GradleException("$fileName not found!")
    }
}

步骤2:配置properties文件

在gradle.properties同级目录下,确保release.properties和debug.properties存在,内容格式如下:

  • release.properties:
API_URL="https://api.url.com"
  • debug.properties:
API_URL="https://api.url.test"

内容的提问来源于stack exchange,提问作者alvaro_se

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 00:47:00