现在如何将 Eclipse 项目导入 Android Studio?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22791150/
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
How do you import an Eclipse project into Android Studio now?
提问by Phazor
Using "Import Project" in Android Studio for an Eclipse project used to change the project structure and generate Gradle files, but right now (I'm using AS 0.5.3) it's only generating IDEA files (.iml, .idea) but not Gradle and it's not touching the file structure either.
在 Android Studio 中为 Eclipse 项目使用“导入项目”,用于更改项目结构并生成 Gradle 文件,但现在(我使用的是 AS 0.5.3)它只生成 IDEA 文件(.iml、.idea)而不是Gradle,它也没有触及文件结构。
How do you import an Eclipse project into Android Studio now?
现在如何将 Eclipse 项目导入 Android Studio?
Update:Trying to export gradle files in Eclipse won't export them for the app, it doesn't show up as a module in Android Studio either.
更新:尝试在 Eclipse 中导出 gradle 文件不会为应用程序导出它们,它也不会在 Android Studio 中显示为模块。
回答by Scott Barta
In newer versions of Android Studio, the best way to bring in an Eclipse/ADT (Android Development Tool)project is to import it directly into Android Studio; we used to recommend you export it from Eclipse to Gradle first, but we haven't been updating ADT often enough to keep pace with Android Studio.
在较新版本的 Android Studio 中,引入 Eclipse/ADT (Android 开发工具)项目的最佳方式是将其直接导入 Android Studio;我们曾经建议您先将其从 Eclipse 导出到 Gradle,但我们更新 ADT 的频率不足以跟上 Android Studio 的步伐。
In any event, if you choose "Import Project" from the File menu or from the Welcome screen when you launch Android Studio, it should take you through a specialized wizard that will prompt you that it intends to copy the files into a new directory structure instead of importing them in-place, and it will offer to fix up some common things like converting dependencies into Maven-style includes and such.
无论如何,如果您在启动 Android Studio 时从“文件”菜单或“欢迎”屏幕中选择“导入项目”,它应该会引导您完成一个专门的向导,提示您打算将文件复制到新的目录结构中而不是就地导入它们,它将提供修复一些常见问题,例如将依赖项转换为 Maven 样式的包含等。
It doesn't seem like you're getting this specialized flow. I think it may not be recognizing your imported project as an ADT project, and it's defaulting to the old built-into-IntelliJ behavior which doesn't know about Gradle. To get the specialized import working, the following must be true:
您似乎没有获得这种专门的流程。我认为它可能不会将您导入的项目识别为 ADT 项目,并且默认为不了解 Gradle 的旧的内置 IntelliJ 行为。要使专门的导入工作,必须满足以下条件:
- The root directory of the project you import must have an AndroidManifest.xmlfile.
- Either:
- The root directory must contain the .projectand .classpathfiles from Eclipse
- or
- The root directory must contain resand srcdirectories.
- 您导入的项目的根目录必须有一个AndroidManifest.xml文件。
- 任何一个:
- 根目录必须包含来自 Eclipse的.project和.classpath文件
- 或者
- 根目录必须包含res和src目录。
If your project is complex, perhaps you're not pointing it as the root directory it wants to see for the import to succeed.
如果您的项目很复杂,也许您没有将其指向它希望成功导入的根目录。
回答by Ajay S
Export from Eclipse
从 Eclipse 导出
Update your Eclipse ADT Plugin to 22.0 or higher, then go to File | Export
Go to Android now then click on
Generate Gradle build files
, then it would generate gradle file for you.Select your project you want to export
Click on finish now
将您的 Eclipse ADT 插件更新到 22.0 或更高版本,然后转到文件 | 出口
现在转到 Android 然后单击
Generate Gradle build files
,然后它会为您生成 gradle 文件。选择您要导出的项目
点击立即完成
Import into Android Studio
导入到 Android Studio
In Android Studio, close any projects currently open. You should see the Welcome to Android Studio window.
Click Import Project.
Locate the project you exported from Eclipse, expand it, select it and click OK.
在 Android Studio 中,关闭当前打开的所有项目。您应该会看到 Welcome to Android Studio 窗口。
单击导入项目。
找到您从 Eclipse 导出的项目,展开它,选择它并单击 OK。
回答by Mohammad
Try these steps: 1- click on Import project (Eclipse, ADT, ...)
尝试以下步骤: 1- 单击导入项目(Eclipse、ADT、...)
2- Choose main directory of your Eclipse project
2- 选择 Eclipse 项目的主目录
3- Keep the defaults. The first two options is for changing jar files into remote libraries (dependencies). It mean while building Android studio try to find library in local system or remote repositories. The last option is for showing only one folder as app after importing.
3-保留默认值。前两个选项用于将 jar 文件更改为远程库(依赖项)。这意味着在构建 Android Studio 时尝试在本地系统或远程存储库中查找库。最后一个选项是在导入后仅将一个文件夹显示为应用程序。
4- Then, you will see the summary of changes
4- 然后,您将看到更改摘要
5- Then, if you see Gradle project sync failed, you should go to project view (top left corner). Then, you should go to your project-> app and open build.gradle.
5- 然后,如果您看到 Gradle 项目同步失败,您应该转到项目视图(左上角)。然后,您应该转到您的项目-> 应用程序并打开 build.gradle。
6- Then, you should change your compilesdkVersion and targetsdkVersion to your current version that you see in buildToolsVersion (mine is 23). For example, in my project I should change 17 to 23 in two places
6- 然后,您应该将 compilesdkVersion 和 targetsdkVersion 更改为您在 buildToolsVersion 中看到的当前版本(我的是 23)。例如,在我的项目中,我应该在两个地方将 17 更改为 23
7- If you see an error in your dependencies, you should change the version of it. For example, in my project I need to check which version of android support library I am using. So, I open the SDK manager and go to bottom to see the version. Then, I should replace my Android studio version with my current version and click try again from top right corner
7- 如果您在依赖项中看到错误,您应该更改它的版本。例如,在我的项目中,我需要检查我使用的是哪个版本的 android 支持库。因此,我打开 SDK 管理器并转到底部查看版本。然后,我应该用我当前的版本替换我的 Android studio 版本,然后点击右上角的再试一次
I hope it helps.
我希望它有帮助。
回答by vikas kumar
Its Got simpler with Android Studio All you need is to first choose
使用 Android Studio 变得更简单 您只需要先选择
3.based on the type of project and library you used like (ActionBarSherlock) you may prompted special import wizard so go ahead and click next then finish. in this case it was simple one
3.根据您使用的项目和库的类型(ActionBarSherlock),您可能会提示特殊的导入向导,请继续并单击下一步然后完成。在这种情况下,它很简单
but sometimes the debug or Run options do not workand a error msg shows like
但有时调试或运行选项不起作用,错误消息显示如下
"this project structure is not gradle based or migrate it to gradle"
“此项目结构不是基于 gradle 或将其迁移到 gradle”
something to solve this close the opened eclipse project and reopen same project through the same process as we did before with import project (eclipse adt,gradle,etc)) this time android studio gonna add all necessary gradle files and green debug option will work too. i have did this somehow accidentally but it worked, i just hope it works for you too.
解决这个问题的方法是关闭打开的 eclipse 项目并通过与我们之前使用导入项目(eclipse adt、gradle 等)相同的过程重新打开相同的项目)这次 android studio 将添加所有必要的 gradle 文件和绿色调试选项也将起作用. 我以某种方式意外地做了这件事,但它奏效了,我只希望它也适用于你。
回答by Carl
Android Studio has been improved since this question was posted, and the latest versions of Android Studio (as of this writing, we are at 2.1.1) have fairly good Eclipse importing capabilities, so importing Eclipse projects directly into Android Studio is now the best approach for migrating projects from Eclipse into Android Studio.
自从发布这个问题以来,Android Studio 得到了改进,并且最新版本的 Android Studio(截至撰写本文时,我们在 2.1.1)具有相当不错的 Eclipse 导入功能,因此现在将 Eclipse 项目直接导入到 Android Studio 中是最好的将项目从 Eclipse 迁移到 Android Studio 的方法。
I will describe how to do this below, including a few of the pitfalls that one might encounter. I will deal in particular with importing an Eclipse workspace that contains multiple apps sharing one or more project libraries (the approaches posted thus far seem limited to importing just one Eclipse app project and its project libraries). While I don't deal with every possible issue, I do go into a lot of detail regarding some of them, which I hope will be helpful to those going through this process for the first time themselves.
我将在下面描述如何做到这一点,包括一些人们可能会遇到的陷阱。我将特别处理导入包含多个共享一个或多个项目库的应用程序的 Eclipse 工作区(迄今为止发布的方法似乎仅限于导入一个 Eclipse 应用程序项目及其项目库)。虽然我不会处理所有可能的问题,但我确实详细介绍了其中的一些问题,我希望这对那些第一次经历这个过程的人有所帮助。
I recently imported the projects from an Eclipse workspace. This workspace included four library projects that were shared between up to nine projects each.
我最近从 Eclipse 工作区导入了这些项目。该工作区包括四个库项目,每个项目在多达九个项目之间共享。
Some background:
一些背景:
An Eclipse workspace contains multiple projects, which may be library projects or apps.
一个 Eclipse 工作区包含多个项目,可能是库项目或应用程序。
An Android Studio projectis analogous to an Eclipse workspace, in that it can contain both library projects and apps. However, a library project or an app is represented by a "module" in Android Studio, whereas it is represented by a "project" in Eclipse.
Android Studio项目类似于 Eclipse工作区,因为它可以包含库项目和应用程序。但是,库项目或应用程序在 Android Studio 中由“模块”表示,而在 Eclipse 中由“项目”表示。
So, to summarize: Eclipse workspaces will end up as Android Studio projects, and Eclipse projects inside a workspace will end up as Android Studio modules inside a project.
因此,总结一下:Eclipse 工作区将最终成为 Android Studio 项目,而工作区中的 Eclipse 项目最终将成为项目中的 Android Studio 模块。
You should start the import process by creating an Android Studio project (File / New / New Project). You might give this project the same (or similar) name as you gave your Eclipse workspace. This project will eventually hold all of your modules, one each for each Eclipse project (including project libraries) that you will import.
您应该通过创建一个 Android Studio 项目(文件/新建/新建项目)来开始导入过程。您可以为该项目指定与您为 Eclipse 工作区指定的名称相同(或相似)的名称。该项目最终将包含您的所有模块,您将导入的每个 Eclipse 项目(包括项目库)各一个。
The import process does not change your original Eclipse files, so long as you place the imported files in a different folder hierarchy, so you should choose a folder for this project that is not in your original Eclipse hierarchy. For example, if your Eclipse projects are all in a folder called Android, you might create a sibling folder called AStudio.
导入过程不会更改您的原始 Eclipse 文件,只要您将导入的文件放在不同的文件夹层次结构中,因此您应该为此项目选择一个不在原始 Eclipse 层次结构中的文件夹。例如,如果您的 Eclipse 项目都在一个名为 Android 的文件夹中,您可以创建一个名为 AStudio 的同级文件夹。
Your Android Studio project can then be created as a sub-folder of this new folder. The New Project wizard will prompt you to enter this top-level project folder, into which it will create your project.
然后可以将您的 Android Studio 项目创建为这个新文件夹的子文件夹。新建项目向导将提示您输入此顶级项目文件夹,它将在其中创建您的项目。
Android Studio's new project wizard will then ask you to configure a single module at the time you create the project. This can be a little confusing at first, because they never actually tell you that you are creating a module, but you are; you are creating a project with a single module in it. Apparently, every project is required to have at least one module, so, since you are relying on Eclipse to provide your modules, your initial module will be a placeholder to vacuously satisfy that formal requirement.
然后,Android Studio 的新项目向导会要求您在创建项目时配置单个模块。一开始这可能有点令人困惑,因为他们实际上从未告诉您您正在创建模块,但您确实是;您正在创建一个包含单个模块的项目。显然,每个项目都需要至少有一个模块,因此,由于您依赖 Eclipse 来提供您的模块,您的初始模块将是一个占位符,以空洞地满足该正式要求。
Thus, you probably will want to create an initial module for your project that does as little as possible. Therefore, select Phone and Tablet as the type of your module, accept the default minimum SDK (API level 8), and select Add No Activity for your module.
因此,您可能希望为您的项目创建一个尽可能少的初始模块。因此,选择手机和平板电脑作为您的模块类型,接受默认的最低 SDK(API 级别 8),并为您的模块选择添加无活动。
Next, select one of the Eclipse app projects in your workspace that requires the largest number of libraries as your first project to import. The advantage of doing this is that when you import that project, all the library projects that it uses (directly, or indirectly, if some of your library projects themselves require other library projects) will get imported along with it as part of the importing process.
接下来,在您的工作区中选择一个需要最多库数量的 Eclipse 应用程序项目作为您要导入的第一个项目。这样做的好处是,当您导入该项目时,它使用的所有库项目(直接或间接,如果您的某些库项目本身需要其他库项目)将作为导入过程的一部分与它一起导入.
Each of these imported projects will get its own module within your Android Studio project. All of these modules will be siblings of one another (both in your project hierarchy, and in the folder hierarchy where their files are placed), just as if you had imported the modules separately. However, the dependencies between the modules will be created for you (in your app's build.gradle files) as part of the importing process.
这些导入的项目中的每一个都将在您的 Android Studio 项目中获得自己的模块。所有这些模块都将是彼此的兄弟(无论是在您的项目层次结构中,还是在放置它们的文件的文件夹层次结构中),就好像您分别导入了这些模块一样。但是,作为导入过程的一部分,将为您创建模块之间的依赖项(在您的应用程序的 build.gradle 文件中)。
Note that after you finish importing, testing and debugging this "most dependent" Eclipse project and its supporting library projects, you will go on to import a second Eclipse app project (if you have a second one in your workspace) and itslibrary project modules (with those imported earlier getting found by the import wizard as existing modules and re-used for this new module, rather than being duplicated).
请注意,在您完成导入、测试和调试这个“最依赖”的 Eclipse 项目及其支持库项目后,您将继续导入第二个 Eclipse 应用项目(如果您的工作区中有第二个)及其库项目模块(之前导入的那些被导入向导发现为现有模块并重新用于这个新模块,而不是被复制)。
So, you should never have to import even a single library project from Eclipse directly; they will all be brought in indirectly, based on their dependencies upon app projects that you import. This is assuming that all of your library projects in the workspace are created to serve the needs of one or more app projects in that same workspace.
因此,您永远不必直接从 Eclipse 导入单个库项目;根据它们对您导入的应用程序项目的依赖关系,它们都将被间接引入。这是假设您在工作区中创建的所有库项目都是为了满足同一工作区中一个或多个应用程序项目的需求。
To perform the import of this first app project, back in Android Studio, while you are in the project that you just created, select File / New / New Module. You might thinkthat you should be using File / New / Import Module, but no, you should not, because if you do that, Android Studio will create a newproject to hold your imported module, and it will import your module to thatproject. You actually couldcreate your firstmodule that way, but then the second through Nth modules would still require that you use this other method (for importing a module into an existingproject), and so I think that just starting with an "empty" project (or rather, one with its own vacuous, do-nothing placeholder module), and then importing each of your Eclipse projects as a new module into thatproject (i.e., the approach we are taking here), may be less confusing.
要执行第一个应用程序项目的导入,请返回 Android Studio,当您在刚刚创建的项目中时,选择 File / New / New Module。你可能会认为,你应该使用文件/新建/导入模块,但是没有,你应该不会,因为如果你这样做,Android的Studio将创建一个新的项目来保存您的导入模块,这将导入您的模块,该项目. 您实际上可以通过这种方式创建第一个模块,但是第二个到第 N 个模块仍然需要您使用其他方法(用于将模块导入现有的项目),所以我认为从一个“空”项目(或者更确切地说,一个有自己的空的、无所作为的占位符模块)开始,然后将每个 Eclipse 项目作为一个新模块导入到该项目中(即,我们在这里采用的方法),可能不那么令人困惑。
So, you are going to take your practically-empty new project, and perform a File / New / New Module in it. The wizard that this invokes will give you a choice of what kind of module you want to create. You must select "Import Eclipse ADT Project." That is what accesses the wizard that knows how to convert an Eclipse project into an Android Studio module (along with the library modules on which it depends) within your current Android Studio project.
因此,您将使用几乎为空的新项目,并在其中执行 File / New / New Module。此调用的向导将让您选择要创建的模块类型。您必须选择“导入 Eclipse ADT 项目”。这就是访问向导的内容,该向导知道如何将 Eclipse 项目转换为当前 Android Studio 项目中的 Android Studio 模块(以及它所依赖的库模块)。
When prompted for a source folder, you should enter the folder for your Eclipse project (this is the folder that contains that project's AndroidManifest.xml file).
当提示输入源文件夹时,您应该输入 Eclipse 项目的文件夹(这是包含该项目的 AndroidManifest.xml 文件的文件夹)。
The import wizard will then display the module name that it intends to create (similar to your original Eclipse project's name, but with a lower-case first letter because that is a convention that distinguishes module names from project names (which start with an upper-case letter). It usually works pretty well to accept this default.
然后,导入向导将显示它打算创建的模块名称(类似于原始 Eclipse 项目的名称,但首字母小写,因为这是区分模块名称和项目名称的约定(以大写字母开头) case letter). 接受这个默认值通常效果很好。
Below the module name is a section titled "Additional required modules." This will list every library required by the module you are importing (or by any of itslibraries, etc.). Since this is the first module you are importing, none of these will already be in your project, so each of them will have its Import box checked by default. You should leave these checked because you need these modules. (Note that when you import laterEclipse app projects, if a library that they need has already been imported, those libraries will still appear here, but there will be a note that "Project already contains module with this name," and the Import box will be un-checked by default. In that case, you should leave the box unchecked, so that the importer will hook up your newly-imported module(s) to the libraries that have already been imported. It may be that accepting the default names that Android Studio creates for your modules will be important for allowing the IDE to find and re-use these library modules.
模块名称下方是标题为“其他所需模块”的部分。这将列出了要导入的模块所需的每个库(或通过任何其库等)。由于这是您要导入的第一个模块,因此这些模块都不会在您的项目中,因此默认情况下,每个模块都将选中其导入框。您应该选中这些,因为您需要这些模块。(请注意,当您稍后导入Eclipse 应用程序项目时,如果已经导入了他们需要的库,则这些库仍会出现在此处,但会出现“项目已包含具有此名称的模块”的注释,以及“导入”框将是联合国- 默认检查。在这种情况下,您应该取消选中该框,以便导入程序将您新导入的模块连接到已导入的库。接受 Android Studio 为您的模块创建的默认名称可能对于允许 IDE 查找和重用这些库模块很重要。
Next, the importer will offer to replace any jars and library sources with Gradle dependencies, and to create camelCase module names for any dependent modules, checking all those options by default. You should generally leave these options checked and continue. Read the warning, though, about possible problems. Remember that you can always delete an imported module or modules (via the Project Structure dialog) and start the import process over again.
接下来,导入程序将提供用 Gradle 依赖项替换任何 jar 和库源,并为任何依赖模块创建驼峰式模块名称,默认情况下检查所有这些选项。您通常应该选中这些选项并继续。但是,请阅读有关可能出现的问题的警告。请记住,您始终可以删除导入的一个或多个模块(通过“项目结构”对话框)并重新开始导入过程。
The next display that I got (YMMV) claims that the Android Support Repository is not installed in my SDK installation. It provides a button to open the Android SDK Manager for purposes of installing it. However, that button did not work for me. I manually opened the SDK manager as a separate app, and found that the Android Support Repository was already installed. There was an update, however. I installed that, and tapped the Refresh button in the import dialog, but that did nothing. So, I proceeded, and the perceived lack of this Repository did not seem to hurt the importing process (although I did get messages regarding it being missing from time to time later on, while working with the imported code, which I was able to appease by clicking a supplied link that corrected the problem - at least temporarily). Eventually this problem went away when I installed an update to the repository, so you may not experience it at all.
我得到的下一个显示 (YMMV) 声称我的 SDK 安装中没有安装 Android Support Repository。它提供了一个按钮来打开 Android SDK 管理器以进行安装。但是,那个按钮对我不起作用。我手动打开SDK管理器作为一个单独的应用程序,发现已经安装了Android Support Repository。然而,有一个更新。我安装了它,并点击了导入对话框中的“刷新”按钮,但没有任何作用。因此,我继续进行,并且感知到缺少此存储库似乎并没有影响导入过程(尽管我确实在使用导入的代码时不时收到有关它丢失的消息,我能够安抚通过单击提供的链接来纠正问题 - 至少是暂时的)。
At this point, you will click Finish, and after a bit it should create your modules and build them. If all goes well, you should get a BUILD SUCCESSFUL message in your Gradle Console.
此时,您将单击完成,稍后它应该创建您的模块并构建它们。如果一切顺利,您应该会在 Gradle 控制台中收到一条 BUILD SUCCESSFUL 消息。
One quirk is that if the build fails, you may not see your imported modules in the Project hierarchy. It seems that you need to get to the first valid build before the new modules will appear there (my experience, anyway). You may still be able to see the new modules in the File / Project Structure dialog (e.g., if you want to delete them and start your import over).
一个怪癖是,如果构建失败,您可能不会在项目层次结构中看到您导入的模块。似乎您需要在新模块出现之前进入第一个有效构建(无论如何,我的经验)。您可能仍然能够在“文件/项目结构”对话框中看到新模块(例如,如果您想删除它们并重新开始导入)。
Remember that since you are not changing your original Eclipse projects, you can always delete the modules that you have just imported (if importing goes badly), and start all over again. You can even make changes to the Eclipse side after deleting your Android Studio modules, if that will make importing go better the second time (so long as you preserve your fallback ability to build your existing source under Eclipse). As you'll see when we discuss version control below, it may be necessary for you to retain your ability to build under Eclipse, because the project structure is changed under Android Studio, so if you need to go back to a commit that precedes your move to Android Studio (e.g., to make a bug fix), you will want to have the ability to build that earlier commit in Eclipse.
请记住,由于您没有更改原始 Eclipse 项目,因此您始终可以删除刚刚导入的模块(如果导入不顺利),然后重新开始。您甚至可以在删除 Android Studio 模块后对 Eclipse 端进行更改,如果这会使第二次导入变得更好(只要您保留在 Eclipse 下构建现有源代码的后备能力)。正如我们在下面讨论版本控制时所看到的,您可能有必要保留在 Eclipse 下构建的能力,因为项目结构在 Android Studio 下发生了变化,因此如果您需要返回到转移到 Android Studio(例如,进行错误修复),您将希望能够在 Eclipse 中构建早期提交。
To delete a module, you must select File / Project Structure, then select the module from the left side of the dialog, and then hit the delete key. For some reason, I was not able to delete a module directly in the Project hierarchy; it had to be done using this Project Structure dialog.
要删除模块,您必须选择文件/项目结构,然后从对话框左侧选择模块,然后按删除键。出于某种原因,我无法直接在项目层次结构中删除模块;它必须使用这个项目结构对话框来完成。
The import wizard generates an import-summary.txt file containing a detailed list of any issues it may have encountered, along with actions taken to resolve them. You should read it carefully, as it may provide clues as to what is happening if you have trouble building or running the imported code. It will also help you to find things that the importer moves around to accommodate the different structure of Android Studio projects.
导入向导会生成一个 import-summary.txt 文件,其中包含可能遇到的任何问题的详细列表,以及为解决这些问题而采取的措施。您应该仔细阅读它,因为如果您在构建或运行导入的代码时遇到问题,它可能会提供有关发生了什么的线索。它还将帮助您找到导入器移动的内容以适应 Android Studio 项目的不同结构。
If all does notgo well, then have at look at these possible problems that you may encounter, along with solutions for those problems:
如果一切都不顺利,那么请查看您可能遇到的这些可能的问题,以及这些问题的解决方案:
Generally speaking, there are two main kinds of problems that I encountered:
一般来说,我遇到的问题主要有两种:
- Proguard problems
- Manifest problems
- 混淆问题
- 明显问题
When Proguard is messed up, the (obfuscated) names of methods in your libraries may not match the names being used to invoke them from your app, and you will get compiler errors like "error: cannot find symbol class ..."
当 Proguard 搞砸时,您库中方法的(混淆的)名称可能与用于从您的应用程序调用它们的名称不匹配,并且您将收到诸如“错误:找不到符号类...”之类的编译器错误。
In Eclipse, Proguard stuff is pretty much ignored for library projects, with the Proguard stuff for any app project that you are building determining the obfuscation, etc. for not just itself, but for processing all of the libraries on which it depends. And that is generally what you want.
在 Eclipse 中,库项目的 Proguard 内容几乎被忽略,您正在构建的任何应用程序项目的 Proguard 内容不仅为它本身确定混淆等,还用于处理它所依赖的所有库。这通常是您想要的。
In Android Studio, however, you need to make some changes to attain this same effect. Basically, in the build.gradle files for each of your library project modules, you will want something like this:
但是,在 Android Studio 中,您需要进行一些更改才能达到相同的效果。基本上,在每个库项目模块的 build.gradle 文件中,您将需要如下内容:
buildTypes {
release {
minifyEnabled false
consumerProguardFiles 'proguard.cfg'
}
}
Where proguard.cfg is your library module's own proguard configuration file.
其中 proguard.cfg 是您的库模块自己的 proguard 配置文件。
The term "consumer" in "consumerProguardFiles" apparently refers to the app module that is using this library module. So the proguard commands from that app are used in preference to those of the library module itself, and apparently this results in obfuscations that are coordinated and compatible, so that all calls from the app module to its library modules are made with matching symbols.
“consumerProguardFiles”中的“consumer”一词显然是指使用此库模块的应用程序模块。因此,该应用程序的 proguard 命令优先于库模块本身的命令使用,显然这会导致协调和兼容的混淆,因此从应用程序模块到其库模块的所有调用都使用匹配的符号进行。
These "consumerProguardFiles" entries are not created automatically during the import process (at least that was my own experience) so you will want to make sure to edit that into your library modules' build.gradle files if they are not created for you during importing.
这些“consumerProguardFiles”条目不是在导入过程中自动创建的(至少这是我自己的经验),因此如果在导入过程中没有为您创建它们,您需要确保将其编辑到库模块的 build.gradle 文件中.
If you wanted to distribute your library projects separately, with obfuscation, then you would need an individual proguard file for them; I have not done this myself, and so that is beyond the scope of this answer.
如果你想单独分发你的库项目,并进行混淆,那么你需要一个单独的 proguard 文件;我自己没有这样做,所以这超出了这个答案的范围。
In the app module, you will want something like this:
在 app 模块中,你会想要这样的东西:
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard.cfg'
}
}
(BTW, as of this writing, while my apps are running just fine, I have not yet directly confirmed that things are actually getting obfuscated using this approach, so do check this yourself - e.g., by using a decompiler like apktool. I will be checking this later on, and will edit this answer when I get that info).
(顺便说一句,在撰写本文时,虽然我的应用程序运行良好,但我还没有直接确认使用这种方法实际上已经混淆了事情,所以请自己检查一下 - 例如,通过使用像 apktool 这样的反编译器。我会稍后检查这一点,并在我获得该信息时编辑此答案)。
The second kind of problem is due to the fact that Eclipse pretty much ignores the manifest files for library projects when compiling an app project that uses those library projects, while in Android Studio, there is an interleaving of the two that apparently does not consistently prioritize the app's manifest over those of its libraries.
第二种问题是由于 Eclipse 在编译使用这些库项目的应用程序项目时几乎忽略了这些库项目的清单文件,而在 Android Studio 中,两者的交错显然并没有始终如一地确定优先级应用程序的清单超过其库的清单。
I encountered this because I had a library manifest that listed (just for documentation purposes) an abstract Activity class as the main activity. There was a class derivedfrom this abstract class in my app that was declared in the manifest of each app that used the library.
我遇到这种情况是因为我有一个库清单,其中列出(仅用于文档目的)一个抽象的 Activity 类作为主要活动。在我的应用程序中有一个从这个抽象类派生的类,它在使用该库的每个应用程序的清单中声明。
In Eclipse, this never caused any problems, because the library manifests were ignored. But in Android Studio, I ended up with that abstract class as my activity class for the app, which caused a run-time error when the code made an attempt to instantiate that abstract class.
在 Eclipse 中,这不会引起任何问题,因为库清单被忽略了。但在 Android Studio 中,我最终使用该抽象类作为应用程序的活动类,当代码尝试实例化该抽象类时,这会导致运行时错误。
You have two choices in this case:
在这种情况下,您有两种选择:
- Use tools syntax to override specific library manifest stuff in your app manifest - for example:
- 使用工具语法覆盖应用清单中的特定库清单内容 - 例如:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.goalstate.WordGames.FullBoard.trialsuite" xmlns:tools="http://schemas.android.com/tools" . . <application tools:replace="android:name"
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.goalstate.WordGames.FullBoard.trialsuite" xmlns:tools="http://schemas.android.com/tools" . . <application tools:replace="android:name"
or,
或者,
- Strip out practically everything from your library modules' manifests, and rely upon the app module's manifest to provide every value. Note that you do need to havea manifest for each library module, but not much more is required than the header and a bare manifest element with just a package attribute in it.
- 从库模块的清单中去除几乎所有内容,并依靠应用程序模块的清单来提供每个值。请注意,您确实需要为每个库模块提供一个清单,但只需要标头和一个只有 package 属性的裸清单元素。
I tried both and ended up with approach 2., above, as the simpler method. However, if you wanted to distribute your library modules separately, you would need to create a more meaningful manifest file that reflects each library module's own individual requirements.
我尝试了这两种方法,最终得到了上面的方法 2,作为更简单的方法。但是,如果您想单独分发库模块,则需要创建一个更有意义的清单文件,以反映每个库模块自己的个别需求。
There is probably a more "correct" way to do this which puts requirements (e.g., permissions) for each library in the library manifest itself, and allows the automatic interleaving process to combine these with those declared for the app. However, given that Eclipse ignores the manifests for libraries, it seems safer at least initially to rely entirely on the app manifests and just strip the library manifests down to the bare bones.
可能有一种更“正确”的方法来做到这一点,它将每个库的要求(例如,权限)放在库清单本身中,并允许自动交错过程将这些与为应用程序声明的那些结合起来。然而,鉴于 Eclipse 忽略了库的清单,至少在最初完全依赖应用程序清单并只是将库清单剥离到裸露的骨骼似乎更安全。
Be aware that some of the project properties, and also the manifest attributes, from your Eclipse project will have been used to construct portions of your build.gradle files. Specifically, your compileSdkVersion in build.gradle is set to the project build version from the Eclipse project properties, applicationId is the package name from your app's manifest, and minSdkVersion and targetSdkVersion are also copied from the app's manifest file. The dependencies section of build.gradle comes from the library project dependencies in your project's properties.
请注意,Eclipse 项目中的一些项目属性以及清单属性将用于构建 build.gradle 文件的部分内容。具体来说,build.gradle 中的 compileSdkVersion 设置为 Eclipse 项目属性中的项目构建版本,applicationId 是应用程序清单中的包名称,minSdkVersion 和 targetSdkVersion 也是从应用程序清单文件中复制的。build.gradle 的依赖项部分来自项目属性中的库项目依赖项。
Note that this may make some of your AndroidManifest.xml values redundant and quite possibly residual (i.e., unused). This could create confusion. My understanding is that the build.gradle values are the ones that actually have an effect, and that the manifest values like targetSdkVersion are not used any more for purposes of building. However, they may still be used by app stores such as Google Play; I don't know for certain one way or the other, so at this point I am just maintaining them in tandem.
请注意,这可能会使您的某些 AndroidManifest.xml 值变得多余并且很可能是残留的(即未使用的)。这可能会造成混乱。我的理解是 build.gradle 值是实际有影响的值,并且像 targetSdkVersion 这样的清单值不再用于构建目的。但是,它们可能仍会被 Google Play 等应用商店使用;我不确定是哪种方式,所以在这一点上,我只是将它们串联起来。
Besides the above two kinds of issue, there are more routine things like importing a project that has a project build level of 22 when you have only installed SDK level 23 in Android Studio. In that situation, it is probably better to edit your app module's build.gradle file to move compileSdkVersion from 22 (the imported value) to 23, than it would be to install the SDK for level 22, but either approach should work.
除了以上两种问题外,还有一些比较常规的事情,比如在Android Studio中只安装了SDK level 23的情况下,导入项目build level为22的项目。在这种情况下,与安装 22 级的 SDK 相比,编辑应用程序模块的 build.gradle 文件以将 compileSdkVersion 从 22(导入的值)移动到 23 可能更好,但任何一种方法都应该有效。
Throughout this entire process, when something does not build properly and you make a change to try to address it, you might want to try Build / Rebuild Project and/or Tools / Android / Sync Project with Gradle Files, and/or File / Invalidate Caches/Restart, to make sure that your changes have been fully incorporated. I don't know exactly when these are truly necessary, because I don't know how much is done incrementally when you haven't yet had a successful build, but I performed them all fairly regularly as a kind of superstitious ritual, and I'm fairly certain that it helped. For example, when I got a Resources$NotFound runtime error that appeared to be from an inability to find the launch icon resource, I tried all three, and the problem was fixed.
在整个过程中,当某些内容无法正确构建并且您进行更改以尝试解决它时,您可能想要尝试构建/重建项目和/或工具/Android/同步项目与 Gradle 文件,和/或文件/无效缓存/重启,以确保您的更改已完全合并。我不知道什么时候真正需要这些,因为当你还没有成功构建时,我不知道增量完成了多少,但作为一种迷信仪式,我相当定期地执行它们,并且我我相当肯定它有帮助。例如,当我收到一个 Resources$NotFound 运行时错误似乎是由于无法找到启动图标资源而导致时,我尝试了所有三个,问题得到解决。
When you have performed the above for your first Eclipse project and have attained a successful build, then with luck, you can select your app module from the dropdown at the top of the Android Studio display to the left of the play button, then click the play button itself, then select a device or Android Virtual Device, and the app should be loaded for running.
当您为第一个 Eclipse 项目执行上述操作并成功构建后,幸运的是,您可以从播放按钮左侧的 Android Studio 显示屏顶部的下拉列表中选择您的应用程序模块,然后单击播放按钮本身,然后选择一个设备或 Android 虚拟设备,应用程序应该被加载以运行。
Likewise, you should be able to create a signed copy of your app using the Build / Generate Signed APK feature. Note that some import-related errors may appear when running your signed copy that do not appear when using the play button, so you need to confirm that both are working before deciding that your import is complete.
同样,您应该能够使用构建/生成签名 APK 功能创建应用程序的签名副本。请注意,在运行您的签名副本时可能会出现一些与导入相关的错误,而在使用播放按钮时不会出现这些错误,因此您需要在确定导入完成之前确认两者都正常工作。
Following this, you will probably want to turn on version control. I am using git myself, but there are a number of other options available.
在此之后,您可能想要打开版本控制。我自己正在使用 git,但还有许多其他选项可用。
Version control is mostly beyond the scope of this answer, but there are a few things that are affected by the importing process. First, in Eclipse you might have your various projects in various folders stuck all over the place, but when you import into Android Studio, all modules will be created as direct child folders of your main project folder. So if you had a separate git folder for each project in Eclipse, or for related groups of projects organized under a parent folder for each group (as I did), that is not going to translate very well to Android Studio.
版本控制主要超出了这个答案的范围,但有一些事情会受到导入过程的影响。首先,在 Eclipse 中,您可能会将各种文件夹中的各种项目卡在各处,但是当您导入 Android Studio 时,所有模块都将创建为主项目文件夹的直接子文件夹。因此,如果您为 Eclipse 中的每个项目或在每个组的父文件夹下组织的相关项目组(就像我所做的那样)有一个单独的 git 文件夹,那将不会很好地转换为 Android Studio。
My knowledge of this is limited as I have not worked with version control yet in Android Studio, so maybe there is a way around this, but it appears that all version control in Android Studio is unified at the project level, and so all of your modules will be under a single git archive.
我对此的了解有限,因为我还没有在 Android Studio 中使用过版本控制,所以也许有办法解决这个问题,但 Android Studio 中的所有版本控制似乎在项目级别都是统一的,所以你所有的模块将在单个 git 存档下。
This means that you may need to abandon your old git archive and start fresh with a new archive for your imported source code. And that means that you will want to keep your old git archive around, so that it can be used with Eclipse to perform any needed bug fixes, etc., at least for a while. And you also will want it to preserve a history of your project.
这意味着您可能需要放弃旧的 git 存档,并为导入的源代码使用新的存档重新开始。这意味着您需要保留旧的 git 存档,以便它可以与 Eclipse 一起使用以执行任何需要的错误修复等,至少在一段时间内。而且您还希望它保留您项目的历史记录。
If you are fortunate enough to have had all of your projects organized under a single Eclipse workspace, and if you were using a single git archive for those projects, then it is possible that you might just copy your old git archive from in and under your Eclipse workspace folder to in and under your Android Studio project folder. Then, you could edit any still-relevant .gitignore items from you Eclipse project into the auto-generated .gitignore file for your Android Studio project, and let git figure out what has been changed during the importing process (and some things willhave been moved around - for example, the manifest file is no longer at the top level of your module). Others have reported that git is pretty good at figuring out what has changed; I have not tried this myself.
如果您有幸在一个 Eclipse 工作区下组织了所有项目,并且如果您为这些项目使用单个 git 存档,那么您可能只是从内部和下面复制旧的 git 存档Eclipse 工作区文件夹到您的 Android Studio 项目文件夹中和下。然后,你可以从你编辑任何静态有关的.gitignore项目Eclipse项目到你的Android Studio项目自动生成的.gitignore文件,并让混帐搞清楚什么在导入过程中已经被改变(有些东西会一直四处移动 - 例如,清单文件不再位于模块的顶层)。其他人报告说 git 非常擅长找出发生了什么变化;我自己没有尝试过。
But even if you did this, going back to a commit that precedes your move from Eclipse to Android Studio would be going back to a set of files that would only make sense from inside Eclipse. So it sounds, well, "difficult" to work with. Especially since Eclipse will still be pointing to its original set of project folders.
但即使您这样做了,回到从 Eclipse 迁移到 Android Studio 之前的提交也将返回到一组仅在 Eclipse 内部才有意义的文件。所以听起来,好,“很难”使用。特别是因为 Eclipse 仍将指向其原始项目文件夹集。
I personally had multiple git archives for my various sets of related projects, and so I decided to just make a clean break and start git over again in Android Studio. If you had to do this, it could affect your planning, because you would want to be at a very stable point in your code development before making the move in that case, since you will lose some accessibility to that older code within your version control system (e.g., ability to merge with post-import code) once you have made the move to Android Studio.
我个人为我的各种相关项目集有多个 git 档案,因此我决定彻底打破并在 Android Studio 中重新开始 git。如果您必须这样做,它可能会影响您的计划,因为在这种情况下,您会希望在代码开发中处于非常稳定的状态,然后再进行此操作,因为您将无法在版本控制中访问该旧代码系统(例如,与导入后代码合并的能力)一旦您转移到 Android Studio。
The part of this answer that pertains to git is partly speculative, since I have not actually worked with version control yet on my imported project files, but I wanted to include it to give some idea of the challenges, and I plan to update my answer after I have worked more with version control inside Android Studio.
这个答案中与 git 有关的部分是推测性的,因为我实际上还没有在我导入的项目文件上使用版本控制,但我想包括它以了解挑战,我计划更新我的答案在我在 Android Studio 中进行了更多版本控制之后。
回答by Carl
According to http://tools.android.com/tech-docs/new-build-system/migrating-from-eclipse-projects,
You have a couple of choices
- simply importing
- pre-exporting first from Eclipse.
根据http://tools.android.com/tech-docs/new-build-system/migrating-from-eclipse-projects,您有几个选择
- 只需导入
- 首先从 Eclipse 预导出。
Pre-exporting from eclipse may be the better choice if your project contains a lot of relationships that are Eclipse-specific. A.S. cannot 'translate' everything Eclipse can produce. If you want to continue using Eclipse as well as A.S. on this project code, this is the better choice. If you choose this method, please read the above link, there are some important pre-requisites.
Simply importing into AS will let AS 'translate' and rearrange the project, and is the recommended method, especially if you have no intention of returning to Eclipse. In this case, you let the A.S. wizard do everything and you dont need to manually generate gradle files.
如果您的项目包含许多特定于 Eclipse 的关系,那么从 Eclipse 预导出可能是更好的选择。AS 无法“翻译”Eclipse 可以生成的所有内容。如果您想在此项目代码上继续使用 Eclipse 和 AS,这是更好的选择。如果您选择这种方法,请阅读上面的链接,有一些重要的先决条件。
简单地导入到 AS 会让 AS“翻译”并重新排列项目,这是推荐的方法,特别是如果您不打算返回 Eclipse。在这种情况下,您让 AS 向导完成所有工作,而无需手动生成 gradle 文件。
回答by Mitch
In addition to the answer by Scott Barta above, you may still have import problems if there are references to Eclipse workspace library files, with e.g.
除了上面 Scott Barta 的回答之外,如果引用了 Eclipse 工作区库文件,您可能仍然会遇到导入问题,例如
/workspace/android-support-v7-appcompat
being a common one.
作为一个共同的。
In this case the import will halt until you provide a reference (and if you've cloned from a git repo, it probably won't be there) and even pointing to your own install (e.g. something like /android-sdk-macosx/extras/android/m2repository/com/android/support/appcompat-v7) won't be recognised and will halt the import, leaving you in no-man's land.
在这种情况下,导入将停止,直到您提供参考(如果您从 git 存储库克隆,它可能不会在那里)甚至指向您自己的安装(例如 /android-sdk-macosx/ extras/android/m2repository/com/android/support/appcompat-v7) 不会被识别并且会停止导入,让你处于无人区。
To get around this, look for refs in the project.properties or .classpath files that came in from the Eclipse project and remove/comment them out, e.g.
要解决此问题,请在来自 Eclipse 项目的 project.properties 或 .classpath 文件中查找 refs 并删除/注释掉它们,例如
<classpathentry combineaccessrules="false" kind="src" path="/android-support-v7-appcompat"/>
That will get you past the import stage and you can then add these refs in your build.gradle (Module:app) as indicated in the Android tutorial, like below:
这将使您通过导入阶段,然后您可以按照Android 教程中的说明在 build.gradle (Module:app) 中添加这些引用,如下所示:
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
}
回答by Devashish Mamgain
Here is a simpler way to migrate:
这是一种更简单的迁移方法:
Start off with a sample Android Studio project. Open android studio and create a new project (or) just download a ‘sample' studio project here: https://github.com/AppLozic/eclipse-to-android-studio-migration. Now, open the downloaded project in Android Studio by following the below instructions.
从示例 Android Studio 项目开始。打开 android studio 并创建一个新项目(或)只需在此处下载“示例”工作室项目:https: //github.com/AppLozic/eclipse-to-android-studio-migration。现在,按照以下说明在 Android Studio 中打开下载的项目。
Import eclipse project modules into Android Studio. Select File -> New -> Import Module Image title
将 eclipse 项目模块导入 Android Studio。选择文件 -> 新建 -> 导入模块图片标题
Next, select the path of the eclipse project and import the modules. In case, if you see the message “Failed to sync Gradle project,” then click on “Install Build Tools and sync project…”
接下来,选择eclipse项目的路径并导入模块。如果您看到消息“无法同步 Gradle 项目”,请单击“安装构建工具并同步项目...”
Now, remove the sample project modules by following the below simple steps:
现在,按照以下简单步骤删除示例项目模块:
Open settings.gradle and remove “include ‘:app'” Right click on “app” module and “Delete” it. Now, what we have is the Eclipse project open in Android studio with the Gradle build.
打开 settings.gradle 并删除“include ':app'” 右键单击“app”模块并“删除”它。现在,我们拥有的是使用 Gradle 构建在 Android Studio 中打开的 Eclipse 项目。
Here are few other links which might help you:
以下是一些可能对您有所帮助的其他链接:
http://developer.android.com/sdk/installing/migrate.htmlSource: http://www.applozic.com/blog/migrating-project-from-eclipse-to-studio/
http://developer.android.com/sdk/installing/migrate.html来源:http: //www.applozic.com/blog/migrating-project-from-eclipse-to-studio/
回答by Touseef Ahmed
Simple steps: 1.Go to Android Studio. 2.Close all open projects if any. 3.There will be an option which says import non Android Studio Projects(Eclipse ect). 4.Click on it and choose ur project Thats't it enjoy!!!
简单步骤: 1.进入Android Studio。2.关闭所有打开的项目(如果有)。3. 会有一个选项说导入非 Android Studio 项目(Eclipse 等)。4.单击它并选择您的项目
回答by RAKESH KUMAR JHA
The best way to bring in an Eclipse/ADT
project is to import it directly into Android Studio. At first GO to Eclipse project
& delete the project.properties
file.
引入Eclipse/ADT
项目的最佳方式是将其直接导入到 Android Studio 中。首先GO to Eclipse project
&删除project.properties
文件。
After that, open the Android studio Tool & import Eclipse project(Eclipse ADT, Gradle etc).
之后,打开 Android studio Tool 并导入 Eclipse 项目(Eclipse ADT、Gradle 等)。