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

时间:2022-01-31 20:43:25

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 Gradle although I'm already on Android Studio I've followed the option Migrating from IntelliJ Projects .

在关于迁移到Gradle的更多信息的链接之后,虽然我已经在Android Studio上,但我已经按照从IntelliJ Projects迁移的选项进行操作。

Since I've already have a build.gradle file 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 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.

按照指示,我在Android Studio终端窗口上运行了gradle assembleDebug,但是出现了一些错误。要解决这个错误,我不得不用sdk.dir = C:\\ Program Files(x86)\\ Android \\ android-sdk创建一个local.properties并从/ libs文件夹中删除android-support-v4.jar文件以及来自项目结构的Libraries-> libs。

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

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

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.

我重新启动了Android Studio,但在重新启动之后,我在项目的外部库中看到了没有支持-v4和appcompat-v7,在我的一个类中我有导入android.support.v4.widget.CursorAdapter ;标记为错误,因为无法解析符号光标适配器。

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

如果我按下按钮同步项目与gradle filesi得到消息:项目MyProject不是基于Gradle的项目

Can someone help me figuring out how to solve this?

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

Here is my project Structure

这是我的项目结构

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

6 个解决方案

#1


75  

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.

该项目认为它仍然是一个非基于Gradle的项目;不存在使其成为基于Gradle的build.gradle文件,但它首先是项目的设置方式。您需要重新导入项目才能完成转换为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:

首先,看起来你没有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. 关闭你的项目
  3. Back up your project
  4. 备份您的项目
  5. Delete the .idea folder in the root directory of the project
  6. 删除项目根目录中的.idea文件夹
  7. Delete all the .iml files in your project
  8. 删除项目中的所有.iml文件
  9. Import your project in Android Studio, and in the dialog that prompts you for a file, choose the build.gradle file.
  10. 在Android Studio中导入项目,在提示您输入文件的对话框中,选择build.gradle文件。

After this you should be good to go.

在此之后你应该好好去。

#2


3  

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步:砰......我希望它的工作。(它对我有用)

#3


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.

在我的情况下,当我尝试打开现有的gradle项目并且AS没有将其识别为gradle项目时,问题出在settings.gradle文件中。 settings.gradle已损坏,因为由于某种原因,分号丢失了。正确的结构包括':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.

我希望它可以帮助别人。

#4


1  

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-incompatible项目,然后通过自动询问我是否要进行导入一个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!

最后这对我有用!

#5


0  

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

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

#6


-2  

  1. click on File
  2. 单击文件
  3. Click on Import Projects
  4. 单击“导入项目”
  5. Click on Users then AndroidStudioProjects
  6. 单击Users然后单击AndroidStudioProjects
  7. 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.

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

#1


75  

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.

该项目认为它仍然是一个非基于Gradle的项目;不存在使其成为基于Gradle的build.gradle文件,但它首先是项目的设置方式。您需要重新导入项目才能完成转换为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:

首先,看起来你没有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. 关闭你的项目
  3. Back up your project
  4. 备份您的项目
  5. Delete the .idea folder in the root directory of the project
  6. 删除项目根目录中的.idea文件夹
  7. Delete all the .iml files in your project
  8. 删除项目中的所有.iml文件
  9. Import your project in Android Studio, and in the dialog that prompts you for a file, choose the build.gradle file.
  10. 在Android Studio中导入项目,在提示您输入文件的对话框中,选择build.gradle文件。

After this you should be good to go.

在此之后你应该好好去。

#2


3  

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步:砰......我希望它的工作。(它对我有用)

#3


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.

在我的情况下,当我尝试打开现有的gradle项目并且AS没有将其识别为gradle项目时,问题出在settings.gradle文件中。 settings.gradle已损坏,因为由于某种原因,分号丢失了。正确的结构包括':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.

我希望它可以帮助别人。

#4


1  

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-incompatible项目,然后通过自动询问我是否要进行导入一个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!

最后这对我有用!

#5


0  

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

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

#6


-2  

  1. click on File
  2. 单击文件
  3. Click on Import Projects
  4. 单击“导入项目”
  5. Click on Users then AndroidStudioProjects
  6. 单击Users然后单击AndroidStudioProjects
  7. 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.

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