Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integration_test screenshot Android - FlutterFragmentActivity - PlatformException(Could not copy the pixels, FlutterView is null) #89683

Closed
hampsterx opened this issue Sep 8, 2021 · 18 comments · Fixed by #132406
Assignees
Labels
a: platform-views Embedding Android/iOS views in Flutter apps f: integration_test The flutter/packages/integration_test plugin found in release: 2.5 Found to occur in 2.5 found in release: 2.6 Found to occur in 2.6 has reproducible steps The issue has been confirmed reproducible and is ready to work on P1 High-priority issues at the top of the work list platform-android Android applications specifically r: fixed Issue is closed as already fixed in a newer version team-android Owned by Android platform team

Comments

@hampsterx
Copy link

hampsterx commented Sep 8, 2021

Steps to Reproduce

  1. flutter create test
  2. Change MainActivity to use FlutterFragmentActivity
class MainActivity: FlutterFragmentActivity() {

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}
  1. Add integration_test, test_driver as per https://github.com/flutter/flutter/tree/master/packages/integration_test
  2. Add screenshot steps..
 await binding.convertFlutterSurfaceToImage();

    // Trigger a frame.
    await tester.pumpAndSettle();
    await binding.takeScreenshot('screenshot-1');
 flutter drive --driver=test_driver/integration_test.dart --target=integration_test/home_test.dart

Expected results:

Takes screenshot, same as when using FlutterActivity

Actual results:

Failure in method: screenshot
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞═════════════════
The following PlatformException was thrown running a test:
PlatformException(Could not copy the pixels, FlutterView is null,
null, null)

When the exception was thrown, this was the stack:
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
<asynchronous suspension>
#2      IOCallbackManager.takeScreenshot (package:integration_test/_callback_io.dart:91:33)
<asynchronous suspension>
#3      IntegrationTestWidgetsFlutterBinding.takeScreenshot (package:integration_test/integration_test.dart:175:39)
<asynchronous suspension>
#4      main.<anonymous closure> (file:///home/tim/AndroidStudioProjects/untitled/integration_test/home_test.dart:40:5)
<asynchronous suspension>
#5      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:176:15)
<asynchronous suspension>
#6      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:817:5)
<asynchronous suspension>

flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.5.0, on KDE neon User Edition 5.22 5.8.0-63-generic, locale en_NZ.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] Android Studio
[✓] Connected device (3 available)

• No issues found!
@darshankawar darshankawar added the in triage Presently being triaged by the triage team label Sep 9, 2021
@darshankawar
Copy link
Member

darshankawar commented Sep 9, 2021

@hampsterx
Thanks for the report. Can you provide the entire MainActivity.kt ? I tried using FlutterFragmentActivity() as you mentioned, but it's giving me bunch of compile errors. With FlutterActivity(), it works properly though.

Also, please check this similar /related issue for your reference: #83856

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Sep 9, 2021
@hampsterx
Copy link
Author


import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterFragmentActivity() {

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }


}

My project is using local_auth for biometrics which requires the FlutterFragmentActivity hence why I hit this issue.

cheers~

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Sep 9, 2021
@darshankawar
Copy link
Member

Thank for the file. I verified this on latest master and stable and see the same behavior. Below exception is thrown:

console error log


D:\FlutterApps\flutter_screenshot_integration_test-main>flutter drive --driver=test_driver/integration_test.dart --target=integration_test/screenshot_test.dart
Could not load custom device from config index 0: Expected enabled to be a boolean.
Running "flutter pub get" in flutter_screenshot_integration_test-main...        707ms
Running Gradle task 'assembleDebug'...                             89.5s
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...                12.4s
VMServiceFlutterDriver: Connecting to Flutter application at http://127.0.0.1:53233/rWSv5B6ltNs=/
VMServiceFlutterDriver: Isolate found with number: 1182669862824263
VMServiceFlutterDriver: Isolate is paused at start.
VMServiceFlutterDriver: Attempting to resume isolate
I/flutter (25803): 00:00 +0: screenshot
VMServiceFlutterDriver: Connected to Flutter application.
I/flutter (25803): ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
I/flutter (25803): The following PlatformException was thrown running a test:
I/flutter (25803): PlatformException(Could not copy the pixels, FlutterView is null, null, null)
I/flutter (25803):
I/flutter (25803): When the exception was thrown, this was the stack:
I/flutter (25803): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
I/flutter (25803): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
I/flutter (25803): <asynchronous suspension>
I/flutter (25803): #2      IOCallbackManager.takeScreenshot (package:integration_test/_callback_io.dart:96:33)
I/flutter (25803): <asynchronous suspension>
I/flutter (25803): #3      IntegrationTestWidgetsFlutterBinding.takeScreenshot (package:integration_test/integration_test.dart:175:39)
I/flutter (25803): <asynchronous suspension>
I/flutter (25803): #4      main.<anonymous closure> (file:///D:/FlutterApps/flutter_screenshot_integration_test-main/integration_test/screenshot_test.dart:35:5)
I/flutter (25803): <asynchronous suspension>
I/flutter (25803): #5      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:176:15)
I/flutter (25803): <asynchronous suspension>
I/flutter (25803): #6      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:817:5)
I/flutter (25803): <asynchronous suspension>
I/flutter (25803):
I/flutter (25803): The test description was:
I/flutter (25803):   screenshot
I/flutter (25803): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (25803): 00:01 +0: screenshot [E]
I/flutter (25803):   Test failed. See exception logs above.
I/flutter (25803):   The test description was: screenshot
I/flutter (25803):
I/flutter (25803): Consider enabling the flag chain-stack-traces to receive more detailed exceptions.
I/flutter (25803): For example, 'dart test --chain-stack-traces'.
I/flutter (25803): 00:01 +0 -1: (tearDownAll)
I/flutter (25803): 00:01 +1 -1: Some tests failed.
Failure Details:
Failure in method: screenshot
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞═════════════════
The following PlatformException was thrown running a test:
PlatformException(Could not copy the pixels, FlutterView is null,
null, null)

When the exception was thrown, this was the stack:
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
<asynchronous suspension>
#2      IOCallbackManager.takeScreenshot (package:integration_test/_callback_io.dart:96:33)
<asynchronous suspension>
#3      IntegrationTestWidgetsFlutterBinding.takeScreenshot (package:integration_test/integration_test.dart:175:39)
<asynchronous suspension>
#4      main.<anonymous closure> (file:///D:/FlutterApps/flutter_screenshot_integration_test-main/integration_test/screenshot_test.dart:35:5)
<asynchronous suspension>
#5      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:176:15)
<asynchronous suspension>
#6      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:817:5)
<asynchronous suspension>

The test description was:
  screenshot
═════════════════════════════════════════════════════════════════

end of failure 1




D:\FlutterApps\flutter_screenshot_integration_test-main>



flutter doctor -v
[√] Flutter (Channel master, 2.6.0-1.0.pre.237, on Microsoft Windows [Version 10.0.19042.1165], locale en-US)
    • Flutter version 2.6.0-1.0.pre.237 at D:\FlutterSDK\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision a54b10254a (2 hours ago), 2021-09-10 07:22:02 +0300
    • Engine revision 57bc7414b8
    • Dart version 2.15.0 (build 2.15.0-92.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\Win\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[!] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.2)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.31624.102
    X The current Visual Studio installation is incomplete. Please reinstall Visual Studio.

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] VS Code (version 1.60.0)
    • VS Code at C:\Users\Win\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension can be installed from:
       https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (4 available)
    • SM A260G (mobile) • 5200763ebcfa861f • android-arm    • Android 8.1.0 (API 27)
    • Windows (desktop) • windows          • windows-x64    • Microsoft Windows [Version 10.0.19042.1165]
    • Chrome (web)      • chrome           • web-javascript • Google Chrome 93.0.4577.63
    • Edge (web)        • edge             • web-javascript • Microsoft Edge 93.0.961.38

! Doctor found issues in 1 category.

[√] Flutter (Channel stable, 2.5.0, on Microsoft Windows [Version 10.0.19042.1165], locale en-US)
    • Flutter version 2.2.3 at D:\FlutterSDK\flutter
    • Framework revision f4abaa0735 (31 hours ago), 2021-09-07 12:46:11 -0700
    • Engine revision f0826da7ef
    • Dart version 2.14.0

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\Win\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[X] Visual Studio - develop for Windows
    X Visual Studio not installed; this is necessary for Windows development.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components

[√] Android Studio
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • android-studio-dir = C:\Program Files\Android\Android Studio
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] Connected device (5 available)
    • SM A260G (mobile) • 5200763ebcfa861f • android-arm    • Android 8.1.0 (API 27)
    • SM G975F (mobile) • RZ8M802WY0X      • android-arm64  • Android 11 (API 30)
    • Windows (desktop) • windows          • windows-x64    • Microsoft Windows [Version 10.0.19042.1165]
    • Chrome (web)      • chrome           • web-javascript • Google Chrome 93.0.4577.63
    • Edge (web)        • edge             • web-javascript • Microsoft Edge 92.0.902.84

! Doctor found issues in 1 category.

C:\Users\Win>

@darshankawar darshankawar added found in release: 2.5 Found to occur in 2.5 found in release: 2.6 Found to occur in 2.6 has reproducible steps The issue has been confirmed reproducible and is ready to work on f: integration_test The flutter/packages/integration_test plugin platform-android Android applications specifically a: platform-views Embedding Android/iOS views in Flutter apps and removed in triage Presently being triaged by the triage team labels Sep 10, 2021
@BouweCeunen
Copy link

Is there already a solution for this? Also hitting the exact same issue.

@hampsterx
Copy link
Author

@jmagman @blasten @nturgut

any thoughts?
(I see a few prior PR's in this area from ya'll)

@jmagman
Copy link
Member

jmagman commented Dec 6, 2021

@blasten worked on this.

@Kiruel
Copy link

Kiruel commented Dec 21, 2021

Same problem on 2.8.1

@kalebhermes
Copy link
Contributor

Also looking for a solution on this issue :(

@hfierro
Copy link

hfierro commented Mar 23, 2022

¿Any updates on this? I'm using flutter 2.10.3 and still having the same error. I'm also using FlutterFragmentActivity

@sebastianbuechler
Copy link

Still an issue in flutter 3.0.5. @darshankawar maybe add the tag for flutter 3.0?

@GaryQian GaryQian added the P1 High-priority issues at the top of the work list label Aug 16, 2022
@matthew-carroll
Copy link
Contributor

@jmagman - Am I correct in understanding that this bug prevents all screenshot captures from Android apps through the relevant Flutter APIs?

@matthew-carroll
Copy link
Contributor

I suppose I should also tag @GaryQian with that question, too ^

@jmagman
Copy link
Member

jmagman commented Sep 21, 2022

I don't work on this 🙂
cc @camsim99

@francois-robert
Copy link

Still an issue in flutter 3.10.0, is there a workaround available ?

@dkobia
Copy link

dkobia commented Jun 16, 2023

Here's what seems to work for me so far:

  • Switch from the deprecated FlutterFragmentActivity to FlutterActivity in MainActivity.kt
  • Use a try/catch with surface->image:
if (Platform.isAndroid) {
  try {
    await binding.convertFlutterSurfaceToImage();
  } catch (e) {
    // Already converted
  }
}

await tester.pumpAndSettle();
await binding.takeScreenshot(screenshotName);

@hampsterx
Copy link
Author

Switch from the deprecated FlutterFragmentActivity

This is still the recommended way for some packages, eg local_auth and hence why this ticket was created.

Activity Changes
Note that local_auth requires the use of a FragmentActivity instead of an Activity. To update your application:
If you are using FlutterActivity directly, change it to FlutterFragmentActivity in your AndroidManifest.xml.

#43921

Seems semi related maybe..

@flutter-triage-bot flutter-triage-bot bot added team-android Owned by Android platform team triaged-android Triaged by Android platform team labels Jul 8, 2023
@flutter-triage-bot
Copy link

This issue is marked P1 but has had no recent status updates.

The P1 label indicates high-priority issues that are at the top of the work list. This is the highest priority level a bug can have if it isn't affecting a top-tier customer or breaking the build. Bugs marked P1 are generally actively being worked on unless the assignee is dealing with a P0 bug (or another P1 bug). Issues at this level should be resolved in a matter of months and should have monthly updates on GitHub.

Please consider where this bug really falls in our current priorities, and label it or assign it accordingly. This allows people to have a clearer picture of what work is actually planned. Thanks!

@flutter-triage-bot flutter-triage-bot bot removed the triaged-android Triaged by Android platform team label Jul 13, 2023
auto-submit bot pushed a commit that referenced this issue Sep 7, 2023
…ctivity`s (#132406)

Fixes #89683.

The changes to `getFlutterView` in `FlutterDeviceScreenshot` are the fix that was required, everything else was done to get tests running (such as re-generating some lockfiles and modifying the android manifest).

The code was all currently not unit tested, and there were no other easy examples to base these java unit tests off in flutter/flutter, so let me know if this approach to testing is wrong.
@darshankawar darshankawar added the r: fixed Issue is closed as already fixed in a newer version label Sep 11, 2023
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: platform-views Embedding Android/iOS views in Flutter apps f: integration_test The flutter/packages/integration_test plugin found in release: 2.5 Found to occur in 2.5 found in release: 2.6 Found to occur in 2.6 has reproducible steps The issue has been confirmed reproducible and is ready to work on P1 High-priority issues at the top of the work list platform-android Android applications specifically r: fixed Issue is closed as already fixed in a newer version team-android Owned by Android platform team
Projects
None yet
Development

Successfully merging a pull request may close this issue.