D8:程序类型已经存在:android.support.v4.media.MediaBrowserCompat $ CustomActionCallback

时间:2019-02-19 17:06:09

标签: android gradle flutter android-gradle flutter-dependencies

我是不熟悉flutter的人,我构建了一个很小的应用程序来显示当前的纬度和经度,这需要我在pubspec.yaml中添加两个依赖项:-

location和simple_permissions可以获取当前用户的位置并请求位置权限。

但是当我运行该应用程序时,它显示以下错误:-

Gradle task 'assembleDebug'...
D8: Program type already present: android.support.v4.media.MediaBrowserCompat$CustomActionCallback

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/2.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/3.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/4.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/5.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/6.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/7.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/8.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/9.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/10.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/11.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/12.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/13.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/14.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/15.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/16.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/17.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/18.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/19.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/20.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/21.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/22.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/23.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/24.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/25.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/26.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/27.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/28.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/29.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/30.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/31.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/32.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/33.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/34.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/35.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/36.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/37.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/38.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/39.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/40.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/41.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/42.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/43.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/44.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/45.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/46.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/47.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/48.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/49.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/50.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/51.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/52.jar, /home/letsdoit/IdeaProjects/location/build/app/intermediates/transforms/dexBuilder/debug/53.jar
  Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
  Program type already present: android.support.v4.media.MediaBrowserCompat$CustomActionCallback

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 9s
Finished with error: Gradle task assembleDebug failed with exit code 1

我认为这是因为两次添加了一个库,但是我没有手动添加依赖项,因此内部出现抖动错误。

请告诉我解决方案

./ gradlew app:dependecies的输出如下:-

+--- project :location
|    \--- com.google.android.gms:play-services-location:15.+ -> 15.0.1
|         +--- com.google.android.gms:play-services-base:[15.0.1,16.0.0) -> 15.0.1
|         |    +--- com.google.android.gms:play-services-basement:[15.0.1] -> 15.0.1
|         |    |    \--- com.android.support:support-v4:26.1.0 -> 27.1.1
|         |    |         +--- com.android.support:support-compat:27.1.1
|         |    |         |    +--- com.android.support:support-annotations:27.1.1
|         |    |         |    \--- android.arch.lifecycle:runtime:1.1.0
|         |    |         |         +--- android.arch.lifecycle:common:1.1.0
|         |    |         |         \--- android.arch.core:common:1.1.0
|         |    |         +--- com.android.support:support-media-compat:27.1.1
|         |    |         |    +--- com.android.support:support-annotations:27.1.1
|         |    |         |    \--- com.android.support:support-compat:27.1.1 (*)
|         |    |         +--- com.android.support:support-core-utils:27.1.1
|         |    |         |    +--- com.android.support:support-annotations:27.1.1
|         |    |         |    \--- com.android.support:support-compat:27.1.1 (*)
|         |    |         +--- com.android.support:support-core-ui:27.1.1
|         |    |         |    +--- com.android.support:support-annotations:27.1.1
|         |    |         |    +--- com.android.support:support-compat:27.1.1 (*)
|         |    |         |    \--- com.android.support:support-core-utils:27.1.1 (*)
|         |    |         \--- com.android.support:support-fragment:27.1.1
|         |    |              +--- com.android.support:support-compat:27.1.1 (*)
|         |    |              +--- com.android.support:support-core-ui:27.1.1 (*)
|         |    |              +--- com.android.support:support-core-utils:27.1.1 (*)
|         |    |              +--- com.android.support:support-annotations:27.1.1
|         |    |              +--- android.arch.lifecycle:livedata-core:1.1.0
|         |    |              |    +--- android.arch.lifecycle:common:1.1.0
|         |    |              |    +--- android.arch.core:common:1.1.0
|         |    |              |    \--- android.arch.core:runtime:1.1.0
|         |    |              |         \--- android.arch.core:common:1.1.0
|         |    |              \--- android.arch.lifecycle:viewmodel:1.1.0
|         |    \--- com.google.android.gms:play-services-tasks:[15.0.1] -> 15.0.1
|         |         \--- com.google.android.gms:play-services-basement:[15.0.1] -> 15.0.1 (*)
|         +--- com.google.android.gms:play-services-basement:[15.0.1,16.0.0) -> 15.0.1 (*)
|         +--- com.google.android.gms:play-services-places-placereport:[15.0.1,16.0.0) -> 15.0.1
|         |    \--- com.google.android.gms:play-services-basement:[15.0.1,16.0.0) -> 15.0.1 (*)
|         \--- com.google.android.gms:play-services-tasks:[15.0.1,16.0.0) -> 15.0.1 (*)
+--- project :simple_permissions
|    \--- com.android.support:support-v4:27.1.1 (*)
\--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.71
     \--- org.jetbrains.kotlin:kotlin-stdlib:1.2.71
          +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.2.71
          \--- org.jetbrains:annotations:13.0
我的应用程序的

build.gradle如下:-

//noinspection GradleCompatible
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: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"

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

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "ab"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    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 "com.android.support:multidex:1.0.3"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

pubspec.yaml:-

name: a
description: b

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# Read more about versioning at semver.org.
version: 1.0.0+1

environment:
  sdk: ">=2.0.0-dev.68.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  permission_handler: ^2.2.0
  location: ^2.0.0



  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  # assets:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.io/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.io/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.io/custom-fonts/#from-packages

5 个答案:

答案 0 :(得分:2)

此gradle错误消息是如此难以解决...好吧,我遇到了这样的问题,并写了解决方法here。无论如何,我建议您使用pubspec中更新的软件包。 simple_permissions软件包已经有一段时间没有更新了,请尝试使用permission_handler软件包。避免将来出现构建问题的另一点是将您的应用程序迁移到androidX并使用已经使用androidX的依赖项,很多官方插件也正在迁移到它,并且当我们在同一项目中将支持库与androidX混合使用时,构建过程会令人遗憾地失败。如果您打算在应用中加入google_maps,我真的建议您迁移androidX来避免使用它。

AndroidX是原始支持库程序包和带有其他命名空间的类。完整说明here

您可以手动迁移到androidx,也可以使用有时会失败的android-studio迁移工具。 here已详细描述了使用android-studio工具的迁移过程。

要手动迁移,您需要用相应的androidx软件包替换所有支持库依赖项。一个例子是:

dependencies {
   // using support library
   implementation 'com.android.support:appcompat-v7:$version' 
}

必须替换为

dependencies {
       // using androidx
       implementation 'androidx.appcompat:appcompat:$version' //androidx artifact 
    }

可以找到Androidx工件映射here 在gradle.properties文件中,您需要在下面插入这两行

android.enableJetifier=true
android.useAndroidX=true

并确保您的flutter依赖性也正在使用androix。

看看this杂乱页面,其中讨论了androidX的兼容性和迁移。

希望对您有帮助。

答案 1 :(得分:2)

我有同样的问题

在gradle.properties中添加了此内容

android.enableJetifier=true 
android.useAndroidX=true

build.gradle中(应用程序级别) 删除:实施'com.android.support:support-v4:27.1.1'

添加了:

implementation `"androidx.legacy:legacy-support-v4:1.+"`

运行清理和使缓存无效并重新启动-已解决

答案 2 :(得分:1)

使用Ionic,通过添加以下插件解决

cordova plugin add cordova-plugin-androidx
cordova plugin add cordova-plugin-androidx-adapter

问候!

答案 3 :(得分:0)

我将添加另一个答案,因为在这里我将更新您的构建文件。

  • build.gradle

    // noinspection GradleCompatible def localProperties =新的Properties() def localPropertiesFile = rootProject.file('local.properties') 如果(localPropertiesFile.exists()){     localPropertiesFile.withReader('UTF-8'){reader->         localProperties.load(阅读器)     } }

    def flutterRoot = localProperties.getProperty('flutter.sdk') 如果(flutterRoot == null){     抛出新的GradleException(“未找到Flutter SDK。在local.properties文件中使用flutter.sdk定义位置。”) }

    def flutterVersionCode = localProperties.getProperty('flutter.versionCode') 如果(flutterVersionCode == null){     flutterVersionCode ='1' }

    def flutterVersionName = localProperties.getProperty('flutter.versionName') 如果(flutterVersionName == null){     flutterVersionName ='1.0' }

    应用插件:“ com.android.application” 应用插件:“ kotlin-android” 适用于:“ $ flutterRoot / packages / flutter_tools / gradle / flutter.gradle”

    android {     编译版本28     buildToolsVersion“ 28.0.3”

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    
    lintOptions {
        disable 'InvalidPackage'
    }
    
    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "ab"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    
    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
        }
    }
    

    }

    颤抖{     资源 '../..' } 依赖项{     实现“ org.jetbrains.kotlin:kotlin-stdlib-jdk7:$ kotlin_version”     实现“ androidx.multidex:multidex:2.0.0”     testImplementation'junit:junit:4.12'     androidTestImplementation'androidx.test:runner:1.1.0-alpha4'     androidTestImplementation'androidx.test.espresso:espresso-core:3.1.0-alpha4' }

* gradle.properties

您将在flutter项目的android模块根文件夹中找到一个名为gradle.properties的文件,并在此文件中添加以下行:

 android.enableJetifier=true

 android.useAndroidX=true

* gradle-wrapper.properties

您需要转到yourProjectRootDirectory> android> gradle>包装器,这里有一个名为gradle-wrapper.properties的文件。在此文件中,也许您应该将您的distributionUrl属性替换为:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip

* pubspec.yaml

有一个新版本的permission_handler插件,也许您可​​以对其进行更新。

permission_handler: ^3.0.0

好吧,我基于已经迁移到androidX的自己的文件编写了此文件,并且我的项目正常运行。我真的希望它会有所帮助。

答案 4 :(得分:0)

对我来说,是Cordova-plugin-mfilechooser插件,它尚未使用androidx(使用Phonegap)