Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "postprocessing" block inside the 'release' block in app level gradle file in Android Studio?

Below is the snipet of app level gradle file.

 buildTypes {
    release {

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

Here, 'postprocessing' seems new in the gradle file. What does it mean? Android studio shows error while adding 'setMinifyEnabled true' along with it.

Studio Error: Error:The setMinifyEnabled true method cannot be used with together with the postprocessing

like image 608
Rikin Prajapati Avatar asked Nov 27 '17 11:11

Rikin Prajapati


People also ask

How do I get rid of .Gradle folder?

Inside the project you can find the . gradle folder. Inside you can find all settings and other files used by gradle to build the project. You can delete these files without problems.

What are flavors in Android?

Product flavours lets you create multiple variants of an android app while using a single codebase. To create product flavours you need to define rules in the build.

What is the purpose of Gradle files in Android?

gradle files are the main script files for automating the tasks in an android project and are used by Gradle for generating the APK from the source files.

What is the purpose of the Gradle in Android Studio and at what point can its content be modified?

Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.


1 Answers

postprocessing block (the new postprocessing DSL) is simple and easy to configure alternative to proguard-android.txt and proguard-android-optimize.txt (proguard-android.txt and proguard-android-optimize.txt generated at build time and stored in the build directory).

1. proguard-rules.pro - your own rules

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

2. proguard-android.txt - standard default rules

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

3. proguard-android-optimize.txt - optimized default rules

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

4. proguard-android.txt - standard default rules, proguard-rules.pro - your own rules

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

5. proguard-android-optimize.txt - optimized default rules, proguard-rules.pro - your own rules

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

6. removeUnusedCode, removeUnusedResources, obfuscate, optimizeCode - simple and easy to configure default rules

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

7. removeUnusedCode, removeUnusedResources, obfuscate, optimizeCode - simple and easy to configure default rules, proguard-rules.pro - your own rules

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

8. removeUnusedCode, removeUnusedResources, obfuscate, optimizeCode - simple and easy to configure default rules, proguard-defaults.txt - default rules (does not disable any actions, includes optimizations config, to be used with the new "postProcessing" DSL block)

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

9. removeUnusedCode, removeUnusedResources, obfuscate, optimizeCode - simple and easy to configure default rules, proguard-defaults.txt - default rules (does not disable any actions, includes optimizations config, to be used with the new "postProcessing" DSL block), proguard-rules.pro - your own rules

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

Also you can use in postprocessing block PostprocessingOptions below:

//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'

PostprocessingOptions.java from com.android.tools.build:gradle:3.1.0-alpha08:

/*
* 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;
}
}

ProguardFiles.java from com.android.tools.build:gradle:3.1.0-alpha08:

...
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;
    }
}
...
like image 178
Alexander Savin Avatar answered Nov 13 '22 07:11

Alexander Savin