Home > front end >  image_cropper crashing the app in flutter
image_cropper crashing the app in flutter

Time:04-04

I have added image cropping library in my project image_cropper: ^1.5.1 with image_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.

here is the 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>

CodePudding user response:

I got the same error previously.

Just cross-check if you have added UCropActivity into your AndroidManifest.xml

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

No configurations needed for iOS.

CodePudding user response:

finally!! i have found the solution go to android > app : right click on 'app' create a file name '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

        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!

  • Related