Build script error, unsupported Gradle DSL method

2019-02-21 11:16发布

问题:

I'm trying to setup gradle to create a Google Play store release in Android Studio 0.4.5. In gradle settings, I am using the default gradle wrapper. I used the Project Properties dialog to set up the signing config and 'release' build type. I only have one build module. Here is the build.gradle file that resulted:

    apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.1'
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 10
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            debuggable false
            signingConfig playstore
            proguardFile 'proguard-rules.txt'
        }
    }
    signingConfigs {
            playstore {
            keyAlias 'mykeyalias'
            storeFile file('playstore.jks')
            keyPassword 'xxxxx'
            storePassword 'xxxxx'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    compile 'com.android.support:support-v4:+'
    compile files('libs/libGoogleAnalyticsServices.jar')
}

But I am getting the following error when gradle tries to sync:

Build script error, unsupported Gradle DSL method found: 'signingConfig()'!
            Possible causes could be:  
            - you are using Gradle version where the method is absent 
            - you didn't apply Gradle plugin which provides the method
            - or there is a mistake in a build script

Is there something I need to do to setup the correct gradle?

Thanx in advance.

回答1:

First define your SigningConfigs before your buildTypes block . Also playstore method is inside signingConfigs so you have to give a reference in a manner like signingConfigs.playstore .

Your final build.gradle file should look like this :

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.1'
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 10
        versionName "1.0"
    }

   signingConfigs {
            playstore {
              keyAlias 'mykeyalias'
              storeFile file('playstore.jks')
              keyPassword 'xxxxx'
              storePassword 'xxxxx'
        }
    }


    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            debuggable false
            signingConfig signingConfigs.playstore
            proguardFile 'proguard-rules.txt'
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    compile 'com.android.support:support-v4:+'
    compile files('libs/libGoogleAnalyticsServices.jar')
}