首页 > 解决方案 > 地理定位器出错:如果我在 pubspec.yaml 文件中添加 ^3.0.0,则无法构建 android 应用程序

问题描述

如果我geolocator: ^3.0.0pubspec.yamlFlutter 中添加文件,我的 Android 应用程序将无法构建并引发如下错误:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithMultidexlistForDebug'.
> com.android.build.api.transform.TransformException: Error while generating the main dex list.

我尝试将目标和 buildtool 版本从 27 更改为 28 仍然遇到同样的问题

build.gradle 文件。

android {
    compileSdkVersion 28

    dexOptions {
        javaMaxHeapSize "4g"
    }
    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).
        multiDexEnabled true
        applicationId "com.depl.toours.business"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    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
        }
    }
}

main.dart 文件。

  class MyDemoPage extends State<AccommodationPage> {
        @override
              void initState() {
                super.initState();
                propertyList = new List<String>();
                propertyList.add('Hotel');
                propertyList.add('Gust House');
                _getCurrentPosition();
              }

              _getCurrentPosition() async{
                GeolocationStatus status = await Geolocator().checkGeolocationPermissionStatus(locationPermission: GeolocationPermission.location);
                print('GPS: $status');
               Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
               double lat = position.latitude;
               double lng = position.longitude;
               print('LatLng: ${lat} & ${lng}');
              }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: new Text('Accommodation'),
      ),
      body: _getBody(),
    );
  }

    }

我期待包中的当前位置坐标geolocator: ^3.0.0,也包含multidexEnabled truedefaultConfigbuild.gradle文件)中。

标签: androidflutter

解决方案


我想你要么需要使用

android {
    defaultConfig {
        ...
        minSdkVersion 21
        targetSdkVersion 28
        multiDexEnabled true
    }
    ...
}

在您的设置中,这是缺失的

        minSdkVersion 21
        targetSdkVersion 28

或者如果您需要支持 min SDK < 21

android {
    defaultConfig {
        ...
        minSdkVersion 15
        targetSdkVersion 28
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.3'
}

在您的设置中,这是缺失的

dependencies {
  compile 'com.android.support:multidex:1.0.3'
}

有关更多详细信息,请参阅https://developer.android.com/studio/build/multidex


推荐阅读