Android Studio中应用级gradle文件中“release”块内的“后处理”块是什么?

时间:2017-11-27 11:39:00

标签: android gradle android-gradle build.gradle

以下是app level gradle文件的snipet。

 buildTypes {
    release {

        postprocessing {
            removeUnusedCode false
            removeUnusedResources false
            obfuscate false
            optimizeCode false
            proguardFile 'proguard-rules.pro'
        }
    }
}

这里,'后处理'在gradle文件中似乎是新的。这是什么意思? Android studio在添加'setMinifyEnabled true'的同时显示错误。

工作室错误: 错误:setMinifyEnabled true方法不能与postprocessing

一起使用

1 个答案:

答案 0 :(得分:7)

postprocessing块(新的后处理DSL)简单易用,可替代构建时生成的proguard-android.txtproguard-android-optimize.txtproguard-android.txtproguard-android-optimize.txt的替代方案并存储在构建目录中。

<强> 1。 proguard-rules.pro - 您自己的规则

buildTypes {
    release {            
        proguardFile 'proguard-rules.pro'
    }
}

<强> 2。 proguard-android.txt - 标准默认规则

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

第3。 proguard-android-optimize.txt - 优化的默认规则

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

<强> 4。 proguard-android.txt - 标准默认规则,proguard-rules.pro - 您自己的规则

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

<强> 5。 proguard-android-optimize.txt - 优化的默认规则,proguard-rules.pro - 您自己的规则

buildTypes {
    release {            
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

<强> 6。 removeUnusedCode,removeUnusedResources,obfuscate,optimizeCode - 简单易用的默认规则

buildTypes {
    release {
        postprocessing {
            removeUnusedCode false
            removeUnusedResources false
            obfuscate false
            optimizeCode false
        }
    }
}

<强> 7。 removeUnusedCode,removeUnusedResources,obfuscate,optimizeCode - 简单易用的配置默认规则,proguard-rules.pro - 你自己的规则

buildTypes {
    release {
        postprocessing {
            removeUnusedCode false
            removeUnusedResources false
            obfuscate false
            optimizeCode false
            proguardFile 'proguard-rules.pro'
        }
    }
}

<强> 8。 removeUnusedCode,removeUnusedResources,obfuscate,optimizeCode - 简单易用的默认规则,proguard-defaults.txt - 默认规则(不禁用任何操作,包括优化配置,用于新的&#34; postProcessing&#34; DSL块)

buildTypes {
    release {
        postprocessing {
            removeUnusedCode false
            removeUnusedResources false
            obfuscate false
            optimizeCode false
            proguardFile getDefaultProguardFile('proguard-defaults.txt')
        }
    }
}

<强> 9。 removeUnusedCode,removeUnusedResources,obfuscate,optimizeCode - 简单易用的默认规则,proguard-defaults.txt - 默认规则(不禁用任何操作,包括优化配置,用于新的&#34; postProcessing&#34; DSL block),proguard-rules.pro - 你自己的规则

buildTypes {
    release {
        postprocessing {
            removeUnusedCode false
            removeUnusedResources false
            obfuscate false
            optimizeCode false
            proguardFiles getDefaultProguardFile('proguard-defaults.txt'), 'proguard-rules.pro'
        }
    }
}

您也可以在后处理块中使用PostprocessingOptions:

//Test ProGuard rule files
testProguardFile 'test-proguard-rules.pro'
testProguardFiles 'test-proguard-rules-1.pro', 'test-proguard-rules-2.pro'    
//ProGuard rule files to be included in the published AAR.
//These proguard rule files will then be used by any application project that consumes the AAR (if ProGuard is enabled).
//This is only valid for Library project. This is ignored in Application project.
consumerProguardFile 'consumer-proguard-rules.pro'    
consumerProguardFiles 'consumer-proguard-rules-1.pro', 'consumer-proguard-rules-2.pro'

来自com.android.tools.build:gradle:3.1.0-alpha08的后处理选项.java:

/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.android.build.gradle.internal.dsl;

import static com.google.common.base.Verify.verifyNotNull;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.annotations.VisibleForTesting;
import com.android.build.gradle.ProguardFiles;
import com.android.build.gradle.internal.scope.CodeShrinker;
import com.android.utils.HelpfulEnumConverter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import org.gradle.api.Project;

/** DSL object for configuring postProcessing: removing dead code, obfuscating etc. */
public class PostprocessingOptions {
private static final String AUTO = "auto";
private static final HelpfulEnumConverter<CodeShrinker> SHRINKER_CONVERTER =
        new HelpfulEnumConverter<>(CodeShrinker.class);

@NonNull private final Project project;

private boolean removeUnusedCode;
private boolean removeUnusedResources;
private boolean obfuscate;
private boolean optimizeCode;

private List<File> proguardFiles;
private List<File> testProguardFiles;
private List<File> consumerProguardFiles;

@Nullable private CodeShrinker codeShrinker;

@Inject
public PostprocessingOptions(@NonNull Project project) {
    this(
            project,
            ImmutableList.of(
                    ProguardFiles.getDefaultProguardFile(
                            ProguardFiles.ProguardFile.NO_ACTIONS.fileName, project)));
}

@VisibleForTesting
PostprocessingOptions(@NonNull Project project, List<File> proguardFiles) {
    this.project = project;
    this.proguardFiles = Lists.newArrayList(proguardFiles);
    this.testProguardFiles = new ArrayList<>();
    this.consumerProguardFiles = new ArrayList<>();
}

public void initWith(PostprocessingOptions that) {
    this.removeUnusedCode = that.isRemoveUnusedCode();
    this.removeUnusedResources = that.isRemoveUnusedResources();
    this.obfuscate = that.isObfuscate();
    this.optimizeCode = that.isOptimizeCode();
    this.proguardFiles = Lists.newArrayList(that.getProguardFiles());
    this.testProguardFiles = Lists.newArrayList(that.getTestProguardFiles());
    this.consumerProguardFiles = Lists.newArrayList(that.getConsumerProguardFiles());
    this.codeShrinker = that.getCodeShrinkerEnum();
}

public boolean isRemoveUnusedCode() {
    return removeUnusedCode;
}

public void setRemoveUnusedCode(boolean removeUnusedCode) {
    this.removeUnusedCode = removeUnusedCode;
}

public boolean isRemoveUnusedResources() {
    return removeUnusedResources;
}

public void setRemoveUnusedResources(boolean removeUnusedResources) {
    this.removeUnusedResources = removeUnusedResources;
}

public boolean isObfuscate() {
    return obfuscate;
}

public void setObfuscate(boolean obfuscate) {
    this.obfuscate = obfuscate;
}

public boolean isOptimizeCode() {
    return optimizeCode;
}

public void setOptimizeCode(boolean optimizeCode) {
    this.optimizeCode = optimizeCode;
}

public List<File> getProguardFiles() {
    return proguardFiles;
}

public void setProguardFiles(List<Object> proguardFiles) {
    this.proguardFiles = new ArrayList<>();
    for (Object file : proguardFiles) {
        this.proguardFiles.add(project.file(file));
    }
}

public void proguardFile(Object file) {
    this.proguardFiles.add(project.file(file));
}

public void proguardFiles(Object... files) {
    for (Object file : files) {
        proguardFile(file);
    }
}

public List<File> getTestProguardFiles() {
    return testProguardFiles;
}

public void setTestProguardFiles(List<Object> testProguardFiles) {
    this.testProguardFiles = new ArrayList<>();
    for (Object file : testProguardFiles) {
        this.testProguardFiles.add(project.file(file));
    }
}

public void testProguardFile(Object file) {
    this.testProguardFiles.add(project.file(file));
}

public void testProguardFiles(Object... files) {
    for (Object file : files) {
        testProguardFile(file);
    }
}

public List<File> getConsumerProguardFiles() {
    return consumerProguardFiles;
}

public void setConsumerProguardFiles(List<Object> consumerProguardFiles) {
    this.consumerProguardFiles = new ArrayList<>();
    for (Object file : consumerProguardFiles) {
        this.consumerProguardFiles.add(project.file(file));
    }
}

public void consumerProguardFile(Object file) {
    this.consumerProguardFiles.add(project.file(file));
}

public void consumerProguardFiles(Object... files) {
    for (Object file : files) {
        consumerProguardFile(file);
    }
}

@NonNull
public String getCodeShrinker() {
    if (codeShrinker == null) {
        return AUTO;
    } else {
        return verifyNotNull(SHRINKER_CONVERTER.reverse().convert(codeShrinker));
    }
}

public void setCodeShrinker(@NonNull String name) {
    if (name.equals(AUTO)) {
        codeShrinker = null;
    } else {
        codeShrinker = SHRINKER_CONVERTER.convert(name);
    }
}

/** For Gradle code, not to be used in the DSL. */
@Nullable
public CodeShrinker getCodeShrinkerEnum() {
    return codeShrinker;
}
}
来自com.android.tools.build:gradle:3.1.0-alpha08的

ProguardFiles.java:

...
public enum ProguardFile {
    /** Default when not using the "postProcessing" DSL block. */
    DONT_OPTIMIZE("proguard-android.txt"),

    /** Variant of the above which does not disable optimizations. */
    OPTIMIZE("proguard-android-optimize.txt"),

    /**
     * Does not disable any actions, includes optimizations config. To be used with the new
     * "postProcessing" DSL block.
     */
    NO_ACTIONS("proguard-defaults.txt"),
    ;

    @NonNull public final String fileName;

    ProguardFile(@NonNull String fileName) {
        this.fileName = fileName;
    }
}
...