Flutter Android App 在使用谷歌地图时崩溃

Posted

技术标签:

【中文标题】Flutter Android App 在使用谷歌地图时崩溃【英文标题】:Flutter Android App crashes when uing google maps 【发布时间】:2021-03-24 08:09:40 【问题描述】:

尝试使用 pub.dev 中的 google_maps_flutter 包显示谷歌地图时,我的 Flutter 应用程序崩溃。这些是日志

Running with unsound null safety
For more information see https://dart.dev/null-safety/unsound-null-safety
I/zzbz    (31588): Making Creator dynamically
D/        (31588): HostConnection::get() New Host Connection established 0x7f2a9c00, tid 31603
W/zygote  (31588): Unsupported class loader
W/zygote  (31588): Skipping duplicate class check due to unsupported classloader
I/DynamiteModule(31588): Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:203112999
I/DynamiteModule(31588): Selected remote version of com.google.android.gms.maps_dynamite, version >= 203112999
V/DynamiteModule(31588): Dynamite loader version >= 2, using loadModule2NoCrashUtils
D/        (31588): HostConnection::get() New Host Connection established 0x89c0fc00, tid 31625
W/ResourceType(31588): ResTable_typeSpec entry count inconsistent: given 67, previously 69
W/ResourceType(31588): ResTable_typeSpec entry count inconsistent: given 5399, previously 5400
W/zygote  (31588): Unsupported class loader
W/zygote  (31588): Skipping duplicate class check due to unsupported classloader
I/Google Maps Android API(31588): Google Play services client version: 12451000
I/Google Maps Android API(31588): Google Play services package version: 204516022
I/zygote  (31588): Do partial code cache collection, code=29KB, data=24KB
I/zygote  (31588): After code cache collection, code=29KB, data=24KB
I/zygote  (31588): Increasing code cache capacity to 128KB
W/DynamiteModule(31588): Local module descriptor class for com.google.android.gms.googlecertificates not found.
I/DynamiteModule(31588): Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:6
I/DynamiteModule(31588): Selected remote version of com.google.android.gms.googlecertificates, version >= 6
W/zygote  (31588): Unsupported class loader
W/zygote  (31588): Skipping duplicate class check due to unsupported classloader
Lost connection to device.

这是 AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
   <application
        android:label="myapp"
        android:icon="@mipmap/ic_launcher">
        <uses-library android:name="org.apache.http.legacy" android:required="false"/>
         <meta-data 
         android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyBx5Lp3oc-aTekzyLykT858a_ECRSEicAU"/>
        <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>
        </activity>
        <!-- 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>
</manifest>

我尝试使用它来显示它

class _MyHomePageState extends State<MyHomePage> 
 
  Completer<GoogleMapController> _controller = Completer();



static const LatLng _center = const LatLng(45.521563, -122.677433);
 void _onMapCreated(GoogleMapController controller) 

_controller.complete(controller);





  @override
  Widget build(BuildContext context) 
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(

         appBar: AppBar(

           title: Text('Maps Sample App'),

           backgroundColor: Colors.green[700],

           ),

           body: GoogleMap(

             onMapCreated: _onMapCreated,

             initialCameraPosition: CameraPosition(

               target: _center,

               zoom: 11.0,

          ),

        ),

      );
  

我在 AndroidManifest.xml 中添加了&lt;uses-library android:name="org.apache.http.legacy" android:required="false"/&gt;,但应用程序仍然崩溃。有人可以帮帮我吗?

【问题讨论】:

我正在使用google_maps_flutter: ^1.0.6。谢谢 可能与这个github.com/flutter/flutter/issues/72185(?)有关 谢谢我去看看 【参考方案1】:

如果您在 build.gradle 中将 minSdkVersion 更改为 24,它会起作用。 还没有找到原因。

顺便说一句,即使 minSdkVersion 为 16,它也可以在 java/android studio 中运行。

所以这可能是一个颤振配置问题。

【讨论】:

我将 minSdkVersion 更改为 24,但它仍然崩溃【参考方案2】:

尝试 Gradle 版本 3.5.0 和最小 SDK 20

为我工作

【讨论】:

以上是关于Flutter Android App 在使用谷歌地图时崩溃的主要内容,如果未能解决你的问题,请参考以下文章

谷歌的 Mobile UI 框架 Flutter Beta 3 发布

谷歌发布Flutter beta 1 :可开发原生APP,支持跨平台

有啥方法可以在 Flutter for Web 中添加谷歌地图?

[flutter]仿深圳地铁app项目

APP开发--Flutter框架初体验

Flutter跨平台框架(Dart语言)