简体   繁体   English

image_cropper 在 flutter 中使应用程序崩溃

[英]image_cropper crashing the app in flutter

I have added image cropping library in my project image_cropper: ^1.5.1 with image_picker: ^0.8.4+11 .我在我的项目image_cropper: ^1.5.1image_picker: ^0.8.4+11中添加了图像裁剪库。 i picked the image with image picker and pass the image file to image cropper after selecting the file from gallery and passing it to image cropper the app is getting crashed with the following generated logs, This code is working fine in debug mode but in release it is getting crashed:在从图库中选择文件并将其传递给图像裁剪器后,我使用图像选择器选择了图像并将图像文件传递给图像裁剪器应用程序因以下生成的日志而崩溃,此代码在调试模式下工作正常但在发布时正在崩溃:

    E/AndroidRuntime(16153): FATAL EXCEPTION: main
    E/AndroidRuntime(16153): Process: com.testapp, PID: 16153
    E/AndroidRuntime(16153): java.lang.IllegalAccessError: Illegal class access: 'androidx.appcompat.widget.Conten
    tFrameLayout' attempting to access 'androidx.appcompat.app.w' (declaration of 'androidx.appcompat.widget.Conte
    ntFrameLayout' appears in /data/app/~~z4bx5ponHbUNL1ta7qj5SA==/com.testapp-E-Oz7_lJH0IimHOXbiaftg==/base.apk)
    E/AndroidRuntime(16153):        at androidx.appcompat.widget.ContentFrameLayout.onAttachedToWindow(Unknown Source:7)
    E/AndroidRuntime(16153):        at     android.view.View.dispatchAttachedToWindow(View.java:20626)
    E/AndroidRuntime(16153):        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3514)       
    E/AndroidRuntime(16153):        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3521)       
    E/AndroidRuntime(16153):        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3521)       
E/AndroidRuntime(16153):        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3521)       
    E/AndroidRuntime(16153):        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3521)       
    E/AndroidRuntime(16153):        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2702)        
    E/AndroidRuntime(16153):        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2182)
    E/AndroidRuntime(16153):        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8730)
    E/AndroidRuntime(16153):        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1352)     
    E/AndroidRuntime(16153):        at android.view.Choreographer.doCallbacks(Choreographer.java:1149)
    E/AndroidRuntime(16153):        at android.view.Choreographer.doFrame(Choreographer.java:1049)
    E/AndroidRuntime(16153):        at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:1275)
    E/AndroidRuntime(16153):        at android.os.Handler.dispatchMessage(Handler.java:106)
    E/AndroidRuntime(16153):        at android.os.Looper.loop(Looper.java:233)
    E/AndroidRuntime(16153):        at android.app.ActivityThread.main(ActivityThread.java:8010)
    E/AndroidRuntime(16153):        at java.lang.reflect.Method.invoke(Native Method)
    E/AndroidRuntime(16153):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
    E/AndroidRuntime(16153):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)

here is the code part:这是代码部分:

final imgPicker.ImagePicker _picker = imgPicker.ImagePicker();

final imgPicker.XFile? images =
    await _picker.pickImage(source: imgPicker.ImageSource.gallery);
try{
  if (images != null) {
    File? _cropped = await cropper.ImageCropper().cropImage(
      sourcePath: images.path,
      aspectRatioPresets: [
        cropper.CropAspectRatioPreset.ratio16x9,
      ],
      maxHeight: 250,
      cropStyle: cropper.CropStyle.rectangle,
      maxWidth: 1000,
      androidUiSettings: cropper.AndroidUiSettings(
          toolbarTitle: 'Crop your cover image',
          toolbarColor: Kolors.kRed,
          toolbarWidgetColor: Colors.white,
          initAspectRatio: cropper.CropAspectRatioPreset.ratio16x9,
          lockAspectRatio: true,
          hideBottomControls: true),
      iosUiSettings: cropper.IOSUiSettings(
        minimumAspectRatio: 1.0,
        aspectRatioLockEnabled: true,
      ),
    );
    if(_cropped != null) {
      // cropped image code
    }
  } else {
    showToast('Please select an image');
  }
}
catch (e) {
  log(e.toString());
}

whenever i select image from gallery after selection the app gets crashed, i tried removing the cropper part then it is working fine but when i user cropper my app get crashed.每当我选择 select 图片后,应用程序崩溃,我尝试删除裁剪器部分然后它工作正常但是当我用户裁剪器时我的应用程序崩溃了。

here is the Androidmanifest.xml这是 Androidmanifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.testapp">
    <application
            android:label="testapp"
            android:requestLegacyExternalStorage="true"
            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">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
                    android:name="io.flutter.embedding.android.NormalTheme"
                    android:resource="@style/NormalTheme"
            />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
                    android:name="io.flutter.embedding.android.SplashScreenDrawable"
                    android:resource="@drawable/launch_background"
            />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <meta-data android:name="flutter_deeplinking_enabled" android:value="true"/>
            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="https" android:host="app.testapp.com"/>
                <!--                <data android:scheme="https" />-->
            </intent-filter>
        </activity>
        <activity
                android:name="com.yalantis.ucrop.UCropActivity"
                android:screenOrientation="portrait"
                android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

        <!-- 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>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

</manifest>

I got the same error previously.我之前遇到了同样的错误。

Just cross-check if you have added UCropActivity into your AndroidManifest.xml只需交叉检查您是否已将 UCropActivity 添加到您的 AndroidManifest.xml 中

<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

No configurations needed for iOS. iOS 无需配置。

finally:!最后:! i have found the solution go to android > app : right click on 'app' create a file name ' proguard-rules.pro '我找到了解决方案 go 到 android > 应用程序:右键单击“应用程序”创建一个文件名“ proguard-rules.pro

add all of these following lines in the created file:在创建的文件中添加以下所有这些行:

#Flutter Wrapper
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.**  { *; }
-keep class io.flutter.util.**  { *; }
-keep class io.flutter.view.**  { *; }
-keep class io.flutter.**  { *; }
-keep class io.flutter.plugins.**  { *; }
-dontwarn org.xmlpull.v1.XmlPullParser
-dontwarn org.xmlpull.v1.XmlSerializer
-keep class org.xmlpull.v1.* {*;}
-keep class androidx.appcompat.** { *; }

now go to app/build.gradle and these lines inside the release block现在 go 到 app/build.gradle 和发布块中的这些行

        signingConfig signingConfigs.debug
        minifyEnabled true
        useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

use flutter clean command, re-create the app in release mode and done!使用 flutter clean 命令,在发布模式下重新创建应用程序并完成!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM