|
| 1 | +<p>This sample demonstrates how an application can make use of shared code and |
| 2 | +resources stored in an Android library project.</p> |
| 3 | + |
| 4 | +<p>In this case, the TicTacToeMain application project includes a reference to |
| 5 | +the TicTacToeLib library project. When you build the TicTacToeMain application, |
| 6 | +the build tools look for the library project and compile it's code and resources |
| 7 | +as part of the main application's <code>.apk</code> file. The main application |
| 8 | +is designed with a launcher activity called <code>MainActivity</code>, shown at |
| 9 | +left, below. When the user presses a button in the View, |
| 10 | +<code>MainActivity</code> starts a second activity, <code>GameActivity</code>, |
| 11 | +which is declared in the library project. </p> |
| 12 | + |
| 13 | +<ul> |
| 14 | +<li>The main application's <a |
| 15 | +href="AndroidManifest.html">AndroidManifest.xml</a> file contains declarations |
| 16 | +of both <code>MainActivity</code>, which is implemented locally in main project, |
| 17 | +and <code>GameActivity</code>, which is implemented in the library project. |
| 18 | +Currently, an application must declare in its manifest any components or |
| 19 | +resources that it is using from a library project.</li> |
| 20 | +<li><a |
| 21 | +href="src/com/example/android/tictactoe/MainActivity.html">MainActivity.java</a> |
| 22 | +shows how a class in the main application imports and uses |
| 23 | +classes from the library project. The Activity sets listeners on the buttons in |
| 24 | +the main View and, when one of them is clicked, creates an explicit Intent |
| 25 | +targeted at the <code>GameActivity</code> component declared in the TicTacToeLib |
| 26 | +library 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 |
| 34 | +href="../TicTacToeLib/AndroidManifest.html">AndroidManifest.xml</a> file that |
| 35 | +declares <code>GameActivity</code>.</li> |
| 36 | +<li><a |
| 37 | +href="../TicTacToeLib/src/com/example/android/tictactoe/library/GameActivity.html">GameActivity.java</a> |
| 38 | +handles most of the application lifecycle and manages general game play.</li> |
| 39 | +<li><a |
| 40 | +href="../TicTacToeLib/src/com/example/android/tictactoe/library/GameView.html">GameView.java</a> |
| 41 | +renders 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 |
| 45 | +downloading the "Samples for SDK API 8" component (or higher version) into your |
| 46 | +SDK, using the <em>Android SDK and AVD Manager</em>. Note that the application |
| 47 | +project depends on code and resources found in the TicTacToeLib library project |
| 48 | +— in order to build TicTacToeMain, you need to add both projects to your |
| 49 | +development environment. </p> |
| 50 | + |
| 51 | +<p>To build an application that uses a library project, you also need to update |
| 52 | +to the latest version of the SDK tools (r6 or higher) and Android platforms, as |
| 53 | +well as the latest version of ADT (0.9.7 or higher), if you are developing in |
| 54 | +Eclipse. </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 |
| 58 | +in Eclipse with ADT</a> or <a |
| 59 | +href="../../../guide/developing/other-ide.html#libraryProject">Developing in |
| 60 | +Other 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" /> |
0 commit comments