android 添加flutter module模式 最新 解决找不到io.face.flutter 包问题

网上很多代码说添加以module模式添加flutter的方式都是过时了,会找不到类,如下:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout rl = findViewById(R.id.rl_root_view);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View flutterView = Flutter.createView(MainActivity.this, getLifecycle(), "route1");
                rl.addView(flutterView);
            }
        });
    }
}

上面的代码你会发现找不到io.face.flutter类,因为这是以前版本的引用,现在最新版本里面就是没有这个类,是用了新的引入方式,如下:

var flutterView: FlutterView? = null
private var flutterEngine: FlutterEngine? = null


private fun initFlutterView() {
        val args = getArgsFromIntent(intent)
        if (flutterEngine == null) {
            flutterEngine = FlutterEngine(this, args)
            flutterEngine!!.dartExecutor.executeDartEntrypoint(
                DartEntrypoint.createDefault()
            )
        }
        flutterView = findViewById(R.id.flutter_view)
        flutterView?.run { attachToFlutterEngine(flutterEngine!!) }
    }

private fun getArgsFromIntent(intent: Intent): Array? {
        // Before adding more entries to this list, consider that arbitrary
        // Android applications can generate intents with extra data and that
        // there are many security-sensitive args in the binary.
        val args = ArrayList()
        if (intent.getBooleanExtra("trace-startup", false)) {
            args.add("--trace-startup")
        }
        if (intent.getBooleanExtra("start-paused", false)) {
            args.add("--start-paused")
        }
        if (intent.getBooleanExtra("enable-dart-profiling", false)) {
            args.add("--enable-dart-profiling")
        }
        if (args.isNotEmpty()) {
            val argsArray = arrayOfNulls(args.size)
            return args.toArray(argsArray)
        }
        return null
    }

如上kotlin代码 在oncreate中调用initFlutterView 即可

你可能感兴趣的:(android基础知识)