Android Gradle Issue

2019-05-11 09:06发布

问题:

I know there are 100's of questions/answers around this topic, but none of them seem to give me an answer. I know some(if not all) of my problems are around my lack of understanding of gradle in general. BUT, i'm hoping for some help.

I've got my project working just fine on my desktop. I'm traveling this week, and wanted to work on it some on my laptop. I have all the files, and have the same version of Android Studio on both machines. I kept getting all kinds of gradle errors when opening my project. I think I've went on several wild goose chases at this point.

So I decided to step back and just create a NEW basically blank project in Studio. That ALSO has all kinds of gradle issues. I tried uninstalling Android Studio and re-insatlling, and still no dice even getting a basic project to not give the gradle errors.

I am getting

11:12:27 PM Gradle 'MyApplication' project refresh failed: A fatal exception has occurred. Program will exit. : Gradle settings

As the error.

Below is my two gradle files.

Top Level File(which was blank in my actual project, but has something in it in the default one)

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

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

The next build file

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.android.support:support-v4:18.0.0'
}

I am using Android Studio 0.4.2

I am at a complete loss of what is going on. I think it could be that Studio is not actually recognizing that this is an android project. I tried some of the gradlew.bat commands and I got an error mentioning that it does not like the "apply plugin: 'android'" entry. But, I have no idea why that would be.

Any thoughts on where I should start would be greatly appreciated. I have no ideas of where to go next, and guessing at it anymore is not an option.

回答1:

I've got the same issue with IntelliJ IDEA(which Android Studio is based on) on my laptop and found a solution here: https://code.google.com/p/android/issues/detail?id=65219

Set File -> Settings -> Compiler -> Java Compiler -> Additional command line parameters

-Xms256m -Xmx512m

File -> Settings -> Compiler -> Gradle -> VM Options

-XX:MaxHeapSize=256m -Xmx256m

and in YourAppName/build.gradle update dependencies to use newer gradle version

classpath 'com.android.tools.build:gradle:0.9.+'

Then restart Android Studio and everything should be fine. Otherwise you can take a look at full log file by going to Help -> Show Log in Explorer



回答2:

To add a little more knowledge to the 'Gradle XXXX project refresh failed'

.

Adding following line to

File -> Settings -> Compiler -> Gradle -> VM Options:

-Xmx256m

seems to solve the problem.

.

It is worth noting that it fixes only certain cause of the problem - and there may be many. In my case: the machine I'm using is 32-bit and has very little memory. As a result my compiler couldn't reserve enough memory on the heap and so refused to compile.

Therefore if you don't have much RAM try adding the above line.

If still stuck, try:

https://stackoverflow.com/a/21168562/3508719



回答3:

Did you verify that you have buildToolsVersion 19.0.1 and compileSdkVersion 19 is available on you machine, since you are working on laptop I believe this is new setup.

Can you go to Tools -> Android -> SDK Manager and see if you need to install any version specified in your gradle script. Or update gradle script as per SDK and build tools you have.

My couple of issue were resolved by updating SDK and tools!