invertase / flutterfire_cli

A CLI to help with using FlutterFire in your Flutter applications.
Apache License 2.0
164 stars 47 forks source link

[bug]: Flutter changed how new Android project is structured and CLI configure doesn't support that #251

Closed DanPetras closed 5 months ago

DanPetras commented 6 months ago

Is there an existing issue for this?

CLI Version

0.2.7

Firebase Tools version

13.0.1

Flutter Doctor Output

[✓] Flutter (Channel stable, 3.16.3, on macOS 14.2.1 23C71 darwin-arm64, locale en-SK) • Flutter version 3.16.3 on channel stable at /Applications/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision b0366e0a3f (2 weeks ago), 2023-12-05 19:46:39 -0800 • Engine revision 54a7145303 • Dart version 3.2.3 • DevTools version 2.28.4

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/***/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15A507 • CocoaPods version 1.14.3

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] IntelliJ IDEA Community Edition (version 2023.1.3) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.85.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.78.0

[✓] Connected device (3 available) • macOS (desktop) • macos • darwin-arm64 • macOS 14.2.1 23C71 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 120.0.6099.109

[✓] Network resources • All expected network resources are available.

• No issues found!

Description

The default flutter project structure for Android changed from the previous format. The CLI doesn't change the gradle files when running the configure command. I would expect CLI to at least generate warning / error when it can't add the required plugins.

I added comments where I'd expect CLI to add required plugins.

Also worth noting is that in default project created in Android Studio, the plugins { id "some.plugin" version "x.y.z" apply false ... } is located in the top module build.gradle file but flutter puts it in the settings.gradle, so it seems that it can work in both locations.

Top module build.gradle file:

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

app module build.gradle file:

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
    # I would expect firebase plugins to be added here <---------------------------------
}

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'
}

android {
    namespace "com.example.app"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        applicationId "com.example.app"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        debug {
            versionNameSuffix 'd'
        }
        release {
            minifyEnabled true
            shrinkResources true
        }
    }
}

flutter {
    source '../..'
}

dependencies {}

settings.gradle file:

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }
    settings.ext.flutterSdkPath = flutterSdkPath()

    includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }

    plugins {
        id "dev.flutter.flutter-gradle-plugin" version "1.0.0" apply false
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    # I would expect firebase plugins to be added here with "apply false" <---------------------------------
}

include ":app"

Steps to reproduce

  1. Run flutter create reproduce_bug --org "com.test"
  2. Run cd reproduce_bug
  3. Run flutter pub add firebase_core (installed version is 2.1.1)
  4. Run flutterfire configure
  5. Select the firebase project
  6. Select platform android and ios

Expected behavior

Gradle files are updated, adding required google-services / firebase plugins.

Screenshots

No response

Additional context and comments

No response

DanPetras commented 6 months ago

Reference https://docs.flutter.dev/release/breaking-changes/flutter-gradle-plugin-apply

russellwheatley commented 6 months ago

@DanPetras - Have you tried the latest dev version? This contains the gradle fix I believe. The dev release will be general release at some point in the new year.

dart pub global activate flutterfire_cli 0.3.0-dev.19 --overwrite
DanPetras commented 6 months ago

I'll try in couple of days, thanks for the info.

DanPetras commented 6 months ago

@russellwheatley I did run ~/.pub-cache/bin/flutterfire configure but it did not update the gradle files. Output of ~/.pub-cache/bin/flutterfire --version is 0.3.0-dev.19. It did add upload crashlytics symbols to iOS but Android gradle files are unchanged. I also ran flutterfire reconfigure but with the same result.

Here are attached my current gradle files. (I'm using gradle 8.3 if that matters) gradle_files.zip

russellwheatley commented 6 months ago

It seems to me you've edited your build.gradle files from default flutter create .. You need

android/build.gradle to have the following dependencies script:

buildscript {
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

See if it changes now and let me know. Thanks.

DanPetras commented 6 months ago

You are right, I just went ahead and used newer syntax not yet used by flutter create in version 3.16. After rolling the change back, the flutterfire updated both build.gradle files 👍

It seems that future (next?) release of flutter will also use this new syntax (see https://github.com/flutter/flutter/issues/135392 Flutter master section) in that case the change will need to be applied also to settings.gradle file as I proposed in the first entry.

russellwheatley commented 6 months ago

Thanks for the heads up, @DanPetras. I will get this updated ahead of the proposed changes 👍