flutter 升级到 1.12.x android 启动时会黑屏解决办法

  1. 直接用flutter 1.12.x SDK创建的项目

    需要修改AndroidManifest.xml文件,修改如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    <application
    android:name="io.flutter.app.FlutterApplication"
    android:label="ifeiyv"
    android:icon="@mipmap/ic_launcher">
    <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">
    //
    <meta-data
    android:name="io.flutter.embedding.android.SplashScreenDrawable"
    android:resource="@drawable/launch_background"
    />
    <meta-data
    android:name="io.flutter.embedding.android.NormalTheme"
    android:resource="@drawable/launch_background"
    />
    <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
    </activity>
    <!-- Don't delete the meta-data below.
    This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
    <meta-data
    android:name="flutterEmbedding"
    android:value="2" />
    </application>

    如果存在meta-dataandroid:name="io.flutter.app.android.SplashScreenUntilFirstFrame",删除此meta-data,添加如下代码:

    1
    2
    3
    4
    5
    6
    7
    8
    <meta-data
    android:name="io.flutter.embedding.android.SplashScreenDrawable"
    android:resource="@drawable/launch_background"
    />
    <meta-data
    android:name="io.flutter.embedding.android.NormalTheme"
    android:resource="@drawable/launch_background"
    />

    修改launch_background.xml,这个要根据需求添加开屏视图

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?xml version="1.0" encoding="utf-8"?>
    <!-- Modify this file to customize your launch splash screen -->
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
    android:left="0dp"
    android:top="0dp"
    android:bottom="0dp"
    android:right="0dp"
    android:drawable="@drawable/pic_bg" />
    <!-- You can insert your own image assets here -->
    </layer-list>

    <application>标签下添加新的 <meta-data> ,一旦您在AndroidManifest中进行了声明并使用了插件,能够使用使用了新的Android的插件(将插件注册为,FlutterEngine而不是PluginRegistry.Registrar

    1
    2
    3
    < meta-data 
    androidname = “flutterEmbedding”
    androidvalue = “2” />
  1. 如果是低版本创建的项目升级,除了以上修改外还需要修改以下内容:

    1
    2
    3
    4
    5
    //FlutterActivity包路径修改
    //把
    import io.flutter.app.FlutterActivity;
    //修改为
    import io.flutter.embedding.android.FlutterActivity;

以前

1
2
3
4
5
6
7
8
9
10
11
12
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}

// ...some amount of custom code for your app is here.
}

现在

1
2
3
4
5
6
7
8
9
10
11
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}
}
  1. 这里仅有修复黑屏的问题,更多升级详细信息请前往 官方升级指南 -》 前往