All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.gfx.android.powerassert.PowerAssertPlugin.groovy Maven / Gradle / Ivy

Go to download

Gradle plugin for Android which enables assertions and makes them much nicer.

There is a newer version: 2.0.0
Show newest version
package com.github.gfx.android.powerassert

import com.android.build.gradle.AppExtension
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.LibraryPlugin
import com.android.build.gradle.api.ApplicationVariant
import com.android.build.gradle.api.LibraryVariant
import com.android.build.gradle.api.TestVariant
import com.android.builder.model.BuildType
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project

// see http://www.gradle.org/docs/current/userguide/custom_plugins.html

public class PowerAssertPlugin implements Plugin {
    public static final String TAG = 'PowerAssert'
    public static final int VERBOSE = Integer.valueOf(System.getenv('POWERASSERT_VERBOSE') ?: '0')

    static boolean empower = true

    /**
     * If you call setEmpower(false), this plugin enable assertions but
     * does not add extra information around assertions.
     * Use this if you have problems in android-power-assert-plugin.
     * @param enabled
     */
    public static void setEmpower(boolean enabled) {
        empower = enabled
    }

    @Override
    void apply(Project project) {
        checkAndroidPlugin(project)

        BaseExtension android = project.android
        assert android != null;

        android.lintOptions.disable "Assert" // assertions are now reliable

        android.packagingOptions {
            // exclude possibly-conflicting files in commons-lang3
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/LICENSE.txt'
        }

        android.buildTypes.all { BuildType buildType ->
            if (isAssertionsEnabled(buildType)) {
                project.dependencies."${buildType.name}Compile"(Empower.DEPENDENCIES)
            }
        }

        if (android instanceof AppExtension) {
            prepareAppExtension(android)
        } else if (android instanceof LibraryExtension) {
            prepareLibraryExtension(android)
        } else {
            throw new GradleException("Unknown extension: ${android}");
        }
    }

    static void prepareAppExtension(AppExtension android) {
        assert android.applicationVariants != null
        android.applicationVariants.all { ApplicationVariant variant ->
            if (isAssertionsEnabled(variant.buildType)) {
                assert variant.javaCompiler != null
                variant.javaCompiler.doLast {
                    def empower = new Empower(project)
                    empower.addClassPaths([variant.javaCompiler.destinationDir])
                    empower.addClassPaths(variant.apkLibraries)

                    empower.process(variant)
                }
            }
        }

        assert android.testVariants != null
        android.testVariants.all { TestVariant testVariant ->
            assert testVariant.javaCompiler != null
            testVariant.javaCompiler.doLast {
                def variant = testVariant.testedVariant as ApplicationVariant
                def empower = new Empower(project)
                empower.addClassPaths([variant.javaCompiler.destinationDir])
                empower.addClassPaths(variant.apkLibraries)

                empower.addClassPaths([testVariant.javaCompiler.destinationDir])
                empower.addClassPaths(testVariant.apkLibraries)

                empower.process(testVariant)
            }
        }
    }

    static void prepareLibraryExtension(LibraryExtension android) {
        assert android.libraryVariants != null;
        android.libraryVariants.all { LibraryVariant variant ->
            if (isAssertionsEnabled(variant.buildType)) {
                assert variant.javaCompiler != null
                variant.javaCompiler.doLast {
                    def empower = new Empower(project)
                    empower.addClassPaths(variant.testVariant.apkLibraries)
                    empower.addClassPaths([variant.javaCompiler.destinationDir])

                    empower.process(variant)
                }
            }
        }
        android.testVariants.all { TestVariant testVariant ->
            assert testVariant.javaCompiler != null
            testVariant.javaCompiler.doLast {
                def variant = testVariant.testedVariant as LibraryVariant
                def empower = new Empower(project)
                empower.addClassPaths(testVariant.apkLibraries)
                empower.addClassPaths([variant.javaCompiler.destinationDir])

                empower.addClassPaths([testVariant.javaCompiler.destinationDir])
                empower.process(testVariant)
            }
        }
    }

    static boolean isAssertionsEnabled(BuildType variant) {
        // TODO: make it customizable
        return variant.debuggable
    }

    static void checkAndroidPlugin(Project project) {
        if (!(project.plugins.hasPlugin(AppPlugin) || project.plugins.hasPlugin(LibraryPlugin))) {
            throw new GradleException('No android plugin detected')
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy