10

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.

4
  • What do you need that for? I assume you have tried it and know whether it worked or not. Commented May 2, 2018 at 5:32
  • 1
    github.com/flutter/flutter/issues/12010#issuecomment-385785392 Commented May 2, 2018 at 10:13
  • 1
    Here's my issue: github.com/flutter/flutter/issues/17093 Commented May 3, 2018 at 2:19
  • 1
    If flutter tries to use the hardware acceleration available on the Raspberry Pi, it becomes unusable. Almost as if the only time the screen refreshes is on user input, and even then, it's bad. I can only get a usable app during profiling with --enable-software-rendering, so I'd like to be able to have a release apk with software rendering forced. Commented May 3, 2018 at 2:20

3 Answers 3

9

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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

import io.flutter.facade.Flutter; is depreciated now. Any way around this?
@MichaelTolsma That call should not be needed with Flutter 1.12+, I have updated the example/imports.
1

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); ).

1 Comment

'onCreate' overrides nothing is the error I receive
1

Matej's answer provides the solution for Java based Flutter apps. For newly created apps using Kotlin for startup, the code looks like this:

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

class MainActivity: FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {

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

        // start Flutter
        super.onCreate(savedInstanceState)
    }
}

The MainActivity class is found in this path:

android/app/src/main/kotlin/com/.../.../MainActivity.kt

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.