You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Flutter项目升级Android Studio后的Gradle迁移及版本升级求助

Flutter项目升级Android Studio后的Gradle迁移及版本升级求助

我完全懂你升级Android Studio Meerkat后踩坑的崩溃感——尤其是Flutter项目碰到Java、Gradle版本不兼容,还把项目搞崩好几次,太闹心了!结合你贴的配置文件,我来一步步帮你把Gradle迁移到适配新版本的结构,同时解决那些兼容问题。

先明确核心版本对应关系

Android Studio 2024.3.2(Meerkat)对应的Android Gradle Plugin (AGP)版本建议用8.2.x,对应的Gradle版本要匹配成8.6,同时需要Java 17(AGP 8.x强制要求),Flutter版本要升级到3.16+才能适配这套组合。


步骤1:先升级Flutter到兼容版本

打开终端运行:

flutter upgrade

确保Flutter版本在3.16以上,旧版本的Flutter Gradle插件不支持新AGP。


步骤2:修改settings.gradle(根目录)

替换成以下内容,更新插件版本并添加依赖仓库管理:

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
    }()
    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

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

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.2.2" apply false
    id "org.jetbrains.kotlin.android" version "1.9.22" apply false
    id "com.google.gms.google-services" version "4.4.2" apply false
}

include ":app"

步骤3:简化根目录build.gradle

删掉旧的allprojects块,替换成更简洁的新版本结构:

plugins {
    id "com.google.gms.google-services" version "4.4.2" apply false
}

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

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

步骤4:更新app/build.gradle

主要调整Java版本到17(AGP 8.x强制要求),并明确编译版本:

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
    id "com.google.gms.google-services"
}

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 keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    namespace = "com.flutterflow.dent_shop_manager"
    compileSdk = 34
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = "17"
    }

    defaultConfig {
        applicationId "com.flutterflow.hailrepair"
        minSdkVersion 26
        targetSdkVersion 34
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
        debug {
            signingConfig signingConfigs.debug // 拆分配置避免同步冲突
        }
    }
}

flutter {
    source = "../../"
}

注意:我把你原来buildTypes里重复的signingConfig拆分成debug和release各自的配置,避免同步时的冲突问题。


步骤5:升级Gradle wrapper版本

修改gradle/wrapper/gradle-wrapper.properties里的distributionUrl:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

步骤6:配置Android Studio的JDK版本

打开Android Studio,依次点击File > Project Structure > SDK Location,在JDK location选择Embedded JDK (recommended),这个是Android Studio自带的JDK 17,完美适配AGP 8.x。


步骤7:最后验证和修复

  1. 运行flutter clean清理旧的构建缓存
  2. 运行flutter pub get更新依赖
  3. 尝试运行Android项目

如果还是遇到依赖问题,比如某些Flutter插件不支持Java 17,去pub.dev搜索对应插件的最新版本,升级到支持AGP 8.x的版本即可。


内容来源于stack exchange

火山引擎 最新活动