如何使用下载的示例代码启动一个 android 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2951068/
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 08:01:31  来源:igfitidea点击:

How to start an android project with downloaded sample code

androideclipse

提问by yokyo

I'm pretty new to android, and just finished setup my environment and reading some tutorials. Then I got sdk samples from the web. Okay, what I wanna ask you is that is there a way to start a sample as a project in the Eclipse? I mean like clicking a project file in c# or a solution file.

我对 android 很陌生,刚刚完成了我的环境设置并阅读了一些教程。然后我从网上得到了 sdk 样本。好的,我想问你的是,有没有办法在 Eclipse 中将示例作为项目启动?我的意思是在 c# 或解决方案文件中单击项目文件。

Here is my folder which I unzipped the code, \android-sdk-windows\samples\android-8\NotePad

这是我解压缩代码的文件夹,\android-sdk-windows\samples\android-8\NotePad

There are three folders and one file in the folder, \res, \src, \tests, and AndroidManifest.xml

文件夹一共有三个文件夹一个文件,\res、\src、\tests、AndroidManifest.xml

Or, do I need to make a new android project and import (or add files? maybe) them?

或者,我是否需要创建一个新的 android 项目并导入(或添加文件?也许)它们?

Thanks in advance, yokyo

提前致谢,yokyo

采纳答案by sgarman

  1. From eclipse go to: File > New > Project...
  2. Select the Android Folder and pick Android Project
  3. Then in the Contents section at the top click the "Create project from existing sample" radio button. Once you pick a target from the bottom the drop down will become selectable and your good to go.
  1. 从 Eclipse 转到:文件 > 新建 > 项目...
  2. 选择 Android 文件夹并选择 Android 项目
  3. 然后在顶部的 Contents 部分中单击“从现有示例创建项目”单选按钮。一旦你从底部选择了一个目标,下拉菜单就会变成可选的,你就可以开始了。

回答by Neutrino

@sgarman

@sgarman

I don't think that's a very good way of doing it. Sadly the 'Create Project from existing sample' feature you describe functions in such a way that when it creates a project from a sample it leaves you editing the source code in the actual SDK sample itself instead of a copy of the source imported into your Eclipse workspace.

我不认为这是一个很好的方法。遗憾的是,“从现有示例创建项目”功能以这样一种方式描述功能,当它从示例创建项目时,它会让您编辑实际 SDK 示例本身中的源代码,而不是将源代码的副本导入到 Eclipse工作区。

This is problematic for a number of reasons including:

这是有问题的,原因有很多,包括:

  1. Once you've edited the new project you no longer have the original sample to refer to, which is the whole point of the samples in the first place.

  2. If you want to hack a sample in several different ways you will want several copies of the sample, but again once you've edited the original sample you no longer have the original sample to make a copy from.

  3. If the SDK is ever patched then when you update it you may end up overwriting your code.

  1. 一旦您编辑了新项目,您就不再拥有可参考的原始样本,这首先是样本的全部意义所在。

  2. 如果您想以多种不同的方式破解一个样本,您将需要该样本的多个副本,但是一旦您编辑了原始样本,您将不再拥有原始样本来制作副本。

  3. 如果 SDK 曾经打过补丁,那么当您更新它时,您最终可能会覆盖您的代码。

In short it makes far more sense to treat the SDK as a readonly reference and not treat it as a sandpit in which to do your own messing around. So imo the best way to create an Eclipse project from a sample is do take a copy of the sample and put it somewhere else.

简而言之,将 SDK 视为只读参考,而不是将其视为可以自己乱搞的沙坑更有意义。因此,imo 从示例创建 Eclipse 项目的最佳方法是获取示例的副本并将其放在其他地方。

If you want the sample to notexist in your actual Eclipse workspace directory then this is very easy. Just copy the sample to a new location and inside Eclipse with your workspace open do New->Android Project and 'Create Project From Existing Source'.

如果您希望示例存在于您的实际 Eclipse 工作区目录中,那么这很容易。只需将示例复制到一个新位置并在 Eclipse 中打开您的工作区,然后执行 New->Android Project 和“Create Project From Existing Source”。

If on the other hand you want to make the project inside your Eclipse workspace directory then there is a problem which is that if you just copy the sample code inside your workspace folder and do 'New->Android Project' and 'Create Project From Existing Source' for me at least it fails with the error "Invalid project description: c:\Users\usernamme\blah\blah\projectname overlaps the location of another project projectname". I don't know why, if you create a project from sample code using 'New->Java Project' it works just fine so I suspect there is a problem with the Android Project Eclipse plugin that is causing this to fail.

另一方面,如果您想在 Eclipse 工作区目录中创建项目,则存在一个问题,即如果您只是将示例代码复制到工作区文件夹中并执行“新建->Android 项目”和“从现有项目创建项目” Source' 至少对我来说它失败并显示错误“无效的项目描述:c:\Users\usernamme\blah\blah\projectname 与另一个项目 projectname 的位置重叠”。我不知道为什么,如果您使用“New->Java Project”从示例代码创建一个项目,它就可以正常工作,所以我怀疑 Android Project Eclipse 插件存在问题导致此失败。

There is a way to get around this which is to first copy the sample code to a temporary location on disk that is outside of the workspace directory. Then use New->Android Project and 'Create Project From Existing Source' which turns the temporary copy of the sample code into a project but leaves it orphaned outside the workspace directory. Then delete the project from the workspace (but without deleting the project from disk). Then use Import->Existing Projects Into Workspace with the 'Copy Projects Into Workspace' checkbox ticked to copy the project into the workspace directory, before finally deleting the project from the temporary location.

有一种方法可以解决这个问题,即首先将示例代码复制到磁盘上工作区目录之外的临时位置。然后使用 New->Android Project 和“从现有源创建项目”将示例代码的临时副本转换为项目,但将其留在工作区目录之外。然后从工作区中删除该项目(但不从磁盘中删除该项目)。然后使用 Import->Existing Projects Into Workspace 并勾选“Copy Projects Into Workspace”复选框将项目复制到工作区目录中,最后从临时位置删除项目。

But ultimately I decided to structure my workspace in such a way that projects are not inside the workspace folder like this:

但最终我决定以这样一种方式构建我的工作区,即项目不在工作区文件夹中,如下所示:

Eclipse Workspaces
\
 Android Projects
 \
  Workspace
  Project 1
  Project 2

 Java Projects
 \
  Workspace
  Project 1
  Project 2

 Other Projects
 \
  Workspace
  Project 1
  Project 2

In this layout the Android Projects, Java Projects and Other Projects directories are conceptually my workspaces but in reality in each case it is the nested Workspace folder which is the actual Eclipse workspace. This enables me to keep my projects contained within their respective pseudo-workspaces while not being inside their actual Eclipse workspace folder which neatly gets around the problem of not being able to easily create Android projects if the project directory is inside the Eclipse workspace directory.

在这个布局中,Android 项目、Java 项目和其他项目目录在概念上是我的工作区,但实际上在每种情况下它都是嵌套的工作区文件夹,它是实际的 Eclipse 工作区。这使我能够将我的项目包含在它们各自的伪工作区中,而不是在它们实际的 Eclipse 工作区文件夹中,这巧妙地解决了如果项目目录在 Eclipse 工作区目录中则无法轻松创建 Android 项目的问题。

Sorry that's all a bit long winded, but it's taken me ages to work out how to get this to work efficiently and I figure it might help someone.

对不起,这有点啰嗦,但我花了很长时间才弄清楚如何让它有效地工作,我认为它可能对某人有所帮助。

回答by Murali-Hassan

  1. Copy the downloaded project into your workspace i.e space allocated for android project
  2. Open the Eclipse
  3. Choose File---> Import ---> Android --> Existing Android code into workspace
  4. Choose Next
  5. Click Browse
  6. choose the foldername (downloaded)
  7. click finish
  1. 将下载的项目复制到您的工作区,即为 android 项目分配的空间
  2. 打开日食
  3. 选择 File---> Import ---> Android --> Existing Android code into workspace
  4. 选择下一步
  5. 点击浏览
  6. 选择文件夹名称(已下载)
  7. 点击完成

回答by Munim Dibosh

If you are using ADT 20 then it's quite easy. Select New > Other...>Android Sample Project > [Select the platform from which you would like to pick the sample] > You must see the available samples, select the one you wish to open,you will see that in your workspace.

如果您使用的是 ADT 20,那么这很容易。选择 New > Other...>Android Sample Project > [选择您想要从中选择示例的平台] > 您必须看到可用的示例,选择要打开的示例,您将在工作区中看到它。

回答by edayangac

http://developer.android.com/samples/index.html

http://developer.android.com/samples/index.html

It states here:

它在这里指出:

"Using Android Studio

“使用 Android Studio

Unpack the downloaded project package. In Android Studio, chose File > Import Project and select the root folder of the unpacked project. Android Studio may ask you to choose the type of project you are importing. If this is the case, make sure to choose Import project from external model and select the Gradle option."

解压下载的工程包。在 Android Studio 中,选择 File > Import Project 并选择解压后的项目的根文件夹。Android Studio 可能会要求您选择要导入的项目类型。如果是这种情况,请确保选择从外部模型导入项目并选择 Gradle 选项。”

I hope this helps you.

我希望这可以帮助你。

回答by adamp

The code samples don't come with eclipse project files out of the box. There are a number of ways to create them but the easiest is probably this. From eclipse (with the android plugin installed), File -> New -> Project, Android Project, Next, select the "Create project from existing source" radio button and specify the location of the existing source. The project wizard should then find your manifest and fill out the rest of the information it needs automatically.

代码示例没有随 Eclipse 项目文件一起提供。有很多方法可以创建它们,但最简单的方法可能是这个。从 eclipse(安装了 android 插件),文件 -> 新建 -> 项目,Android 项目,接下来,选择“从现有源创建项目”单选按钮并指定现有源的位置。然后,项目向导应该会找到您的清单并自动填写所需的其余信息。

回答by Rich

This may not be a definitive answer because I'm relatively new to Android as well (and come from more of a .Net background too).

这可能不是一个明确的答案,因为我对 Android 也比较陌生(并且来自更多的 .Net 背景)。

When you create a new Workspace in Eclipse, you'll see a new folder created with the name of your workspace and a .metadata folder inside of it. I don't think there's a "workspace file" per se, so you don't double click anything to bring it back up. You just open to that workspace (by telling the Eclipse dialog what path to open).

当您在 Eclipse 中创建一个新的工作区时,您将看到一个使用工作区名称创建的新文件夹和一个 .metadata 文件夹。我认为本身没有“工作区文件”,因此您无需双击任何内容将其恢复。您只需打开该工作区(通过告诉 Eclipse 对话框打开的路径)。

As far as importing existing projects into a workspace, copy the whole folder into your workspace and click File - Import... - General - Existing Projects into Workspace. Click the Browse button to select the Root directory and it should default to the Workspace's main folder. Click OK and the project should show up in the dialog. Select it and click Finish

至于将现有项目导入工作区,请将整个文件夹复制到工作区,然后单击文件 - 导入... - 常规 - 现有项目到工作区。单击浏览按钮选择根目录,它应该默认为工作区的主文件夹。单击“确定”,项目应显示在对话框中。选择它并单击完成