Skip to content

Instantly share code, notes, and snippets.

@KevinSchildhorn
Last active May 14, 2023 23:07
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save KevinSchildhorn/4eb2093118fd6426fc7983a3950a0c99 to your computer and use it in GitHub Desktop.
Gradle Cheatsheet
/*
* A Top-level build file where you can add configuration options common to all sub-projects/modules.
*/
buildscript {
ext {
compose_ui_version = '1.2.0'
agp.version = '7.1.3'
}
dependencies {
/*
* Classpaths are an alternative method to import plugins
* Binary plugins that have been published as external jar files can be added to a project
* by adding the plugin to the build script classpath and then applying the plugin.
*/
classpath("org.jetbrains.kotlin:kotlin-serialization:1.8.10")
}
}
/*
* Where plugins are defined.
* Having the definition here in the root allows all plugin versions for the build to be defined in a single location
*/
plugins {
// To define a plugin we need to pass the id and its version
id("com.android.application")
.version( // Setting a Version to be used globally in the project
/*
* ext is used to define extra properties for the project object. This one is defined in gradle.properties
*/
extra["agp.version"] as String
)
/*
* you want to apply plugins to some or all of the subprojects in your build, but not to the root project.
* The default behavior of the plugins {} block is to immediately resolve and apply the plugins.
* But, you can use the apply false syntax to tell Gradle not to apply the plugin to the
* current project and then use the plugins {} block without the version in subprojects'
*/
.apply(false)
// Kotlin simply means we're using the `org.jetbrains.kotlin` group ID
kotlin("multiplatform").version("1.8.10").apply(false)
}
/*
* All Projects not including the root build.gradle
* Can contain any definitions wanted for all the projects
* You can define repositories and dependencies here, though it's not recommended
*/
subprojects {
/*
* Here you can define repositories for all subprojects, however it is now
* Recommended to use the dependencyResolutionManagement in the settings.gradle
*/
repositories {
google()
mavenLocal()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") // From A custom Maven repository
}
}
/*
* All Projects including the root build.gradle
* Can contain any definitions wanted for all the projects
* You can define repositories and dependencies here, though it's not recommended
*/
allprojects {
repositories {
mavenCentral()
}
group = "com.kevinschildhorn.gradlecheatsheet"
}
/*
* Applying a plugin to a project allows the plugin to extend the project’s capabilities. It can do things such as:
* Extend the Gradle model (e.g. add new DSL elements that can be configured)
* Configure the project according to conventions (e.g. add new tasks or configure sensible defaults)
* Apply specific configuration (e.g. add organizational repositories or enforce standards)
*/
plugins {
id("com.android.library") // To define a plugin we need to pass the group id and name
kotlin("multiplatform") // Kotlin simply means we're using the `org.jetbrains.kotlin` group ID
}
/*
* The kotlin extension, where we can define our targets and sourcesets for KMP
* Docs: https://kotlinlang.org/docs/multiplatform-dsl-reference.html
*/
kotlin {
/*
* Targets are what platforms or environments you want to build to
* Docs: https://kotlinlang.org/docs/multiplatform-dsl-reference.html#targets
*/
android()
ios {
binaries { // Configuring the binaries (https://kotlinlang.org/docs/multiplatform-dsl-reference.html#binaries)
framework { // Configuring the Objective-C framework
baseName = "shared"
}
}
}
/*
* Source Sets are where we define dependencies for platform specific codebases
* Includes their resources, dependencies, and language settings.
* Docs: https://kotlinlang.org/docs/multiplatform-dsl-reference.html#source-sets
*/
sourceSets {
/*
* One Method of defining a sourceset if it's source set is created outside of the build script
* Docs: https://docs.gradle.org/current/userguide/kotlin_dsl.html#type-safe-accessors
*/
commonMain {
dependencies {
}
}
/*
* Another Method of defining a sourceset, a string based lookup
*/
named("iosMain") {
dependencies {
/*
* APIs are exported to consumers, that is to say found on their compile classpath.
*/
api("androidx.appcompat:appcompat:1.6.1")
/*
* Implementations are only for this module, other modules implementing this module will not include this.
*/
implementation(compose.preview)
}
}
/*
* Another Method of defining a sourceset, the preferred method.
* Can be used to easily add as dependencies to other sourcesets
*/
val androidMain by getting {
dependencies {
implementation(project(":other")) // Adding local module
}
}
}
}
/**
* The android block is where you configure all your Android-specific
* build options.
*/
android {
/*
* compileSdk specifies the Android API level Gradle should use to
* compile your app. This means your app can use the API features included in
* this API level and lower.
*/
compileSdk = 33
/*
* The defaultConfig block encapsulates default settings and entries for all
* build variants and can override some attributes in main/AndroidManifest.xml
* dynamically from the build system. You can configure product flavors to override
* these values for different versions of your app.
*/
defaultConfig {
minSdk = 26
targetSdk = 33
}
/*
* Where you can define what versions of the JDK you want to use
* Gradle Plugin Requires a minimum version of java, so can be defined here
*/
compileOptions {
/*
* The Version we're using for building,
* if you only have a certain type or want certain benefits of newer Java Versions
*/
sourceCompatibility = JavaVersion.VERSION_11
/*
* The version we're using for generating
* by default equals a current Gradle's JDK version
* Docs: https://kotlinlang.org/docs/gradle-configure-project.html#gradle-java-toolchains-support
*/
targetCompatibility = JavaVersion.VERSION_11
}
sourceSets {
named("androidMain") {
manifest.srcFile("src/androidMain/AndroidManifest.xml")
res.srcDirs("src/androidMain/res")
}
}
}

Flash Cards

Here is some quick references to code surrounding gradle

Blocks: Full Coverage

  • plugins - Where you define the plugins you want to use. (More Info)
  • repositories - A block that tells gradle where to look for items. (More Info)
  • pluginManagement - Where you define the projects plugins, contains plugins and repositories blocks. (More Info)
  • BuildScript - (Legacy for plugin) Defines repositories and dependencies for adding plugins. Can also be used to add variables such as versions that are used across the project. (More Info)

Plugins:

  • id("id") - Add a plugin with a group ID and a name (ex: "com.android.library"). (More Info)
  • kotlin("id") - Kotlin simply means we're using the "org.jetbrains.kotlin" group ID
  • version("version") - Setting the version of the plugin, required at some module level. (More Info)
  • apply(boolean) - Whether to apply the plugin or not. Useful if you want to define the plugin in the root module, but only use it in other certain submodules (More Info)

Legacy:

  • classpath - A dependency for the plugins you want to add, classpath is used in the buildscript block
  • apply("id") - The legacy way to add a plugin

Repositories:

/*
* This is where you configure the plugins used in this project.
* You can define the plugins and their versions here, as well as the repositories they're pulled from
* (by default gradlePluginPortal is only repository and no plugins are set)
*/
pluginManagement {
/*
* Repositories are where you pull plugins from.
* If you don't set this block the only repository will be the `gradlePluginPortal`
* If you do set this block, you will need to set the `gradlePluginPortal` otherwise it will be omitted
*/
repositories {
gradlePluginPortal() // The Default Gradle plugin portal (https://plugins.gradle.org/)
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
google()
}
}
/*
* The dependencyResolutionManagement block is where you configure the
* repositories and dependencies used by all modules in your project.
* However, you should configure module-specific dependencies in each module-level build.gradle file.
* Defining repositories here is recommended as of gradle 7.
*/
dependencyResolutionManagement {
/*
* Enforces how Dependencies are declared
* FAIL_ON_PROJECT_REPOS - any repository declared directly in a project will trigger a build error.
* [More Info](https://testfairy.com/blog/how-to-prefer-settings-gradle-repositories-over-build-gradle/)
*/
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
/*
* Maven is a popular open-source build tool developed by the Apache Group to publish and deploy libraries
* A Maven Repository is a location where maven artifacts are stored and managed.
* They are a specific format of artifact, containing both POMs and artifact files.
*/
mavenCentral() // From Central Maven Repository (https://repo1.maven.org/maven2/)
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev") // From A custom Maven repository
mavenLocal()
}
}
/*
* rootProject.name assigns a name to the build, which overrides the default behavior of naming the build after the directory it’s in.
* It’s recommended to set a fixed name as the folder might change if the project
*/
rootProject.name = "demo"
/*
* Including submodules named `common` and `android
*/
include(":common", ":android")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment