Wednesday, May 12, 2010

Creating an Android 'Library'

In some instances, you might want Android classes available to multiple Android applications. To simply add an Android project to the another Android project's build path will not work (a good explanation is here: http://groups.google.com/group/android-developers/browse_thread/thread/5537ae10e4143240). A workaround is summarized below:

1. Create a new Android project.
2. Add any necessary classes and interfaces (including AIDLs)
3. Allow the AIDLs to compile and generate Java code.
4. Update the .project file. This file is located in the root of the Android project. It contains the following contents:

1:  <?xml version="1.0" encoding="UTF-8"?> 
2: <projectDescription>
3: <name>[project name]</name>
4: <comment></comment>
5: <projects>
6: </projects>
7: <buildSpec>
8: <buildCommand>
9: <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
10: <arguments>
11: </arguments>
12: </buildCommand>
13: <buildCommand>
14: <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
15: <arguments>
16: </arguments>
17: </buildCommand>
18: <buildCommand>
19: <name>org.eclipse.jdt.core.javabuilder</name>
20: <arguments>
21: </arguments>
22: </buildCommand>
23: <buildCommand>
24: <name>com.android.ide.eclipse.adt.ApkBuilder</name>
25: <arguments>
26: </arguments>
27: </buildCommand>
28: </buildSpec>
29: <natures>
30: <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
31: <nature>org.eclipse.jdt.core.javanature</nature>
32: </natures>
33: </projectDescription>
Remove the following elements from the buildSpec:
1:   <buildCommand>  
2: <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
3: <arguments>
4: </arguments>
5: </buildCommand>
6: <buildCommand>
7: <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
8: <arguments>
9: </arguments>
10: </buildCommand>
11: <buildCommand>
12: <name>com.android.ide.eclipse.adt.ApkBuilder</name>
13: <arguments>
14: </arguments>
15: </buildCommand>

Remove the following element from the natures:

1:   <nature>com.android.ide.eclipse.adt.AndroidNature</nature>  

5. Save the updated .project file.
6. Refresh the Android project in Eclipse.
7. Clean the project (select the project folder, select Project > Clean... from the main menu)
8. Remove the R.java file from the generated files.

Your project should now be ready to be added to the build path of other Android projects.

No comments:

Post a Comment