Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since Android makes the <code>R</code> class automatically with resource files under the <code>/res</code> folder, using the <code>R</code> class as <code>final static</code> is impossible.</p> <p>I found a nice solution to use a <code>jar</code> file with the <code>res</code> files. Here is how I did it:</p> <ol> <li><p>In your source code which will be exported in the <code>jar</code> file, <strong>don't use</strong> <code>R</code> variable because it will be replaced with a final static memory address in compile time. Instead of using <code>R</code>, I made my own method below:</p> <pre><code>public static int getResourceIdByName(String packageName, String className, String name) { Class r = null; int id = 0; try { r = Class.forName(packageName + ".R"); Class[] classes = r.getClasses(); Class desireClass = null; for (int i = 0; i &lt; classes.length; i++) { if (classes[i].getName().split("\\$")[1].equals(className)) { desireClass = classes[i]; break; } } if (desireClass != null) { id = desireClass.getField(name).getInt(desireClass); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } return id; } </code></pre> <p>For example, if you have a layout named <code>main.xml</code>, you can get it by calling the method:</p> <pre><code>int id = getResourceIdByName(context.getPackageName(), "layout", "main"); </code></pre> <p>If you have a string whose <code>id</code> is "text1", you can get it by calling method</p> <pre><code>int id = getResourceIdByName(context.getPackageName(), "string", "text1"); </code></pre> <p>This method gives you your resource id at runtime. It uses the reflection API to get the status of <code>R</code> at runtime.</p> <p>By using this method you can avoid using the <code>R</code> variable.</p></li> <li><p>Copy your <code>res</code> files to your target project.</p></li> <li><p>Build.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload