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