Gradle和proguard:找不到方法runProguard(),其参数为[true]。

17

我已按照Proguard Gradle手册的建议配置了build.gradle

以下是根build.gradle文件

buildscript {
    repositories {
        flatDir dirs: '/home/username/android-sdks/tools/proguard/lib'
        mavenCentral()
    }
    dependencies {                     
        classpath 'com.android.tools.build:gradle:0.5.+'
        classpath ':proguard'
    }
}

现在这是我的项目的 build.gradle 文件

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':SomeLibraryProject')
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    sourceSets {
        ...
    }

    task runProguardTask(type: proguard.gradle.ProGuardTask) {
    }

    signingConfigs {
        debug {
            storeFile file("./keystore/keystore")
            storePassword "******"
            keyAlias "******"
            keyPassword "*******"
        }

        release {
            runProguard true
            proguardFile 'proguard-android.txt'
            storeFile file("./releasekey/keystore")
            storePassword "******"
            keyAlias "********"
            keyPassword "*******"
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }  
}

这是输出结果

$ ./gradlew build

FAILURE: Build failed with an exception.

* Where:
Build file '/home/username/Documents/eclipse/workspace/repo/ProjectName/build.gradle' line: 49

* What went wrong:
A problem occurred evaluating project ':ProjectName'.
> Could not find method runProguard() for arguments [true] on SigningConfigDsl_Decorated{name=release, storeFile=null, storePassword=null, keyAlias=null, keyPassword=null, storeType=null}.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 9.14 secs

我也想知道为什么storeFile、storePassword、keyAlias和keyPassword都是null?

3个回答

58

runProguard已被废弃(即将停止工作);请改用minifyEnabled代替

...

buildTypes {
    release {
        minifyEnabled true
        ....

4
谢谢,这解决了我在Android Studio 1.0 RC 1上的gradle错误。 - Roman Nazarevych

14

由于DSL属性名称不正确,此类错误很常见。请确保在build.gradle中指定了正确的值:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard

android {
    buildTypes {
        release {
            minifyEnabled true
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }
    }
}

您可以在此处找到具有所有属性的Javadoc(单击“下载DSL参考”按钮): http://developer.android.com/tools/building/plugin-for-gradle.html


2014-11-24更新:

0.14.0 gradle插件中更名了一些属性。runProguard -> minifyEnabled 请查看Alécio的答案并在此处查看最新更改列表:http://tools.android.com/tech-docs/new-build-system


4
runProguard现已弃用。请查看正确答案下方,你需要改为使用minifyEnabled。 - David Guerrero

4
runProguard is deprecated after gradle build tools version 1.0.0-rc1
Running ProGuard



ProGuard is supported through the Gradle plugin for ProGuard version 4.10. The ProGuard plugin is applied automatically, and the tasks are created automatically if the Build Type is configured to run ProGuard through the minifyEnabled property.

android {
    buildTypes {
        release {
            minifyEnabled true
            proguardFile getDefaultProguardFile('proguard-android.txt')
        }
    }

    productFlavors {
        flavor1 {
        }
        flavor2 {
            proguardFile 'some-other-rules.txt'
        }
    }
}

以上部分确实可以保障您的安卓代码吗? - Anshul Tyagi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接