title: Custom Views and attributes used across Android apps
body: <p>I've developed several Android apps now and have created a code base of classes that I frequently use in more than one app.  This code is all in a subversion (svn) repository with each app in its own repository.  Each app then has svn:externals references for the needed packages (e.g., com.company.android.views).  This works great except in the case when the R class has to be imported for custom attributes.</p>
  
  <p>A custom view has an import like this:</p>
  
  <pre><code>import com.company.apps.myapp.R
  </code></pre>
  
  <p>so that it can have code like this:</p>
  
  <pre><code>attributes.getBoolean(R.styleable.WebImageView_autoload, autoload)
  </code></pre>
  
  <p>That custom attribute is defined in res/values/attrs.xml:</p>
  
  <pre><code>&lt;declare-styleable name="WebImageView"&gt;
      &lt;attr name="autoload" format="boolean" /&gt;
      ...others
  &lt;/declare-styleable&gt;
  </code></pre>
  
  <p>This works perfectly, but the problem comes when I have a second app that uses this same view.  Now I update the import to <code>import com.company.apps.anotherapp.R</code> so that it will work with "anotherapp" and that breaks it with "myapp."  When working on several apps at once, this becomes an issue.</p>
  
  <p>My temporary solution has been to check in an update to the applicable classes and then lock the svn:externals to that specific revision.  Each app ends up being locked to a different revision, which gets messy fast, but that still seems better than copying the various classes into the app's repo directly.</p>
  
  <p>The only other solution I've thought of it using reflection, something like:</p>
  
  <pre><code>Class class = Class.forName(context.getPackageName() + ".R");
  Field[] fields = class.getDeclaredFields();
  </code></pre>
  
  <p>And then loop through the fields, assigning the ones I care about to variables that are used throughout the class.  This seems rather heavy-handed though, especially when we could be talking about several classes needing to do this.</p>
  
  <p>How can I solve this issue?  Is there a way to dynamically import the com.company.apps.*.R or to somehow generate a different R class that doesn't depend on the specific app?  Or is there some other obvious (or not so obvious) solution I've totally missed?</p>