• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<p>This sample demonstrates how an application can make use of shared code and
2resources stored in an Android library project.</p>
3
4<p>In this case, the TicTacToeMain application project includes a reference to
5the TicTacToeLib library project. When you build the TicTacToeMain application,
6the build tools look for the library project and compile it's code and resources
7as part of the main application's <code>.apk</code> file. The main application
8is designed with a launcher activity called <code>MainActivity</code>, shown at
9left, below. When the user presses a button in the View,
10<code>MainActivity</code> starts a second activity, <code>GameActivity</code>,
11which is declared in the library project. </p>
12
13<ul>
14<li>The main application's <a
15href="AndroidManifest.html">AndroidManifest.xml</a> file contains declarations
16of both <code>MainActivity</code>, which is implemented locally in main project,
17and <code>GameActivity</code>, which is implemented in the library project.
18Currently, an application must declare in its manifest any components or
19resources that it is using from a library project.</li>
20<li><a
21href="src/com/example/android/tictactoe/MainActivity.html">MainActivity.java</a>
22shows how a class in the main application imports and uses
23classes from the library project. The Activity sets listeners on the buttons in
24the main View and, when one of them is clicked, creates an explicit Intent
25targeted at the <code>GameActivity</code> component declared in the TicTacToeLib
26library project. </li>
27</ul>
28
29<p>The TicTacToeLib library project includes a single Activity,
30<code>GameActivity</code>, that handles most of the application lifecycle.</p>
31
32<ul>
33<li>The library project includes an <a
34href="../TicTacToeLib/AndroidManifest.html">AndroidManifest.xml</a> file that
35declares <code>GameActivity</code>.</li>
36<li><a
37href="../TicTacToeLib/src/com/example/android/tictactoe/library/GameActivity.html">GameActivity.java</a>
38handles most of the application lifecycle and manages general game play.</li>
39<li><a
40href="../TicTacToeLib/src/com/example/android/tictactoe/library/GameView.html">GameView.java</a>
41renders the UI of the game and manages interaction events during game play.</li>
42</ul>
43
44<p>If you want to build the TicTacToeMain application, you can obtain it by
45downloading the "Samples for SDK API 8" component (or higher version) into your
46SDK, using the <em>Android SDK and AVD Manager</em>. Note that the application
47project depends on code and resources found in the TicTacToeLib library project
48&mdash; in order to build TicTacToeMain, you need to add both projects to your
49development environment. </p>
50
51<p>To build an application that uses a library project, you also need to update
52to the latest version of the SDK tools (r6 or higher) and Android platforms, as
53well as the latest version of ADT (0.9.7 or higher), if you are developing in
54Eclipse. </p>
55
56<p>For information about how to set up Android library projects, refer to
57<a href="../../../guide/developing/eclipse-adt.html#libraryProject">Developing
58in Eclipse with ADT</a> or <a
59href="../../../guide/developing/other-ide.html#libraryProject">Developing in
60Other IDEs</a>, depending on your environment.</p>
61
62<img alt="Screenshot of the main application" src="../images/TicTacToeMain.png" />
63<img alt="Screenshot of an Activity declared in a library project" src="../images/TicTacToeLib.png" />
64