title: Best practice: Extending or overriding an Android library project class
body: <p>We're using an <a href="http://developer.android.com/guide/developing/projects/index.html#LibraryProjects" rel="nofollow noreferrer">Android Library Project</a> to share core classes and resources across different builds (targets) of our Android application. The Android projects for each specific target <a href="http://developer.android.com/guide/developing/projects/projects-eclipse.html#ReferencingLibraryProject" rel="nofollow noreferrer">reference the Core library project</a> (behind the scenes, Eclipse creates and references a jar from the referenced library project).</p>
  
  <p>Overriding resources such as images and XML layouts is easy. Resource files placed in the target project, such as the app icon or an XML layout, automatically override the core library's resources with the same name when the app is built. However, sometimes a class needs to be overridden to enable target-specific behavior. For example, the Amazon target preferences screen cannot contain a link to the Google Play app page, requiring a change in the Amazon project's preferences.xml and preferences Activity class.</p>
  
  <p>The goal is to reduce the amount of duplicate code among target projects while removing as much target-specific code from the Core library as possible. We've come up with a couple of approaches to implement logic specific to different targets:</p>
  
  <ol>
  <li>Write the target-specific functions within Core library classes and use if/switch blocks to select behavior based on product SKU. This approach is not very modular and bloats the Core library codebase.</li>
  <li>Extend the particular Core class in a target project and override the base (Core) class functions as needed. Then keep a reference to the base-class object in the Core library and instantiate it with an extended class object (from <a href="https://stackoverflow.com/questions/6564133/android-library-project-how-to-overwrite-a-class">How to override a class within an Android library project?</a>)</li>
  </ol>
  
  <p>Are there other strategies to override or extend an Android library project class? What are some of the best practices for sharing and extending common classes among Android app targets?</p>