错误:(26,0)Gradle DSL方法未找到:'runProguard()'

137

我正在使用 Android Studio 0.9.3 与 Gradle 'com.android.tools.build:gradle:0.14.+'

应用插件:'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion '20.0.0'

    defaultConfig {
        applicationId "xxx.xxx.xxx"
        minSdkVersion 16
        targetSdkVersion 19
        versionCode 1
        versionName "1.0.11"
    }

    signingConfigs{
        releaseConfig{
            storeFile file("xxxxxxx")
            storePassword = "xxxx"
            keyAlias = "xxxx"
            keyPassword = "xxxx"
        }
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.releaseConfig

            // adds version to file name
            applicationVariants.all { variant ->
                def file = variant.outputFile
                variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // You must install or update the Support Repository through the SDK manager to use this dependency.
    // You must install or update the Support Repository through the SDK manager to use this dependency.
    // You must install or update the Google Repository through the SDK manager to use this dependency.
    // You must install or update the Support Repository through the SDK manager to use this dependency.
    compile 'com.android.support:support-v4:19.+'
    compile 'com.android.support:appcompat-v7:19.+'
    compile 'com.mcxiaoke.volley:library:1.0.6'
    compile 'com.google.code.gson:gson:2.2.+'
}

在那个文件中之前编译是没有问题的,但现在我遇到了这个错误:Error:(26, 0) Gradle DSL method not found: 'runProguard()'

如何修复?


1
您可参考此链接:http://tools.android.com/tech-docs/new-build-system。它会为您解答这些问题。 - IgorGanapolsky
8个回答

131

尝试使用minifyEnabled代替在gradle文件中使用runProguard。这样应该可以解决问题。runProguard已被弃用,并且很快将停止工作。

编辑

要使用minifyEnabled,gradle必须更新至2.2版本或更高版本。


错误现在已更改为-->“Gradle DSL方法未找到:'minifyEnable()'” - shaktiman_droid
你是否已安装最新的Gradle构建系统(2.2)? - Varundroid
@Varundroid 我已将我的Gradle包装器更新为--> //services.gradle.org/distributions/gradle-2.2-all.zip :: 但是在minifyEnable()方面仍然出现相同的错误。 - shaktiman_droid
6
应为minifyEnabled(而不是minifyEnable)。 - JRomero
缩小资源怎么样,那是同样的事情吗? - IgorGanapolsky
显示剩余2条评论

112

更改app build.gradle文件可能会有所帮助:

旧的:

buildTypes {
    release {

        runProguard false // this line has to be changed

        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

新:

buildTypes {
    release {

        minifyEnabled false // new version

        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

按照您建议的做了,仍然出现相同的错误。 - Karan Khurana

97
据我所知,runProguard已被替换为minifyEnabled。我仍然不确定如何定义proguard的配置,但通过谷歌搜索应该可以找到答案。
编辑:
有关outFile的信息请参阅:https://groups.google.com/forum/#!topic/adt-dev/4_-5NvxuFB0 简言之:他们使用了一个更复杂的版本:
applicationVariants.all { variant ->

    variant.outputs.each { output ->

        def apk = output.outputFile;
        def newName;

        // newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-" + variant.buildType.name.toUpperCase() + ".apk");
        if (variant.buildType.name == "release") {
            newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-release.apk");
        } else {
            newName = apk.name.replace(".apk", "-v" + defaultConfig.versionName + "-beta.apk");
        }

        output.outputFile = new File(apk.parentFile, newName);

        if (output.zipAlign) {
            output.outputFile = new File(apk.parentFile, newName.replace("-unaligned", ""));
        }

        logger.info('INFO: Set outputFile to ' + output.outputFile + " for [" + output.name + "]");
    }
}

1
现在它出现了问题,错误为:(32,0)无法在com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@1615795上找到属性'outputFile'。 - NickF
1
这是正确的,阅读0.14.0下的更改以了解确切的更改 http://tools.android.com/tech-docs/new-build-system - Soham
我已经编辑过了,以反映有关outputFile的错误(编辑当前正在等待同行审查)。 - RoundSparrow hilltx
1
我已经删除了重复的 applicationVariants.all { variant -> 行,但接受其余部分,谢谢。 - WarrenFaith
我用不同的方法遇到了相同的问题:Gradle DSL 找不到 compileSdkVersion() 方法。 - Usman

26
如果您正在使用0.14.0或更高版本的gradle插件,则应该在您的build.gradle文件中将"runProguard"替换为"minifyEnabled"。
只需添加这个。
 buildTypes {           
     release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
                }
            }
minifyEnabled false 的意思是构建类型的名称不能为 main 或 androidTest(插件强制执行),并且它们必须彼此唯一。
Android Gradle 插件的新版本可以自动删除未使用的资源。这里的优势在于它不仅从您自己的代码中删除未使用的资源,而且更重要的是从您正在使用的中删除未使用的资源(例如,当库包括了支持您应用程序未实际使用的功能的资源时)。

@GeorgiAngelov 很高兴收到您的评论。愉快编码! - IntelliJ Amiya

25
截至Gradle 0.14.4,这些错误被报告为编译时错误。
因此,你必须用 minifyEnabled false/true 替换 runProguard false/true
更改已在Android Developers Blog中列出。

21

将Gradle项目迁移至版本1.0.0只需要进行简单的重命名工作,所有内容都在这里说明: http://tools.android.com/tech-docs/new-build-system/migrating-to-1-0-0

对于proguard,您只需将 'runProguard' 重命名为 'minifyEnabled',其他内容请参见下文:

Renamed Properties in BuildTypes:    
runProguard => minifyEnabled
zipAlign => zipAlignEnabled
jniDebugBuild => jniDebuggable
renderscriptDebug => renderscriptDebuggable

Renamed Properties in ProductFlavors:    
flavorGroups => flavorDimensions
packageName => applicationId
testPackageName => testApplicationId
renderscriptSupportMode => renderscriptSupportModeEnabled
ProductFlavor.renderscriptNdkMode => renderscriptNdkModeEnabled
Other Name changes

InstrumentTest was renamed to androidTest.

我很感激你提供的安卓文档以及你对它们的强调,但是在哪里可以找到其他关于zipAlign的信息呢? 这里没有提到在哪些文件中可以找到这些信息。我在我的根gradle配置文件中没有看到它们。 - tinonetic

3

这是由于gradle android工具更新至0.14.3所致。 在你的"build.gradle"文件中进行以下替换:

classpath 'com.android.tools.build:gradle:0.14.+'

通过:

classpath 'com.android.tools.build:gradle:0.14.2'

在他们修复它之前...


1

runProguard在Gradle的版本0.14.0(2014/10/31)中已更名为minifyEnabled

要解决此问题,您需要在项目的build.gradle文件中将runProguard更改为minifyEnabled。

enter image description here


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