2

I've already tried a thousand configurations and codes, but my map activity insists in not working. Things I've tried:

AndroidManifest.xml

  • enabled access permissions (INTERNET, ACCESS_FINE_LOCATION, READ_GSERVICES etc);
  • enabled OpenGL (android:glEsVersion="0x00020000" etc);
  • supplied two keys (com.google.android.geo.API_KEY and com.google.android.maps.v2.API_KEY) with directly key in AndroidManifest and referencing that by xml values in debug and release folders (@string/map_api);
  • tried to use map library (uses-library android:name="com.google.android.maps");
  • tried to include permission tag uses-permission (...permission.MAPS_RECEIVE);
  • tried to inform services version (android:name="com.google.android.gms.version");
  • tried to generate keys for a own certificate and android.keystore under .android folder;
  • file:

    <manifest package="com.myapp"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:anrdroid="http://schemas.android.com/apk/res-auto">
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <application
                android:allowBackup="true"
                android:icon="@drawable/logo"
                android:label="@string/name"
                android:supportsRtl="true"
                android:name="android.support.multidex.MultiDexApplication"
        android:theme="@style/Default">
            <meta-data
                    android:name="com.google.android.geo.API_KEY"
                    android:value="@string/google_maps_key"/>
                <activity
                    android:name=".view.MainActivity"
                    android:icon="@drawable/logo"
                    android:label="@string/name">
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN"/>
                        <category android:name="android.intent.category.LAUNCHER"/>
                    </intent-filter>
                </activity>
                <activity
                    android:name=".view.ActivityMap"
                    android:icon="@drawable/logo_row"
                    android:label="@string/name">
                </activity>
                ...
            </application>
            </manifest>
    

build.gradle

  • tried different compile and build configurations (23, 22, 19 etc);
  • tried different library versions (8.4.0, 7.80 etc);
  • tried different library versions for appcompat library (23.2.1, 22.2.1 etc);
  • used selective compile only for maps and dependencies or Google Services complete;
  • tried to sign debug and release with same and different keys;
  • file:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion '23.0.2'
        defaultConfig {
            applicationId "com.myapp"
            minSdkVersion 16
            targetSdkVersion 23
            versionCode 1
            versionName "0.0.1"
            multiDexEnabled true
        }
    
        signingConfigs {
            keystore {
                storeFile file("path_to_keystore")
                storePassword "my_password"
                keyAlias "my_key_alias"
                keyPassword "my_password"
            }
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                signingConfig signingConfigs.keystore
            }
        }
    
        packagingOptions {
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/io.netty.versions.properties'
            exclude 'META-INF/INDEX.LIST'
        }
    
        dexOptions {
            javaMaxHeapSize "2g"
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.2.1'
        compile 'com.android.support:design:23.2.1'
        compile 'com.google.android.gms:play-services-maps:8.4.0'
        compile 'com.datastax.cassandra:cassandra-driver-mapping:3.0.0'
        compile 'com.fasterxml.jackson.core:jackson-core:2.5.4'
        compile 'com.fasterxml.jackson.core:jackson-databind:2.5.4'
    }
    

Code

  • tried to extends AppCompat, FragmentActivity;
  • tried to request permission in api 6 using ActivityCompat.requestPermissions();
  • tried call MapsInitializer.initialize() and check return;
  • tried to use only fragment into layout file, informing context etc;
  • tried to use fragment inside FragmentLayout;
  • files:

    public class ActivityMap extends FragmentActivity implements OnMapReadyCallback
    {
        private GoogleMap map;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_map);
            SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.activityMapID);
            fragment.getMapAsync(this);
        }
    
    
        @Override
        public void onMapReady(GoogleMap googleMap)
        {
            map = googleMap;
            map.addMarker(new MarkerOptions().position(new LatLng(-34, 151)).title("Marker in Sydney"));
        }
    }
    

activity_map.xml:

    <fragment android:id="@+id/activityMapID"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

Others

  • checked my network connections and firewalls;
  • checked if the keys were available and working in Google Console;
  • tried to clean, rebuild project and invalidate caches for Android Studio many times;
  • tried to wipe data, restart and clean device (and tested in more than one device);
  • removed the app through adb using "adb shell pm clear ...";
  • no nullpointers, onMapReady returns a valid map and debugging code, everything works fine;
  • if I create a new map app using Android Studio, everything works fine;

In first call, I have this log:

    03-21 09:34:28.478 25570-26174/com.myapp I/LoadedApk: connected(), package name=com.google.android.gms ,class name=com.google.android.gms.maps.auth.ApiTokenService
    03-21 09:34:29.770 25570-25570/com.myapp I/Choreographer: Skipped 127 frames!  The application may be doing too much work on its main thread.
    03-21 09:34:30.020 25570-27391/com.myapp I/b: Sending API token request.
    03-21 09:34:30.090 25570-25633/com.myapp I/LoadedApk: connected(), package name=com.google.android.gms ,class name=com.google.android.location.internal.GoogleLocationManagerService
    03-21 09:34:31.231 25570-27391/com.myapp I/b: Received API Token: AH0uPGGIx9Ygap4ZXq8T8vK7qcPxgyDi5NRIaIPyPauG4xWFn1lI7KQF9IuY2yqbpYMhWitKnTUiU-sZD9tFdJvr1naCVhz3c9APmLbueopuhcD6K5LHpZKnkYCQDEefhrTeGPNOR0fdz0QmC4WD8rkgQTbLmfbnOIA7cvZZBOmn7hFmcyavIVRQZlaY9_OLaPMFxIVFVBml / Expires in: 432000000ms
    03-21 09:34:31.241 25570-27391/com.myapp I/c: Scheduling next attempt in 431700 seconds.
    03-21 09:34:31.251 25570-27391/com.myapp I/d: Saved auth token
    03-21 09:34:31.832 25570-25582/com.myapp I/LoadedApk: connected(), package name=com.google.android.gms ,class name=com.google.android.gms.clearcut.service.ClearcutLoggerService

Then after second call, I have the message that my key is invalid:

    03-21 09:35:22.596 25570-25633/com.myapp I/LoadedApk: connected(), package name=com.google.android.gms ,class name=com.google.android.gms.clearcut.service.ClearcutLoggerService
    03-21 09:35:37.762 25570-25570/com.myapp I/Google Maps Android API: Google Play services package version: 8703034
    03-21 09:35:37.973 25570-28472/com.myapp E/Google Maps Android API: Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).
    03-21 09:35:38.363 25570-25582/com.myapp I/LoadedApk: connected(), package name=com.google.android.gms ,class name=com.google.android.location.internal.GoogleLocationManagerService

I really don't know what to do anymore. Does someone have any idea what could be happening?

Thanks in advance.

  • http://stackoverflow.com/questions/17491400/failed-to-load-map-error-contacting-google-servers-issue-with-android-google-ma Another link: http://stackoverflow.com/questions/17667935/failed-to-load-map-error-contacting-google-servers-this-is-probably-an-authent – Rusheel Jain Mar 21 '16 at 14:02
  • I assuming you are quite confused. As your other map app is working. You certainly missed some configuration while following the process in the current app. – Pankaj Nimgade Mar 21 '16 at 14:13
  • The strange thing is if I call directly my map activity from manifest as the launcher, it works fine. When I call it from another activity, doesn't work. I'm still trying to figure out some permission problem... – user2387273 Mar 21 '16 at 14:19
  • Did you put correct map API key?? – Shailendra Madda Mar 21 '16 at 14:29
  • Yes, and I tried with more than just one key. I've tried with the generated android.keystore and my own. None of them worked... :( – user2387273 Mar 21 '16 at 14:31
  • If you checking it in real device, Once uninstall your app from the device and clean your project and run again.. – Shailendra Madda Mar 21 '16 at 14:36

2 Answers2

1

Finally I found the problem.

In another part of my code, I was calling

    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

To simplify all the calls I do for HTTPS. The issue was when Google Maps tried to call its server: it was using the same SSL Context I've set before so it couldn't connect on Map servers.

I've just commented that line and changed my code to:

    URLConnection urlConn = new URL(myAddress).openConnection();
    HttpsURLConnection https = (HttpsURLConnection) urlConn;
    https.setSSLSocketFactory(sslContext.getSocketFactory());

and everything worked like magic!

Thanks everyone!

0

Try to add this line at the end of the build.radle

apply plugin: 'com.google.gms.google-services'

Add it in inside of manifest tag :

<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" >
    </uses-feature>
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • Make sure your API key is generated with SHA-1 key + package name mentioned in the android manifest file not but your actual source package – Shailendra Madda Mar 21 '16 at 14:51