7

I'm trying to add a map into the fragment and I've got no sign of error in my IDE but the app just crashed in the device. I don't know what happened. I've enable my map SDK for Android in the console already and all I did was just modify the MapsActivity which generated by the Android Studio to MapsFragment.

MainActivity.kt

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val fragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.add(R.id.fragment_container, MapsFragment.newInstance()).commit()
    }
}

MapsFragment.kt

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.gms.maps.*

import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions

class MapsFragment : Fragment(), OnMapReadyCallback {

    private lateinit var mMap: GoogleMap

    companion object {
        fun newInstance() = MapsFragment()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_maps, container, false)

        val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }

    override fun onMapReady(googleMap: GoogleMap) {
        mMap = googleMap

        // Add a marker in Sydney and move the camera
        val sydney = LatLng(3.0414067, 101.5901829)
        mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
    }
}

fragment_maps.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsFragment">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

And all I got is this error.

E/AndroidRuntime: FATAL EXCEPTION: Thread-7
    java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
        at ep.b(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):3)
        at eo.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):4)
        at eq.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):55)
        at com.google.maps.api.android.lib6.drd.ap.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):11)
        at dx.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):17)
        at dx.run(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):65)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.ProtocolVersion" on path: DexPathList[[zip file "/system/priv-app/PrebuiltGmsCore/app_chimera/m/MapsDynamite.apk"],nativeLibraryDirectories=[/data/user_de/0/com.google.android.gms/app_chimera/m/00000006/n/x86_64, /system/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at ad.loadClass(:com.google.android.gms.dynamite_dynamiteloader@13280056@13.2.80 (040800-211705629):25)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at ep.b(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):3) 
        at eo.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):4) 
        at eq.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):55) 
        at com.google.maps.api.android.lib6.drd.ap.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):11) 
        at dx.a(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):17) 
        at dx.run(:com.google.android.gms.dynamite_mapsdynamite@13280056@13.2.80 (040800-211705629):65) 
Froyo
  • 103
  • 1
  • 7

2 Answers2

5

If you target api level 28 or above, you must include in your Manifest:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

For your references: https://issuetracker.google.com/issues/79478779

Cao Minh Vu
  • 1,900
  • 1
  • 16
  • 21
  • You should maybe also link where you got the answer from. https://stackoverflow.com/a/50794266/2664634 – Ezzy Nov 02 '18 at 09:41
  • This is incomplete! You need to add `useLibrary 'org.apache.http.legacy'` in the build.gradle file as well! @Ramesh answer is right. – Tgo1014 Nov 06 '18 at 16:18
1

In Android 6.0,Google had removed support for the Apache HTTP client and beginning with Android 9, that library is removed from the bootclasspath and is not available to apps by default.

Apps that target Android 9 and above can add the following to their AndroidManifest.xml:

In the build.gradle you need to add this to support apache above android 6.0

 useLibrary 'org.apache.http.legacy'

Declare in the Android Manifest files to support above 9.0

 <uses-library android:name="org.apache.http.legacy" android:required="false"/>
Ramesh Yankati
  • 1,197
  • 9
  • 13
  • I was stuck like 2 hours trying to fix this and just the manifest as the accepted answer wasn't enought but I hadn't saw your response. Adding the useLibraty in gradle solved my problem. Thank you so much!!! – Tgo1014 Nov 06 '18 at 16:17