Manually generate R.java

2019-01-28 12:32发布

I am currently working on a project to automatically generate very simple Android apps from UML models. Both the Java parts and the XML parts are working ok, but I have run into a problem: the R.java file.

As I see it, I have to options of generating the R.java file.

  1. The preferred way would be to make the Android SDK generate it for me, based on the XML files. But how could I do that? I can not open Eclipse and choose "Clean project", as I would normally do, since this is supposed to be an automated process. Is there any command line commands I could run? Something like apk -generateR -/path/R.java would be perfect.
  2. The other possibility I see is to generate it myself, by simply enumerating all resources and outputting a file with the R.java format.

If alternative 2 is the only way, the main issue I have is how to generate the numbers. A typical sequence of items in R.java is as follows:

    public static final int button_buy=0x7f020000;
    public static final int button_search=0x7f020001;
    public static final int cover=0x7f020002;
    ...
    public static final int Artist_name=0x7f05000e;
    public static final int Artist2_name=0x7f05001c;
    public static final int Artist2NameLabel=0x7f05001d;

How is that generated? Are the numbers important? Would everything work now (and in future versions?) if I simply started on zero, and counted my way through them?

    public static final int button_buy=0x00000000;
    public static final int button_search=0x00000001;
    public static final int cover=0x00000002;
    ...
    public static final int Artist_name=0x00000003;
    public static final int Artist2_name=0x00000004;
    public static final int Artist2NameLabel=0x00000005;

1条回答
SAY GOODBYE
2楼-- · 2019-01-28 12:53

You can use ant scripts to build your project. R.java will be generated automatically as if you use "Clean project" option in Eclipse.

Read more here: http://developer.android.com/tools/projects/projects-cmdline.html

I wouldn't recommend to mess around with R.java file directly.

查看更多
登录 后发表回答