Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter build apk with --enable-software-rendering?

Tags:

flutter

dart

Is it possible to do something like this:

flutter build apk --enable-software-rendering

I need a release version that performs the say way as:

flutter run --enable-software-rendering --profile

Thank you.

like image 909
captnPlanet Avatar asked May 02 '18 04:05

captnPlanet


2 Answers

TL;DR Put getIntent().putExtra("enable-software-rendering", true); on top of your onCreate()


Note - I assumed Android from the "apk" in question title and the need for software rendering.

Looking at the source code, the --enable-software-rendering flag for flutter run causes the activity to be launched using am start with --ez enable-software-rendering true, which puts that as a boolean extra into the intent.

If you wish to control when to use software rendering from code (such as depending on API level or device model), set the mentioned intent extra early in your onCreate().

Full Example:

import android.os.Bundle;
import io.flutter.embedding.android.FlutterActivity;

public class MyActivity extends FlutterActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        // use software rendering (ideally only when you need to)
        getIntent().putExtra("enable-software-rendering", true);

        // start Flutter
        super.onCreate(savedInstanceState);
    }
}
like image 62
Matej Snoha Avatar answered Nov 07 '22 10:11

Matej Snoha


For those whom got here because are struggling with your Flutter Android app crashing with the following error

ERROR:flutter/shell/gpu/gpu_surface_gl Failed to setup Skia Gr context

when coming back to foreground after being put in background, just add enable-software-rendering to "onCreate" method as our friend Matej Snoha said above.

In other words, change android/app/src/main/kotlin/[project]/MainActivity file to the following Kotlin code:

class MainActivity : FlutterActivity() {

    // add onCreate method (if not exists)
    override fun onCreate(savedInstanceState: Bundle?) {
        // add this line to "onCreate" method
        this.getIntent().putExtra("enable-software-rendering", true)
        // don't forget to call "super"
        super.onCreate(savedInstanceState)
    }

}

It worked like a charm for me (no need to call Flutter.startInitialization(this); ).

like image 39
Christian Avatar answered Nov 07 '22 09:11

Christian