无法在 Android Studio 中将项目迁移到 Gradle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21606504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:49:13  来源:igfitidea点击:

Can't migrate project to Gradle in Android Studio

androidintellij-ideaandroid-studio

提问by Favolas

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

我有一个已经在 Android Studio 中运行的项目。

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

在 Android Studio 中打开项目后,我收到以下消息:

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.

将项目迁移到 Gradle?该项目不使用 Gradle 构建系统。我们建议您迁移到使用 Gradle 构建系统。有关迁移到 Gradle 的更多信息 不再显示此消息。

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

按照有关迁移到 Gradle更多信息的链接,虽然我已经在使用,但Android Studio我已经按照选项从 IntelliJ 项目迁移

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

因为我已经build.gradle在我的项目的根目录下有一个文件,所以我已经修改为包含一些依赖项。

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 assembleDebugon the Android Studioterminal windows but was getting some errors. To solve this errors I had to create a local.propertieswith sdk.dir=C:\\Program Files (x86)\\Android\\android-sdkand remove the android-support-v4.jarfile from the /libsfolder and from the Libraries->libsfrom the Project Structure.

按照指示,我已经gradle assembleDebugAndroid Studio终端窗口上运行,但遇到了一些错误。为了解决这个错误,我要创建一个local.propertiessdk.dir=C:\\Program Files (x86)\\Android\\android-sdk并删除android-support-v4.jar从文件/libs夹,并从Libraries->libsProject Structure

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

我现在再次运行gradle assembleDebug,这次它没有任何错误地完成。

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

我已经重启Android Studio,但重启[我已锯后,关于External Libraries该项目的有没有support-v4appcompat-v7在我的课之一,我已在import android.support.v4.widget.CursorAdapter;标记为错误以来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

如果我按下按钮,Sync project with gradle files我会收到消息:The project MyProject is not a Gradle-based project

Can someone help me figuring out how to solve this?

有人可以帮我弄清楚如何解决这个问题吗?

Here is my project Structure

这是我的项目结构

enter image description here

enter image description here

回答by Scott Barta

The project thinks it's still a non-Gradle based project; it's not the presence of the build.gradlefile 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.

该项目认为它仍然是一个非基于 Gradle 的项目;并不是build.gradle文件的存在使它基于 Gradle,而是项目最初是如何设置的。您需要重新导入您的项目以完成到 Gradle 的转换。

First, though, it looks like you don't have a settings.gradlefile; 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:

不过,首先,您似乎没有settings.gradle文件;看起来你需要一个。由于您已将项目设置为单模块项目,因此您可以将该文件放在项目的根目录中,在build.gradle旁边。它应该包含以下内容:

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:

将来,如果您向项目添加更多模块,您可能希望将其转换为多模块目录结构,但现在您无需担心。无论如何,现在您需要在 Android Studio 中重新导入:

  1. Close your project
  2. Back up your project
  3. Delete the .ideafolder in the root directory of the project
  4. Delete all the .imlfiles in your project
  5. Import your project in Android Studio, and in the dialog that prompts you for a file, choose the build.gradlefile.
  1. 关闭你的项目
  2. 备份您的项目
  3. 删除项目根目录下的.idea文件夹
  4. 删除项目中的所有.iml文件
  5. 在 Android Studio 中导入您的项目,并在提示您输入文件的对话框中,选择build.gradle文件。

After this you should be good to go.

在此之后,您应该可以开始了。

回答by Mahmudur Rahman

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

就我而言:第 1 步:项目结构 > Settings.gradle(打开它并添加 - 包括 ':app';

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

第 2 步:转到 Gradle 选项卡(通常出现在 Android Studio 的左侧或右侧)> 然后单击刷新所有 Gradle 项目

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

第 3 步:砰……我希望它起作用。(它对我有用)

回答by Mr.Moustard

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.gradlefile. settings.gradlewas 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.

就我而言,当我尝试打开一个现有的 gradle 项目并且 AS 没有像 gradle 项目那样识别它时,问题出在 file.gradle 中settings.gradlesettings.gradle已损坏,因为由于某种原因,缺少分号。正确的结构是include ':app';app 是项目模块的名称。

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

此外,如果需要,您可以放置​​多个模块,如下所示: include ':module1', ':module2', ':module3';

I hope it could help someone.

我希望它可以帮助某人。

回答by Samuele Bolognini

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.

我所做的只是尝试在Android Studio的启动窗口中使用“import Gradle,Eclipse ETC”选项导入项目,选择要导入的与gradle不兼容的项目,然后通过自动询问我是否要导入来导入一个 gradle 构建项目,瞧它这次正确导入,没有错误。

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

该过程非常简单,无需摆弄讨厌的设置,只需按照 IDE 自动修复操作即可。

At last that worked for me!

最后这对我有用!

回答by 44kksharma

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

只需检查您是否正在导入包含 .gradle 和 .idea 文件夹的正确文件夹

回答by Ipesh Jung Pandey

  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.

  1. 点击文件
  2. 点击导入项目
  3. 单击用户,然后单击 AndroidStudioProjects
  4. 选择您的项目,然后单击确定

    注意:如果您要从 eclipse 迁移到 Android Studio,那么您可以做完全相同的事情,但选择 eclipse 项目。