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

Gradle构建报错:无法获取SigningConfig的keystoreProperties属性

解决SigningConfig中未知属性'keystoreProperties'的错误

错误信息

无法获取类型为com.android.build.gradle.internal.dsl.SigningConfig$AgpDecorated的SigningConfig$AgpDecorated_Decorated{name=release, storeFile=null,
storePassword=null, keyAlias=null, keyPassword=null, storeType=pkcs12,
v1SigningEnabled=true, v2SigningEnabled=true, enableV1Signing=null,
enableV2Signing=null, enableV3Signing=null, enableV4Signing=null}的未知属性'keystoreProperties'。

问题原因

你在signingConfigs的release配置中直接使用了keystoreProperties变量,但没有提前加载对应的密钥属性文件,Gradle无法识别这个未定义的变量。另外你的build.gradle中还有一处拼写错误:packagingOption应该改为packagingOptions(复数形式)。

解决方案

步骤1:创建密钥属性文件

在项目根目录下新建keystore.properties文件,填入你的签名配置信息:

keyAlias=你的密钥别名
keyPassword=你的密钥密码
storeFile=../your_keystore.jks  # 密钥文件的相对路径或绝对路径
storePassword=你的密钥库密码

步骤2:在build.gradle中加载属性文件

在app模块的build.gradle最顶部(plugins块之后,android块之前)添加代码加载属性文件:

// 加载签名配置属性文件
def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

步骤3:修正packagingOption拼写错误

将packagingOption改为packagingOptions:

packagingOptions {
    exclude 'META-INF/atonicfu.kotlin_module'
}

修改后的完整build.gradle代码

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

// 加载签名配置属性文件
def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.sonkhiasinghansh.groceryapp"
        minSdk 19
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release

            minifyEnabled false

            proguardFiles(
                getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            )
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude 'META-INF/atonicfu.kotlin_module'
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.10"
    implementation 'androidx.appcompat:appcompat:1.5.0'
    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    def room_version = "2.2.1"
    def lifecycle_version = "2.0.0"

    // Room and Architectural Components
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.legacy:legacy-support-v4:1.0.0"
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
    implementation "androidx.room:room-ktx:2.4.3"
    kapt "androidx.room:room-compiler:$room_version"

    // Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1'
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1"

    // New Material Design
    implementation "com.google.android.material:material:1.6.1"

    // ViewModel
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    //noinspection LifecycleAnnotationProcessorWithJava8
    kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
}

注意事项

  • 确保keystore.properties文件路径正确,如果放在app模块目录下,就把rootProject.file改为file
  • 不要把keystore.properties提交到版本控制系统(比如添加到.gitignore),避免密钥信息泄露

内容的提问来源于stack exchange,提问作者Ansh Singh Sonkhia

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 06:24:16