Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start an android project with downloaded sample code

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.

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

like image 777
yokyo Avatar asked Jun 01 '10 15:06

yokyo


1 Answers

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

like image 184
Neutrino Avatar answered Sep 29 '22 12:09

Neutrino