0

Does anyone know of a way to template build.gradle files as modules are created? Right now the issue I have is that I want to be able to have the module's build.gradle contain only a small amount of information since I'd be setting the shared configuration within the top level build.gradle file. I have a buildSrc folder setup to centralize the versioning as well.

Top-level build.gradle file:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath Deps.tools_gradleandroid
        classpath Deps.tools_kotlin
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10'
    }
}

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            android {
                buildToolsVersion Config.buildTools
                compileSdkVersion Config.compileSdk

                defaultConfig {
                    minSdkVersion Config.minSdk
                    targetSdkVersion Config.targetSdk
                    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
                }

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

                compileOptions {
                    sourceCompatibility Config.javaVersion
                    targetCompatibility Config.javaVersion
                }

                kotlinOptions {
                    jvmTarget = Config.javaVersion.toString()
                }

            }
        }
    }
}

A module's expected build.gradle file:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    defaultConfig {
        applicationId "com.company.expectedmodule"
    }
}

dependencies {
    implementation Deps.androidx_core
    implementation Deps.androidx_appcompat
    implementation Deps.androidx_material
    testImplementation Deps.testlib_junit
    androidTestImplementation Deps.testandroidx_junit
    androidTestImplementation Deps.testandroidx_espressocore
}

What the build.gradle file looks like when I create a 'New Module':

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.company.newmodule"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.0'
    implementation 'com.google.android.material:material:1.6.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

EDIT: For sake of simplicity I've moved the dependencies bracket to the top-level build.gradle file and also changed the modules to be libraries instead and reduced the code of a library module to below:

plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
}

android {

}

1 Answers1

1

recommended way of sharing build logic in Gradle is by using convention plugins. Like in this sample.

Also precompiled script plugins can be used to write plugins in syntax similar to regular build scripts.

Dave562
  • 211
  • 1
  • 2