1

For some reason when i run the app in Android studio its giving me this:

Error:Could not create the Java Virtual Machine.

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
Error:A fatal exception has occurred. Program will exit.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files (x86)\Java\jdk1.7.0_80\bin\java.exe'' finished with non-zero exit value 1
Information:BUILD FAILED

This happnes in android studio. Here is my app:

MainActivity.java:

package com.example.amanuel.webview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private WebView webView;
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OnClickEvent();
    }

    public void OnClickEvent(){
        button = (Button) findViewById(R.id.Url_Button);
        webView = (WebView) findViewById(R.id.webView);
        editText = (EditText) findViewById(R.id.Edit_Text_Url);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = editText.getText().toString();
                webView.getSettings().setLoadsImagesAutomatically(true);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
                webView.loadUrl(url);
            }
        });
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.amanuel.webview.MainActivity">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="URL"
        android:id="@+id/Url_Button"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webView"
        android:layout_below="@+id/Edit_Text_Url"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/Edit_Text_Url"
        android:layout_below="@+id/Url_Button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="https://google.com" />
</RelativeLayout>

And AndroidManifest.xml:

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

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Help would be really appreciated!

amanuel2
  • 4,508
  • 4
  • 36
  • 67

2 Answers2

1

You might be having multiple dex files with same signatures to fix this add this in your app level gradle file.

 defaultConfig {
    multiDexEnabled true
}
H.T
  • 161
  • 7
0

This ERROR have POSSIBLE SOLUTION

SOLUTION 1.

If some conflicting drawable resources found then It give the same error. So you have to clean the Project and rebuild it.

SOLUTION 2.

Problem with RAM : So close the application which are not used. and free the Memory RAM.

SOLUTION 3.

GC overhead (out of memory) :

so add this in build.gradle file.

android {

    dexOptions {
        incremental = true;
        preDexLibraries = false
        javaMaxHeapSize "4g" // 2g should be also OK
    }

}