在发布模式下打开 image_cropper 时应用崩溃

The app crashes when opening image_cropper in release mode

当我尝试在发布模式下打开图像裁剪器时,我的应用程序一直崩溃。

我按照 documentation 中的指示在 manifest 中添加了所有内容:

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

我如何使用它:

Future<void> _cropImage(File imageFile, int index) async {
if(imageFile != null){
  File cropped = await ImageCropper.cropImage(
      sourcePath: imageFile.path,
      aspectRatio: CropAspectRatio(ratioX: 1.0, ratioY: 1.0),
      compressQuality: 100,
      maxHeight: height,
      maxWidth: width,
      androidUiSettings: AndroidUiSettings(
        toolbarTitle: "",
        toolbarWidgetColor: Colors.black.withOpacity(0.6),
        statusBarColor: Colors.black,
        activeControlsWidgetColor: Colors.white,
      )
  );

  setState(() {
    compress(cropped, index);
  });
}}

在发布模式下启动应用程序后,立即出现以下错误消息:

E/AndroidRuntime(13412): FATAL EXCEPTION: main

E/AndroidRuntime(13412): Process: com.packages.xxx, PID: 13412

E/AndroidRuntime(13412): java.lang.NoSuchMethodError: No static method 
asAttributeSet(Li/a/a/a;)Landroid/util/AttributeSet; in class Landroid/util/Xml; or its super classes (declaration of 'android.util.Xml' appears in /system/framework/framework.jar!classes3.dex)

E/AndroidRuntime(13412):        at a.a.n.g.inflate(Unknown Source:21)

E/AndroidRuntime(13412):        at com.yalantis.ucrop.UCropActivity.onCreateOptionsMenu(Unknown Source:6)

E/AndroidRuntime(13412):        at android.app.Activity.onCreatePanelMenu(Activity.java:4212)

E/AndroidRuntime(13412):        at androidx.fragment.app.d.onCreatePanelMenu(Unknown Source:2)

E/AndroidRuntime(13412):        at a.a.n.i.onCreatePanelMenu(Unknown Source:2)

E/AndroidRuntime(13412):        at androidx.appcompat.app.g$j.onCreatePanelMenu(Unknown Source:8)

E/AndroidRuntime(13412):        at a.a.n.i.onCreatePanelMenu(Unknown Source:2)

E/AndroidRuntime(13412):        at androidx.appcompat.app.j.m(Unknown Source:25)

E/AndroidRuntime(13412):        at androidx.appcompat.app.j$a.run(Unknown Source:2)

E/AndroidRuntime(13412):        at android.os.Handler.handleCallback(Handler.java:938)

E/AndroidRuntime(13412):        at android.os.Handler.dispatchMessage(Handler.java:99)

E/AndroidRuntime(13412):        at android.os.Looper.loop(Looper.java:233)

E/AndroidRuntime(13412):        at android.app.ActivityThread.main(ActivityThread.java:7959)

E/AndroidRuntime(13412):        at java.lang.reflect.Method.invoke(Native Method)

E/AndroidRuntime(13412):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)

E/AndroidRuntime(13412):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)

我该如何解决这个问题?。谢谢。

我们将尝试多种解决方案,然后告诉我哪一个是您的问题

1- 在您的 build.gradle(Module : app) 中您会发现此部分 将其删除

if (details.requested.group == 'com.android.support'
&& !details.requested.name.contains('multidex') ) {
details.useVersion "27.1.1"
}
if (details.requested.group == 'androidx.core'
&& !details.requested.name.contains('androidx') ) {
details.useVersion "1.0.1"
}
if (requested.group == "androidx") {
if (!requested.name.startsWith("multidex")) {
details.useVersion "${targetSdk}.+"
}

2- 尝试向您的清单添加所需的权限

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

3- 尝试将 gradle 版本更改为类似

的版本
classpath 'com.android.tools.build:gradle:3.6.3'

package/android/app/build.gradle

def localProperties = new Properties()
        def localPropertiesFile = rootProject.file('local.properties')
        if (localPropertiesFile.exists()) {
            localPropertiesFile.withReader('UTF-8') { reader ->
                localProperties.load(reader)
            }
        }
    
    def flutterRoot = localProperties.getProperty('flutter.sdk')
    if (flutterRoot == null) {
        throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
    }
    
    def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
    if (flutterVersionCode == null) {
        flutterVersionCode = '1'
    }
    
    def flutterVersionName = localProperties.getProperty('flutter.versionName')
    if (flutterVersionName == null) {
        flutterVersionName = '1.0'
    }
    
    apply plugin: 'com.android.application'
    apply plugin: 'com.google.gms.google-services'
    apply plugin: 'kotlin-android'
    apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
    
    def keystoreProperties = new Properties()
    def keystorePropertiesFile = rootProject.file('key.properties')
    if (keystorePropertiesFile.exists()) {
        keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
    }

android {
    compileSdkVersion 30

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

defaultConfig {
        // Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.packages.xxx"
        minSdkVersion 24
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

    flutter {
        source '../..'
    }
    
    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation platform('com.google.firebase:firebase-bom:27.1.0')
        implementation 'com.google.firebase:firebase-analytics'
        implementation 'com.android.support:multidex:1.0.3'
    }

我遇到了同样的问题,下面的代码更改修复了它:

Add 'release' block inside the android in app/build.gradle file like give below

buildTypes {
        release {
            useProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
        }
    }

Create if not, 'app/proguard-rules.pro' file and add the following lines:

-dontwarn com.yalantis.ucrop**
-keep class com.yalantis.ucrop** { *; }
-keep interface com.yalantis.ucrop** { *; }
-keep class androidx.appcompat.** { *; }

添加上述代码后,发布模式对我有用。