Passing Android Code Around
Author: Daniel Fowler
There are plenty of code samples around, this site included, a clean way to use (and generate) example code is needed.
Use the Eclipse Export and Import wizard to produce and grab clean code.
It is useful to be able to grab the source code for an Android project so that you can send it to colleagues, upload it to web sites, take a back up or move it to another machine. Simply zipping up the project in the Eclipse workspace means that it will contain unnecessary files, files that have been generated by Eclipse and thus do not need to be included, only the actual source code is required (usually XML files, Java class files and graphics files). Using the Eclipse Export and Import functionality only the files that need to be shipped can be selected. Try it on an existing project, exporting to another machine set up for Android development, or use your existing development machine by setting up another Workspace. (To create another Workspace in Eclipse select Switch Workspace from the file menu, then Other..., create and select the directory for the new Workspace. Eclipse will restart and open the new Workspace. The ADT configuration screen may display again, there is no need to download the SDK, select the location of the currently installed SDK.)
In Eclipse select File menu and then Export. In the Export dialog expand the General folder and select File System (to generate a Zip file select Archive File). Click Next. The dialog will show the project folders on the left and the files in the select folder on the right. All the folders, and hence files, will initially be selected (ticked). Remove the selection from the top-level folder to clear all the selections. Then select the AndroidManifest.xml file in the top folder and select the assets, res, and src folders. Some projects will have other folders that need selecting, as the project developer you will be aware of which files you have created. Choose the directory for the export, if exporting to another machine on the same network chose a shared folder. Click Finish and the selected files will be copied to the given directory or Zip file.
To Import the files into Eclipse on the other machine (or a different Workspace on the same machine) an Android project to take the files must exist. Open Eclipse and generate a new project by selecting Android Project from the New option under the File menu (it is also in the Android folder when selecting Other off the New menu). In the Project Name box on the New Android Project dialog enter the same name as the project that is going to be imported. Select Next. Set the Build Target (usually set to be the same as the importing project, it can be changed later if required). Select Next. Give the same Package Name as that of the importing package, if in doubt look at the directory structure under the src folder in the exported files. For example the Java file stored in src/com/example/main.java would have a Package Name of com.example. Uncheck the Create Activity option.
Click Finish. The new project will be created, Eclipse will report errors because the project will not compile until the exported project files are imported. With the new project selected in the Eclipse Package Explorer select the Import option from the File menu. Again select File System (or Archive File if importing from a Zip file) under General on the Import dialog, then select Next. Browse to the directory containing the exported files (or Zip file). Make sure all the files are checked for import. Check the Overwrite existing resources without warning box to prevent messages about files being overwritten shown, this is a new project so nothing is being lost. Click Finish.
With the files imported the project should compile and run. If not Eclipse will show any errors that need addressing. During the import a message box with the title Launch Configuration Update may appear. It is likely to display The package definition in the manifest changed. Do you want to update your Launch Configuration? Select Yes. The Package Name selected for the new project is likely not to match the Package Name in the files being imported, probably due to a typing mistake or case difference when naming the package. This can be checked when the files have finished importing. Open AndroidManifest.xml and make a note of the package attribute on the manifest element. Check that it matches the package name under the src folder in the project tree. With the project selected in the Package Explorer use the File menu to select Properties. Select Run/Debug. Select the project name and click the Edit button, either Launch Default Activity is selected or Launch is set to the correct starting Activity in the correctly named package. Make any changes that are not correct, use the Refactor option on the File menu to rename a package.