Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update plugin to support libraries and add example lib module #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Build Example
on: [ push, workflow_dispatch ]

jobs:
build:
build_app:
runs-on: ubuntu-latest
steps:
# Checkout repository
Expand All @@ -20,3 +20,21 @@ jobs:
# Run the Gradle Build task
- name: Build Example App
run: ./gradlew clean :appexample:assemble

build_lib:
runs-on: ubuntu-latest
steps:
# Checkout repository
- name: Checkout
uses: actions/[email protected]

# Setup JDK environment
- name: Set up JDK
uses: actions/[email protected]
with:
distribution: 'zulu'
java-version: '17'

# Run the Gradle Build task
- name: Build Example Lib
run: ./gradlew clean :libexample:assemble
4 changes: 2 additions & 2 deletions appexample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.kotlinAndroid)
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
id("ch.ubique.gradle.preset")
}

Expand Down
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
plugins {
alias(libs.plugins.kotlin) apply false
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.kotlinAndroid) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
}

tasks.register("clean", Delete::class.java) {
Expand Down
5 changes: 3 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ androidx-lifecycle = "2.8.6"

[plugins]
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin"}
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
pluginPublish = { id = "com.gradle.plugin-publish", version.ref = "pluginPublish"}
androidApplication = { id = "com.android.application", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
vanniktech = { id = "com.vanniktech.maven.publish", version.ref = "vanniktech" }

[libraries]
Expand Down
1 change: 1 addition & 0 deletions libexample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
27 changes: 27 additions & 0 deletions libexample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.kotlin.android)
id("ch.ubique.gradle.preset")
}

android {
namespace = "ch.ubique.preset.example"
compileSdk = 34

defaultConfig {
minSdk = 26
targetSdk = 34

testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}

dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.lifecycle.viewmodelKtx)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ch.ubique.preset.example

object ExampleLibrary {

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ch.ubique.gradle.preset

import ch.ubique.gradle.preset.config.PresetPluginConfig
import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.LibraryExtension
import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.gradle.AppExtension
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.ProguardFiles.getDefaultProguardFile
import org.gradle.api.GradleException
import org.gradle.api.JavaVersion
Expand All @@ -19,39 +21,12 @@ abstract class PresetPlugin : Plugin<Project> {
val androidExtension = getAndroidExtension(project)
val androidComponentExtension = getAndroidComponentsExtension(project)

// Apply presets specific to applications
(androidExtension as? ApplicationExtension)?.applyAppPreset(project)

// Enable BuildConfig
androidExtension.buildFeatures.buildConfig = true

// Default dimension
androidExtension.flavorDimensions("default")

// Add flavor boolean fields to BuildConfig
androidExtension.productFlavors.configureEach { flavor ->
val sanitizedFlavorName = flavor.name.replace("[^a-zA-Z0-9_]", "_")

// default flavor dimension
flavor.dimension = "default"

// default application id suffix
flavor.applicationIdSuffix = when (flavor.name) {
"prod", "production" -> null
else -> ".$sanitizedFlavorName"
}

// flavor BuildConfig flag
val flavorFieldName = "IS_FLAVOR_${sanitizedFlavorName.uppercase()}"
// true for this flavor ...
flavor.buildConfigField("boolean", flavorFieldName, "true")
// ... false for all others
androidExtension.defaultConfig.buildConfigField("boolean", flavorFieldName, "false")
}

// Release build config
androidExtension.buildTypes.maybeCreate("release").apply {
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android.txt", project.layout.buildDirectory), "proguard-rules.pro")
}

// R8 full mode check
if (project.findProperty("android.enableR8.fullMode") != "false" && project.findProperty("android.enableR8.fullModeAllowed") != "true") {
throw IllegalArgumentException("R8 full mode is enabled. Disable it with android.enableR8.fullMode=false or allow it by setting android.enableR8.fullModeAllowed=true")
Expand Down Expand Up @@ -81,9 +56,10 @@ abstract class PresetPlugin : Plugin<Project> {
androidExtension.lintOptions.isAbortOnError = false
}

private fun getAndroidExtension(project: Project): AppExtension {
val ext = project.extensions.findByType(AppExtension::class.java)
?: throw GradleException("Android gradle plugin extension has not been applied before")
private fun getAndroidExtension(project: Project): BaseExtension {
val ext = project.extensions.findByType(BaseExtension::class.java)
?.takeIf { it is ApplicationExtension || it is LibraryExtension }
?: throw GradleException("Android gradle plugin (application or library) extension has not been applied before")
return ext
}

Expand All @@ -93,4 +69,36 @@ abstract class PresetPlugin : Plugin<Project> {
return ext
}

private fun ApplicationExtension.applyAppPreset(project: Project) {
// Default dimension
flavorDimensions += "default"

// Add flavor boolean fields to BuildConfig
productFlavors.configureEach { flavor ->
val sanitizedFlavorName = flavor.name.replace("[^a-zA-Z0-9_]", "_")

// default flavor dimension
flavor.dimension = "default"

// default application id suffix
flavor.applicationIdSuffix = when (flavor.name) {
"prod", "production" -> null
else -> ".$sanitizedFlavorName"
}

// flavor BuildConfig flag
val flavorFieldName = "IS_FLAVOR_${sanitizedFlavorName.uppercase()}"
// true for this flavor ...
flavor.buildConfigField("boolean", flavorFieldName, "true")
// ... false for all others
defaultConfig.buildConfigField("boolean", flavorFieldName, "false")
}

// Release build config
buildTypes.maybeCreate("release").apply {
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android.txt", project.layout.buildDirectory), "proguard-rules.pro")
}
}

}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ dependencyResolutionManagement {
rootProject.name = "gradle-plugin-preset-android"

include("appexample")
include("libexample")
includeBuild("preset")
Loading