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

如何修复Flutter运行警告:应用使用旧版Android embedding将被弃用

Hey there, let’s tackle that Android embedding deprecation warning together—it’s a common fix once you know the steps, and switching to v2 will get your app aligned with Flutter’s current standards.

Step-by-Step Migration to Android Embedding v2

1. Update Flutter to the Latest Stable Version

First, make sure you’re on a recent Flutter stable release (v2.0+ is required for embedding v2). Run this in your terminal:

flutter upgrade

2. Update Android Build Configurations

Modify android/build.gradle

Open the top-level build file and update these values to meet v2 requirements:

  • Set a compatible Kotlin version (v1.5.31 or higher works; we’ll use a recent one here):
    ext.kotlin_version = '1.7.10'
    
  • Update the Android Gradle Plugin version to 7.0.0 or higher:
    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
    

Modify android/app/build.gradle

  • Bump your compile and target SDK versions to at least 33 (matches Flutter’s current defaults):
    android {
        compileSdkVersion 33
    
        defaultConfig {
            minSdkVersion 16  // v2 requires min SDK 16+
            targetSdkVersion 33
        }
    }
    

3. Update Your MainActivity Class

Replace your existing MainActivity with the v2-compatible version:

For Kotlin Projects (android/app/src/main/kotlin/[your-package]/MainActivity.kt)

import io.flutter.embedding.android.FlutterActivity

class MainActivity : FlutterActivity() {
}

For Java Projects (android/app/src/main/java/[your-package]/MainActivity.java)

import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {
}

4. Adjust AndroidManifest.xml

  • Ensure your AndroidManifest.xml has the flutterEmbedding meta-data set to 2 (this tells Flutter to use v2 embedding):
    <application
        android:label="your_app_name"
        android:name="android.app.Application"
        android:icon="@mipmap/ic_launcher">
        <!-- Add this meta-data line -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
        
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Keep your existing intent filter here -->
        </activity>
    </application>
    
  • Remove any old v1-specific entries like io.flutter.app.android.SplashScreenUntilFirstFrame meta-data if present.

5. Clean and Rebuild Your Project

Run these commands to clear old build artifacts and ensure everything syncs correctly:

flutter clean
flutter pub get
flutter run

Troubleshooting Tip

If you hit errors with third-party plugins, most likely they’re using the old v1 embedding. Check the plugin’s pub page and update it to the latest version—popular plugins have all been updated to support v2 by now.

内容的提问来源于stack exchange,提问作者Javeed Ishaq

火山引擎 最新活动