Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make runProguard work for the android-library Gradle plugin?

I would like to run ProGuard on release build of my Android library. Below is my build.gradle file that is within my project.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android-library'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 18
    }

    sourceSets {
        main {
            java {
                srcDir 'src/main/java'
            }
            resources {
                srcDir 'src/../lib'
            }
        }
    }

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

Running gradlew clean or gradlew build returns the following error:

> Could not find method buildTypes() for arguments [build_25r1m0a3etn5cudtt5odlegprd$_run_closure2_closure10@5f3306ad] on project

It seems that plugin android-library is missing the buildTypes method. If I change apply plugin to android, gradlew runs successfully. However, this is a library project and I would like to keep it so. Other parts my my gradle.build (not mentioned here) rely on the project being a library.

Is there any way to make the android-library plugin run ProGuard on build?

like image 853
BVB Avatar asked Sep 30 '13 18:09

BVB


People also ask

Which are the two types of plugins in gradle?

There are two general types of plugins in Gradle, binary plugins and script plugins.

What is gradle plugin Android?

The Android Gradle plugin (AGP) is the official build system for Android applications. It includes support for compiling many different types of sources and linking them together into an application that you can run on a physical Android device or an emulator.

What is proguard gradle?

The Android Gradle plugin generates proguard-android-optimize. txt , which includes rules that are useful to most Android projects and enables @Keep* annotations. By default, when creating a new module using Android Studio, the module-level build. gradle file includes this rules file in your release build for you.


1 Answers

Solved my issue by taking release out of buildTypes like so:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android-library'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 18
    }

    sourceSets {
        main {
            java {
                srcDir 'src/main/java'
            }
            resources {
                srcDir 'src/../lib'
            }
        }
    }

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

Also note that I added another proguardFile under release. This fixed another issue I was having after solving the initial buildTypes problem.

like image 62
BVB Avatar answered Nov 04 '22 00:11

BVB