I create an project based the wizard Empty Compose Activity of Android Studio Chipmunk | 2021.2.1 Patch 1, and the wizard generate two build.gradle files, you can see Code A and Code B.
Code C and Code D are two build.gradle files from an old project.
1: What does apply false
in id 'com.android.library' version '7.2.0' apply false
of Code A mean?
2: I find that the plugins section exist both Code A and Code B which have almost the same code, why?
3: I find there is a dependencies section in Code C, is it obsolete ?
4: Is repositories section in Code C obsolete ?
Code A: Sample build.gradle (Project)
buildscript {
ext {
compose_version = '1.1.0-beta01'
}
}
plugins {
id 'com.android.application' version '7.2.0' apply false
id 'com.android.library' version '7.2.0' apply false
id 'org.jetbrains.kotlin.android' version '1.5.31' apply false
}
Code B: Sample build.gradle (Module)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "info.dodata.myapplication"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
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'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes = '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
...
}
Code C: My build.gradle (Project)
buildscript {
ext {
compose_version = '1.2.0'
hilt_version = '2.42'
}
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"
classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
}
Code D: build.gradle (Module)
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
id 'kotlin-parcelize'
}
android {
compileSdk 32
flavorDimensions "default"
namespace 'info.dodata.soundmeter'
defaultConfig {
applicationId "info.dodata.soundmeter"
minSdk 23
targetSdk 32
versionCode 3
versionName "1.03"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
allWarningsAsErrors = false
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes = '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
...
CodePudding user response:
Android Studio Chipmunk creates a different structure for Gradle files, with a settings.gradle
for repositories and a build.gradle
(project level) for plugins. In short, this new structure just makes the Gradle files
cleaner.
With Gradle
is common to see 2 (or more) ways to do the same thing. Comparing code A
with code C
is the perfect example. Both codes do the same thing but with a different structure in code and file.
Now talking about apply false
, it's basically literally. You declare a plugin, with a version, and say to not apply it immediately (in entire project, since it's a build.gradle from project). But you apply what you need only in the module that you are going to use this plugin, for example, in the build.gradle
from app module
.
You can take a look here for a complete description.
Back to the "new structure" generated by Android Studio with plugins, sometimes you will need to create a dependencies
block inside buildscript
to use the "old" classpath
instead a plugin id
because currently not all libraries have the plugin to be used.