任务执行失败':app:transformClassesWithDexForDebug使用MapsActivity时出错

时间:2016-12-28 14:40:44

标签: android google-maps

当用户点击MainActivity中MapsActivity的地图时,我只想在Android Studio中调用默认NavigationDrawer

对于地图,我在Android studio中使用模板。当我尝试构建应用程序时,它会显示

  

任务执行失败':app:transformClassesWithDexForDebug

这里我包含我的错误屏幕截图,我的MapsActivity代码。 在我的Android Studio中,我已经在SDK管理器中安装了google play服务,我还获得了Google Map的谷歌API密钥,并将其包含在google_maps_api.xml中。

请帮帮我

MapsActivty

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

activity_maps.xml

<fragment 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:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ajay.com.dhruvafest.MapsActivity" />

我认为这些代码已经足够了......我在https://developers.google.com/maps/documentation/

中提到了这一点

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.2"

    defaultConfig {
        applicationId "ajay.com.dhruvafest"
        minSdkVersion 14
        targetSdkVersion 23
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
    }
    dexOptions {

        javaMaxHeapSize "4g"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.google.android.gms:play-services:10.0.1'
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.google.android.gms:play-services-maps:10.0.1'
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ajay.com.dhruvafest">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Splash"
            android:launchMode="standard"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Home"
            android:label="Home"
            android:largeHeap="true"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".Policies" />
        <activity android:name=".Events" />
        <activity android:name=".EventList" />
        <activity android:name=".EventDet" />
        <activity android:name=".Sponsors" />
        <activity android:name=".Workshops" />
        <activity android:name=".Developers" />
        <activity android:name=".Schedule" />
        <activity android:name=".Contact" />
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps"></activity>
    </application>
</manifest>

已更新错误屏幕截图 enter image description here

4 个答案:

答案 0 :(得分:0)

multiDexEnabled true文件中添加app.gradle

defaultConfig {
     multiDexEnabled true
}

答案 1 :(得分:0)

启动多dexing需要以下步骤:

将android-support-multidex.jar添加到您的项目中。 jar可以在Android SDK文件夹中找到/ sdk / extras / android / support / multidex / library / libs

尝试将以下内容添加到您的gradle中,

defaultConfig {
     multiDexEnabled true
}

添加依赖项,

dependencies {
compile 'com.android.support:multidex:1.0.0'
}

现在让您的应用程序类扩展MultiDexApplication

public class MyApplication extends MultiDexApplication

答案 2 :(得分:0)

第1步请检查以下代码并将此代码添加到您的gradle中。

 dexOptions {

        javaMaxHeapSize "4g"
    }

defaultConfig中的第2步添加

 multiDexEnabled true

依赖项编译中的第3步

 compile 'com.android.support:multidex:1.0.1'

第4步请参阅以下代码

apply plugin: 'com.android.application'

    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.1"

        defaultConfig {
            applicationId "com.jmtechnologies.askuscash"
            minSdkVersion 11
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
// ADD BELOW LINE
            multiDexEnabled true

        }
// BELOW LINE FOR HEAP SIZE
        dexOptions {

            javaMaxHeapSize "4g"
        }

    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:multidex:1.0.1'

    }

如果仍然会出现错误,请在此处更新您的gradle文件我需要检查gradle文件

答案 3 :(得分:0)

1)请检查下面是我的gradle文件,这个gradle文件对我来说很好。

2)对于您正在使用的Google地图

compile 'com.google.android.gms:play-services:10.0.1'

compile 'com.google.android.gms:play-services-maps:10.0.1&#39;

3)请删除compile 'com.google.android.gms:play-services:10.0.1'

第4步使用以下代码更新您的gradle文件我已对此进行了更改。

   apply plugin: 'com.android.application'

android {

    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "ajay.com.dhruvafest"
        minSdkVersion 16
        targetSdkVersion 25
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
    }
    dexOptions {

        javaMaxHeapSize "4g"
    }
    aaptOptions {
        additionalParameters "--no-version-vectors"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.google.android.gms:play-services-maps:10.0.1'
}

如果上面的gradle文件无效,请按照步骤5和6进行操作

第5步检查我已检查过的gradle文件,以及它在Build.Gradle(模块:应用程序)中的正常工作

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"


    defaultConfig {
        applicationId "ajay.com.dhruvafest"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        //if 2g or 4g
        javaMaxHeapSize "2g"
    }

    // important to run code on kitkat

    aaptOptions {
        additionalParameters "--no-version-vectors"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    // multidex
    compile 'com.android.support:multidex:1.0.0'


    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    compile 'com.android.support:recyclerview-v7:25.1.0'


    // Map
    //compile 'com.google.android.gms:play-services:10.0.1'
    compile 'com.google.android.gms:play-services-maps:10.0.1'
    compile 'com.google.maps.android:android-maps-utils:0.3+'


    // Volley
    compile 'com.android.volley:volley:1.0.0'

    //Firebase
    compile 'com.google.firebase:firebase-messaging:10.0.1'
    compile 'com.google.firebase:firebase-core:10.0.1'

    //CropImage
    compile project(':CropImage')


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

第6步和Build.Gradle(项目:您的项目名称)文件

您需要添加此行
    classpath&#39; com.google.gms:google-services:3.0.0&#39;

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:3.0.0'

    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}