开发者

How to start an android project with downloaded sample code

I开发者_StackOverflow社区'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.

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

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

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

Thanks in advance, yokyo


@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.

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.

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.

If you want the sample to not exist 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'.

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.

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.

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.

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.


  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. 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


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.


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).

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).

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


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.


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

It states here:

"Using 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."

I hope this helps you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜