Expose versionCode and versionName from local.properties in FlutterExtension (#141417)
This PR has no issue. I got this cool idea and decided to quickly try it out, and it works.
### Summary
This will allow Flutter Developers to have less code in their Android Gradle buildscripts.
```diff
plugins {
id "com.android.application"
id "dev.flutter.flutter-gradle-plugin"
id "kotlin-android"
}
-def localProperties = new Properties()
-def localPropertiesFile = rootProject.file("local.properties")
-if (localPropertiesFile.exists()) {
- localPropertiesFile.withReader("UTF-8") { reader ->
- localProperties.load(reader)
- }
-}
-
-def flutterVersionCode = localProperties.getProperty("flutter.versionCode")
-if (flutterVersionCode == null) {
- flutterVersionCode = "1"
-}
-
-def flutterVersionName = localProperties.getProperty("flutter.versionName")
-if (flutterVersionName == null) {
- flutterVersionName = "1.0"
-}
-
-def keystorePropertiesFile = rootProject.file("keystore.properties")
-def keystoreProperties = new Properties()
-
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
android {
applicationId "pl.baftek.discoverrudy"
minSdk 21
targetSdk 34
- versionCode flutterVersionCode.toInteger()
- versionName flutterVersionName
+ versionCode flutter.versionCode()
+ versionName flutter.versionName()
}
```
The boilerplate that loads 'local.properties' can live in Flutter Gradle Plugin.
### Concerns
I was worried about lifecycle/ordering issues, so I tested it.
To Flutter Gradle Plugin, I added:
```diff
class FlutterPlugin implements Plugin<Project> {
//...
@Override
void apply(Project project) {
+ project.logger.quiet("Start applying FGP")
// ...
}
}
```
and to my `android/app/build.gradle` I added:
```diff
android {
+ logger.quiet("Start evaluating android block")
namespace "pl.bartekpacia.awesomeapp"
compileSdk 34
defaultConfig {
applicationId "pl.baftek.discoverrudy"
minSdk 21
targetSdk 34
versionCode flutter.versionCode()
versionName flutter.versionName()
}
```
Gradle first applies the plugins (which sets versionCode and versionName on FlutterExtension), and then it executes the `android {}` extension block:
```
$ ./gradlew :app:assembleDebug
> Configure project :app
Start applying FGP
Start evaluating android block
BUILD SUCCESSFUL in 2s
383 actionable tasks: 10 executed, 373 up-to-date
```
So ordering is fine.
This commit is contained in:
@@ -15,16 +15,6 @@ if (flutterRoot == null) {
|
||||
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
|
||||
}
|
||||
|
||||
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
|
||||
if (flutterVersionCode == null) {
|
||||
flutterVersionCode = '1'
|
||||
}
|
||||
|
||||
def flutterVersionName = localProperties.getProperty('flutter.versionName')
|
||||
if (flutterVersionName == null) {
|
||||
flutterVersionName = '1.0'
|
||||
}
|
||||
|
||||
apply plugin: 'com.android.application'
|
||||
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
|
||||
|
||||
@@ -41,8 +31,8 @@ android {
|
||||
applicationId "io.flutter.examples.layers"
|
||||
minSdkVersion flutter.minSdkVersion
|
||||
targetSdkVersion flutter.targetSdkVersion
|
||||
versionCode flutterVersionCode.toInteger()
|
||||
versionName flutterVersionName
|
||||
versionCode flutter.versionCode()
|
||||
versionName flutter.versionName()
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
|
||||
Reference in New Issue
Block a user