1

我正在使用 Kotlin 在 Google Maps 上关注 Raywenderlich的教程。我有以下代码由于我无法理解的原因而崩溃:

地图活动.kt

import com.google.android.gms.location.LocationServices
...
private lateinit var map: GoogleMap
private lateinit var fusedLocationClient: FusedLocationProviderClient

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_maps)
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    val mapFragment = supportFragmentManager
        .findFragmentById(R.id.map) as SupportMapFragment
    mapFragment.getMapAsync(this)

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
}

构建.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'com.google.android.gms:play-services-maps:16.1.0'
    implementation 'com.google.android.gms:play-services-location:11.8.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

我已经注释掉getFusedLocationProviderClient()并且应用程序运行良好。一旦取消注释,应用程序就会崩溃。有什么我做错了吗?gms:play-services-location:11.8.0是否可能是对可能是问题的版本的引用。如果是这样,我如何检查我安装的版本?

更新这是来自 Logcat 的冲突日志

--------- beginning of crash
2019-12-23 09:37:02.698 14916-14916/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.johndoe.cityguide, PID: 14916
    java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/api/Api$zzf;
        at com.google.android.gms.location.LocationServices.<clinit>(Unknown Source:0)
        at com.google.android.gms.location.LocationServices.getFusedLocationProviderClient(Unknown Source:0)
        at com.johndoe.cityguide.MapsActivity.onCreate(MapsActivity.kt:29)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.api.Api$zzf" on path: DexPathList[[zip file "/data/app/com.johndoe.cityguide-jR7TeGEs6Oxa46o3Ce5Zcw==/base.apk"],nativeLibraryDirectories=[/data/app/com.johndoe.cityguide-jR7TeGEs6Oxa46o3Ce5Zcw==/lib/x86, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at com.google.android.gms.location.LocationServices.<clinit>(Unknown Source:0) 
        at com.google.android.gms.location.LocationServices.getFusedLocationProviderClient(Unknown Source:0) 
        at com.johndoe.cityguide.MapsActivity.onCreate(MapsActivity.kt:29) 
        at android.app.Activity.performCreate(Activity.java:7009) 
        at android.app.Activity.performCreate(Activity.java:7000) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
4

1 回答 1

1

我想我曾经遇到过类似的问题。这是由于 Play Services 依赖项之间的版本冲突。我目前使用以下方法来利用 FusedLocationProviderClient:

dependencies {
    def play_services_version = '17.0.0'

    ...

    implementation "com.google.android.gms:play-services-maps:$play_services_version"
    implementation "com.google.android.gms:play-services-location:$play_services_version"
}

我希望这有帮助!

于 2019-12-23T00:10:22.647 回答