Can't migrate project to Gradle in Android Stu

2019-01-10 11:42发布

问题:

I have a project that I already run in Android Studio.

After I've opened the project in Android Studio I got the message:

Migrate Project to Gradle? This project does not use the Gradle build system. We recommend that you migrate to using the Gradle build system. More Information about migrating to Gradle Don't show this message again.

Following the link of More Information about migrating to Gradle although I'm already on Android Studio I've followed the option Migrating from IntelliJ Projects .

Since I've already have a build.gradle file at the root of my project I've modified to include some dependencies.

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

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')

    compile "com.android.support:support-v4:18.0.+"
    compile "com.android.support:appcompat-v7:18.0.+"
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

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

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

As instructed, i've ran gradle assembleDebug on the Android Studio terminal windows but was getting some errors. To solve this errors I had to create a local.properties with sdk.dir=C:\\Program Files (x86)\\Android\\android-sdk and remove the android-support-v4.jar file from the /libs folder and from the Libraries->libs from the Project Structure.

I now ran again gradle assembleDebug and this time it finishes without any errors.

I've restarted Android Studio but after restarting I've saw that on the External Libraries of the project there were no support-v4 and appcompat-v7 and in one of my classes I have the import android.support.v4.widget.CursorAdapter; marked as error since Cannot resolve symbol Cursor Adapter.

If I press the button Sync project with gradle filesi got the message: The project MyProject is not a Gradle-based project

Can someone help me figuring out how to solve this?

Here is my project Structure

回答1:

The project thinks it's still a non-Gradle based project; it's not the presence of the build.gradle file that makes it Gradle-based, but it's how the project was set up in the first place. You'll need to re-import your project to finish the conversion to Gradle.

First, though, it looks like you don't have a settings.gradle file; it looks like you need one. Since you've set up your project as a single-module project, then you can put the file in your project's root directory, next to build.gradle. It should contain this:

import ':'

In the future if you add more modules to your project you may want to convert it to a multi-module directory structure, but you don't need to worry about that now. In any event, now you need to do the re-import in Android Studio:

  1. Close your project
  2. Back up your project
  3. Delete the .idea folder in the root directory of the project
  4. Delete all the .iml files in your project
  5. Import your project in Android Studio, and in the dialog that prompts you for a file, choose the build.gradle file.

After this you should be good to go.



回答2:

In my case, when I tried to open an existing gradle project and AS didn't recognize it like a gradle project, the problem was inside settings.gradle file. settings.gradle was corrupt because, for some reason, the semicolon was missing. The right structure is include ':app'; where app is the name of the project's module.

Also, if you need, you can put more than one module, like this: include ':module1', ':module2', ':module3';

I hope it could help someone.



回答3:

In my case: Step 1: Project Structure > Settings.gradle (Open it and add-- include ':app'; .

Step 2: Goto Gradle Tab (It normally present in Left or Right of Android Studio) > Then click to Refresh All Gradle Project

Step 3: Bang....... I hope its work.(Its works for me)



回答4:

All I did was to try importing the project with the "import Gradle, Eclipse ETC" option at the start window of Android Studio, selected my gradle-incompatible project to import, and then it did import by asking me automatically if I wanted to make a gradle build project and voila it did import correctly this time with no errors.

The process is pretty straightforward, nothing to fiddle around with nasty settings, just follow the IDE auto-fixes and it will work.

At last that worked for me!



回答5:

Just check if you are importing the right folder which contains .gradle and .idea folder



回答6:

  1. click on File
  2. Click on Import Projects
  3. Click on Users then AndroidStudioProjects
  4. Choose your Project then Click OK

    NOTE: If you are moving from eclipse to Android Studio then you have do exactly same thing but choose the eclipse projects.