• Bartek Pacia's avatar
    Expose versionCode and versionName from local.properties in FlutterExtension (#141417) · fd827e3a
    Bartek Pacia authored
    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.
    fd827e3a
Name
Last commit
Last update
.github Loading commit data...
.vscode Loading commit data...
bin Loading commit data...
dev Loading commit data...
examples Loading commit data...
packages Loading commit data...
.ci.yaml Loading commit data...
.gitattributes Loading commit data...
.gitignore Loading commit data...
AUTHORS Loading commit data...
CODEOWNERS Loading commit data...
CODE_OF_CONDUCT.md Loading commit data...
CONTRIBUTING.md Loading commit data...
LICENSE Loading commit data...
PATENT_GRANT Loading commit data...
README.md Loading commit data...
TESTOWNERS Loading commit data...
analysis_options.yaml Loading commit data...
dartdoc_options.yaml Loading commit data...
flutter_console.bat Loading commit data...