Home > OS >  How to use Android Gradle without multi-project
How to use Android Gradle without multi-project

Time:10-18

I would like to create an android build with a single build.gradle file. How to do that?

CodePudding user response:

You need at least two build.gradles, a project-level one and a module-level one. They are not the same. You can omit neither of them.

CodePudding user response:

It actually is possible.

The whole problem when trying to create an Android build with a single build.gradle comes from the fact, that a plugins block of the top-level build.gradle can only use plugins from Gradle plugin portal and cannot address anything from buildscript's classpath.

However, there is a pluginManagement block you can add to settings.gradle, that can be used to modify how plugins are resolved.

A working example:

// settings.gradle.kts

rootProject.name = "android_app"

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.name.startsWith("com.android")) {
                useModule("com.android.tools.build:gradle:${requested.version}")
            }
        }
    }
}
// build.gradle.kts

plugins {
    id ("com.android.application") version "4.2.2"
}

group = "pl.gieted.android_app"
version = "1.12-SNAPSHOT"

android {
    compileSdkVersion(31)
    defaultConfig {
        applicationId = group.toString()
        minSdkVersion(24)
        targetSdkVersion(31)
        versionName = version.toString()
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
}

CodePudding user response:

As hata answered , you need at least two .

Every project have a top level gradle file and modules gradle file\files .

the build.gradle (Project:My-app) file in the root folder usually contains the common configuration for all other modules .

while modules (Module:app) are isolated piece of the bigger project , they work together to form the whole project . and the build.gradle file will only apply to this module .

If you only want to have one module such as app , the advantages of the root build.gradle is not shown clearly , because you can configure everything in the module gradle build file , even overwrite the root gradle file .

BUT if your project have 4 moudles and it happened that they have the same dependency X , once you want to change this , you will need to change this individually with 4 modules build.gradle .

  • Related