Easylauncher Gradle Plugin

repository·master·Indexed 19 days ago

https://github.com/usefulness/easylauncher-gradle-plugin

A Gradle plugin for Android that allows developers to dynamically modify launcher icons for different build variants. It supports adding colored ribbons, image overlays, and custom text/fonts using simple Gradle rules. The plugin includes specialized filters like ChromeLikeFilter and ColorRibbonFilter, and provides a Canvas API for implementing custom icon filter drawing with support for adaptive icons.

Tokens
4.2K
Snippets
14
Records
18
Agent score
63%

What's inside easylauncher-gradle-plugin

  1. Install the Easylauncher Gradle Plugin

    master

    Apply the plugin to your application module's build.gradle file.

    If you encounter a Could not resolve all artifacts error (specifically Could not find com.android.tools.build:gradle:x.x.x), apply the plugin to the root project first to ensure correct dependency resolution, then apply it to your application module.

    Alternatively, you can use a Gradle Version Catalog.

    // Standard application module setup
    // in app/build.gradle
    plugins {
        id "com.starter.easylauncher" version "${{version}}"
    }
    
    // Troubleshooting: Root project setup for dependency resolution
    // in root project's build.gradle 
    buildscript {
        repositories.google()
    }
    plugins {
        id "com.starter.easylauncher" version "${{version}}" apply false
    }
    
    // Troubleshooting: Application module setup after root setup
    // in app/build.gradle 
    plugins {
        id "com.starter.easylauncher"
    }
    
    // Using Version Catalog
    // in libs.versions.toml
    [plugins]
    starter-easylauncher = { id = "com.starter.easylauncher", version.ref = "easylauncher" }
  2. Configure Easylauncher filters and options

    master

    The easylauncher configuration block allows you to customize how launcher icons are modified across different build types, product flavors, and specific variants.

    Hierarchy and Precedence:

    • productFlavors: Filters defined here are added to filters defined in buildTypes.
    • buildTypes: Filters defined here are added to filters defined in productFlavors.
    • variants: Filters defined here override all other filters defined for that specific variant.

    Key Options:

    • defaultFlavorNaming: If true, uses the flavor name for default ribbons instead of the build type name.
    • showWarnings: If true, the plugin shows warnings during configuration time.
    • enable: Set to false to disable all filters for a specific configuration.
    • iconNames: A list of icon resource names (e.g., ["@mipmap/custom_icon"]). If provided, the plugin disables automatic discovery and only applies filters to these specific icons.
    easylauncher {
        defaultFlavorNaming = true
        showWarnings = true
        iconNames = ["@mipmap/custom_launcher_icon"]
    
        productFlavors {
            register("qa") {
                filters redRibbonFilter()
            }
        }
    
        buildTypes {
            register("beta") {
                filters = [
                        customRibbon(ribbonColor: "#0000FF"),
                        overlayFilter(file("example-custom/launcherOverlay/beta.png"))
                ]
            }
            register("canary") {
                enable = false
            }
        }
    
        variants {
            register("productionDebug") {
                filters = orangeRibbonFilter("custom")
            }
        }
    }
  3. Use the Canvas API for icon filter drawing

    master

    The Canvas class provides a managed drawing surface for implementing custom icon filters. It wraps a Graphics2D object and handles padding calculations to ensure drawing operations respect the image boundaries, especially when dealing with adaptive icons.

    Key Concepts

    • Viewport vs. Full Size: For adaptive icons, the Canvas calculates a viewport (the inner 72dp of a 108dp icon) and applies padding to the remaining area. For legacy icons, the viewport is the full image size.
    • Lifecycle: A Canvas is intended to be used once. After executing drawing operations, the underlying graphics context is disposed of automatically.
    • Adaptive Scaling: When adaptive = true is passed during creation, the canvas uses an ADAPTIVE_SCALE (72/108f) to define the drawable area.
    // Example of creating a canvas for an adaptive icon
    val canvas = Canvas(image, adaptive = true)
    
    // Perform drawing operations
    // Note: The 'use' method is internal, so you typically interact with the graphics
    // via the provided Canvas properties or within the plugin's filter lifecycle.
  4. Troubleshoot font loading errors

    master

    If you encounter the error Problem reading font data. (common in Docker or minimalistic environments), it means the system lacks font support.

    On Debian/Ubuntu-based systems, install the required packages using:

    apk add --no-cache freetype fontconfig ttf-dejavu

    (Note: The command provided in the documentation uses apk, which is for Alpine Linux. Adjust for your specific environment if necessary.)

    apk add --no-cache freetype fontconfig ttf-dejavu
  5. Use Overlay and Ribbon filters

    master

    Filters are used to modify the launcher icon.

    Overlay Filter: Applies an image overlay to the icon.

    • overlayFilter(file("path/to/image.png"))

    Standard Ribbon Filters: Predefined colored ribbons:

    • grayRibbonFilter()
    • greenRibbonFilter()
    • yellowRibbonFilter()
    • orangeRibbonFilter()
    • redRibbonFilter()
    • blueRibbonFilter()
    easylauncher {
        buildTypes {
            register("debug") {
                filters = [overlayFilter(file("overlay.png")), redRibbonFilter()]
            }
        }
    }
  6. Use Chrome-like filters

    master

    Chrome-like filters provide a specific UI style for the icon modification.

    Available Options:

    • chromeLike(): Default appearance.
    • chromeLike(label: "...", ribbonColor: "...", labelColor: "..."): Custom appearance.
    • chromeLike(label: "...", font: "..."): Custom font.
    • chromeLike(label: "...", gravity: "...", labelPadding: ...): Custom gravity and padding.
    • chromeLike(label: "...", overlayHeight: ..., textSizeRatio: ...): Custom alignment using overlay height and text size ratio.
    easylauncher {
        buildTypes {
            register("qa") {
                filters = [chromeLike(label: "QA", ribbonColor: "#FF00FF", labelColor: "#FFFFFF")]
            }
        }
    }
  7. Customize ribbons with Advanced Ribbon filters

    master

    The customRibbon function allows for highly granular control over the ribbon's appearance and placement.

    Available Parameters:

    • ribbonColor: Hex string for background color (e.g., "#6600CC").
    • label: String for the text displayed on the ribbon.
    • labelColor: Hex string for the text color.
    • position: Gravity/alignment. Options: "top", "bottom", "topLeft", "topRight".
    • textSizeRatio: Relative text size (e.g., 0.2).
    • font: Either a locally installed font name (e.g., "ComicSansMs") or a path to a font file (e.g., file("fonts/CustomFont.ttf")).
    easylauncher {
        buildTypes {
            register("beta") {
                filters = [
                    customRibbon(label: "BETA", ribbonColor: "#0000FF", labelColor: "#FFFFFF", position: "top"),
                    customRibbon(position: "bottom", textSizeRatio: 0.2)
                ]
            }
        }
    }
  8. Configure EasyLauncher via the EasyLauncherExtension

    master

    The easylauncher plugin is configured using the EasyLauncherExtension. This extension allows you to define variant-specific configurations, including enabled status, filters (like ribbons or chrome-like filters), and custom icon names.

    Key Configuration Concepts:

    • Variant Matching: The plugin looks for configurations in the variants block that match the Android variant name. If no specific variant configuration is found, it attempts to find matches within productFlavors or buildTypes.
    • Icon Names: You can define iconNames globally for the entire project or specifically within a variant configuration. Variant-specific names take precedence.
    • Automatic Ribbon: If no filters are explicitly provided for a debuggable variant, the plugin automatically adds a greenRibbonFilter using either the flavor name or the build type (depending on your isDefaultFlavorNaming setting).
  9. Create a Canvas from a BufferedImage

    master

    You can instantiate a Canvas by passing a BufferedImage. This automatically calculates the necessary paddings based on whether the icon is adaptive or legacy.

    • image: The source BufferedImage.
    • adaptive: A Boolean. If true, the canvas viewport is scaled to the inner 72dp (for a 108dp icon). If false, the viewport matches the full image dimensions.

    Properties available on the Canvas instance:

    • width: The width of the drawable viewport.
    • height: The height of the drawable viewport.
    • fullWidth: The total width including paddings.
    • fullHeight: The total height including paddings.
    • paddingTop, paddingBottom, paddingLeft, paddingRight: The calculated padding values.
    val canvas = Canvas(image, adaptive = true)
    println("Drawable width: ${canvas.width}")
    println("Total width: ${canvas.fullWidth}")
  10. Use the easylauncher<VariantName> task

    master

    For every enabled Android variant, the plugin registers a specific Gradle task following the pattern easylauncher<CapitalizedVariantName>.

    For example, if you have a variant named debug, the task will be easylauncherDebug. This task is responsible for processing manifests, resource directories, and filters to generate the modified assets used by the plugin.

    ./gradlew easylauncherDebug