Proxying R.java

2019-08-30 22:31发布

I am sorry if the question title is a bit cryptic. I simply could not come up with a better one.

I have a project where I wanted to play around a bit with the package name in the AndroidManifest. Please don't ask why I did it or why I did not simply use build.gradle's applicationId.

The thing is, in order not to have to constantly correct the import for R.java everywhere when modifying the package name, I created a proxy class for R.java that looks like this:

import static com.package.main.R.*;

public class RProxy {
    public static final class R{
        public static anim anim;
        public static attr attr;
        public static bool bool;
        public static color color;
        public static drawable drawable;
        public static dimen dimen;
        public static id id;
        public static integer integer;
        public static layout layout;
        public static menu menu;
        public static plurals plurals;
        public static raw raw;
        public static string string;
        public static style style;
        public static styleable styleable;
    }
}

I now simply changed the R import in every class to RProxy, and every usage of R to RProxy.R. I now only have to change the R import once, in RProxy. The whole thing is compiling and running.

Now, here are my questions:

  1. I am now getting a few warnings (one per RProxy.R usage, to be exact) saying: Static member com.package.main.R.id.firstId accessed via instance reference. I understand why I get them, but I don't know how to do the proxying in another way. How could I get rid of these?
  2. Should I avoid this kind of hack? Why?
  3. Would there have been a better way to do that kind of Proxying?

Please note my main interest in getting an answer is mainly educational purpose (good old curiousity), but that does not mean I won't highly appreciate any answer!

0条回答
登录 后发表回答