dens14345
dens14345

Reputation: 302

Android studio Importing unity project error(Error:Execution failed for task ':app:dexDebug' : UNEXPECTED TOP-LEVEL EXCEPTION)

This error is bothering me for two days. So I need to import a Project from unity into android studio but gives me this error:

Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    C:\Users\DELL\AppData\Local\Android\sdk\build-tools\23.0.2\dx.bat --dex --no-optimize --output C:\Users\DELL\AndroidStudioProjects\Passingdata1\app\build\intermediates\dex\debug --input-list=C:\Users\DELL\AndroidStudioProjects\Passingdata1\app\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
    2
Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lcom/king/passingdata/BuildConfig;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:579)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:535)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:517)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:164)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
        at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
        at com.android.dx.command.dexer.Main.run(Main.java:277)
        at com.android.dx.command.dexer.Main.main(Main.java:245)
        at com.android.dx.command.Main.main(Main.java:106)

My build gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.king.passingdata"
        minSdkVersion 18
        targetSdkVersion 22
    }

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

dependencies {
    compile files('libs/myandroidplugin.jar')
    compile files('libs/QCARUnityPlayer.jar')
    compile files('libs/unity-classes.jar')
    compile files('libs/Vuforia.jar')
}

I see a lot of UNEXPECTED TOP-LEVEL EXCEPTION: and Error:Execution failed for task ':app:dexDebug'. here in stackoverflow and in other website, but non of them seems to be similar with my error or helpful with my situation.

Upvotes: 0

Views: 444

Answers (1)

Rahul
Rahul

Reputation: 510

In your build.gradle file try adding the following block inside your android block.

dexOptions {
        incremental true
        javaMaxHeapSize "4g"
        preDexLibraries = false
}

And your defaultConfig add this

multiDexEnabled true

defaultConfig {
        applicationId "com.example"
        minSdkVersion 17
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        renderscriptTargetApi 21
        renderscriptSupportModeEnabled true;
    }

In you gradle dependency add,

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

dependencies {
    //compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.android.support:multidex:1.0.1'
}

In Application class, attachBaseContext method include this line,

MultiDex.install(this);

public class ExampleApp extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

In your AndroidManifest.xml file,

Add attribute "name" and assign above mentioned Application Class.

 <application
        android:name="com.ExampleApp">

<!--- Activities -->

</application>

Now try to run your project after clean and build your project.

Hope it will help you my dear friend !

Upvotes: 1

Related Questions