Compilation error in Android Studio When making a simple Google Android Maps API v2 project

11,616

I was following the same instructions except I was creating a new project. Under the project structure I removed the Android-Gradle facet and was able to build successfully. Optionally one can update the gradle build files and add the Android-Gradle facet to the play services library.

NOTE: I changed the name of Google Play Services directory.

build.gradle for Google Play Services library.

apply plugin: 'android-library'

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}

dependencies {
    compile files('libs/android-support-v4.jar')
    compile files('google-play-services.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion '17.0.0'

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aild.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
}

build.gradle for test app.

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
    compile project(':lib-google-play-services')
    compile files('../lib-google-play-services/libs/google-play-services.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 16
    }
}
Share:
11,616
niranjan94
Author by

niranjan94

Developer at @Mesitis & @fossasia. Backend usually; Android rarely. Python, Golang & Kubernetes. Self proclaimed designer.

Updated on June 19, 2022

Comments

  • niranjan94
    niranjan94 almost 2 years

    Recently I migrated to the New Android Studio IDE based on IntelliJ

    The Guides i followed were:

    1. https://developers.google.com/maps/documentation/android/start (for basics)

    2. How can I create an Android application in Android Studio that uses the Google Maps Api v2? (for importing the required Google Play services and support libraries into android studio)

    All the libraries were properly detected by Android Studio and i didn't get any 'library not found errors'. The project was shown error free. Then when i tried to compile it i got this error.

    Gradle: 
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':AID-AmritaInfoDesk:compileDebug'.
    > Compilation failed; see the compiler error output for details.
    
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
    
    
    Could not execute build using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.6-bin.zip'.
    

    This was my explorer.java file

    package com.aid.explorer;
    
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;
    
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    
    
    public class explorer extends FragmentActivity {
    
        private GoogleMap mMap;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.explorer);
            setUpMapIfNeeded();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            setUpMapIfNeeded();
        }
    
    
        private void setUpMapIfNeeded() {
            // Do a null check to confirm that we have not already instantiated the map.
            if (mMap == null) {
                // Try to obtain the map from the SupportMapFragment.
                mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                        .getMap();
                // Check if we were successful in obtaining the map.
                if (mMap != null) {
                    setUpMap();
                }
            }
        }
    
    
        private void setUpMap() {
            mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        }
    }
    

    And this was my explorer.xml file

    <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/map"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              class="com.google.android.gms.maps.SupportMapFragment"/>
    

    I would like to know what went wrong. any help would be greatly appreciated

  • lepoetemaudit
    lepoetemaudit almost 11 years
    This worked for me, although I had to add this to settings.gradle: include ':google-play-services_lib'
  • androidtester
    androidtester almost 11 years
    Ok but with the external compiler option turned off you get more information about errors
  • opyate
    opyate almost 11 years
    And for those who want to get google-play-services_lib imported as a Gradle project into Android Studio: stackoverflow.com/questions/16977739/…