javaandroidandroid-studiodelphir.java-file

How to manually generate R.java


My project (made in Delphi) need to manually generate for each package it's include the R.java file manually. Actually I put all resources of each library in a single .res directory and I run aapt.exe on this directory for each package like this for android.support.compat:

"aapt.exe" package -v -f -m 
  -M "\libraries\android.support.compat\AndroidManifest.xml"
  -I "C:\SDKs\android\platforms\android-32\android.jar" 
  -S ".\res"
  -J ".\tmp\src" 
  --auto-add-overlay 
  --output-text-symbols .\tmp\

and this for each package ex for android.support.coreui

"aapt.exe" package -v -f -m 
  -M "\libraries\android.support.coreui\AndroidManifest.xml"
  -I "C:\SDKs\android\platforms\android-32\android.jar" 
  -S ".\res"
  -J ".\tmp\src" 
  --auto-add-overlay 
  --output-text-symbols .\tmp\

The problem is that at the end I have for each package the same R.java that is quite big because it's include all resources ids of the whole App. ex :

package androidx.core;

public final class R {
   public static final class styleable {
      public static final int[] ActionBar = new int[]{2130771969, 2130771971, 2130771972, 2130771973, 2130771974, 2130771975, 2130771976, 2130771977, 2130771978, 2130771979, 2130771980, 2130771981, 2130771982, 2130771983, 2130771984, 2130771985, 2130771986, 2130771987, 2130771988, 2130771989, 2130771990, 2130771991, 2130771992, 2130771993, 2130771994, 2130771995, 2130771996, 2130771997, 2130772082};
      public static final int ActionBar_background = 10;
      public static final int ActionBar_backgroundSplit = 12;
....

and

package android.support.coreui;

public final class R {
   public static final class styleable {
      public static final int[] ActionBar = new int[]{2130771969, 2130771971, 2130771972, 2130771973, 2130771974, 2130771975, 2130771976, 2130771977, 2130771978, 2130771979, 2130771980, 2130771981, 2130771982, 2130771983, 2130771984, 2130771985, 2130771986, 2130771987, 2130771988, 2130771989, 2130771990, 2130771991, 2130771992, 2130771993, 2130771994, 2130771995, 2130771996, 2130771997, 2130772082};
      public static final int ActionBar_background = 10;
      public static final int ActionBar_backgroundSplit = 12;

Is there a way to generate for each package a R.java with only the resource ids need by the package?


Solution

  • You can (and should) use at least one of these:


    For an elaboration on purpose and structure of resource IDs in R.java you might take a look at this Stackoverflow thread.

    Note, that it's highly unrecommended to manipulate R.java manually. As it is written in B.Burd & J.P.Mueller, Android Application Development All-in-One For Dummies, 3rd Ed., 2020, Wiley, pp.87 (a serious book, despite the name) in respect to R.java and Android Studio:

    Each Android project has an R.java file. Android Studio generates this file and protects it as if the file were made of gold. You, the developer, never create or modify the R.java file’s text.

    as well as

    Android’s documentation tells you to put R.java and its hex values out of your mind, and that’s probably good advice.

    and finally

    You cannot edit R.java. Long after the creation of a project, your IDE continues to monitor (and, if necessary, update) the contents of the R.java file. If you delete R.java, Android Studio recreates the file. If you edit R.java, Android Studio undoes your edit.

    Though I don't agree on such kind of paternalism, one really shouldn't manually edit R.java (even if one could when not using Android Studio, like in your case). As I've put it once somewhere:

    Though human-readable, this file should never be edited manually as it is highly probable to crash the app because of erroneous references. There is no good reason imaginable why one should want to edit R.java anyway.

    The last sentence would also be valid in your case.