Why do I have to import R class when I use resource in a non-root package?

android

Solution

Your `R` class lives in your application package `com.example.test` -- check out it's package declaration. If you want to reference it from any other package, you need to import it or fully qualify its name, like any other class.

Problem

Whenever I want to use any resource element in the non-root package, I have to import my own `R class` (not Android R class). For example, Root package `com.example.test` and a file `Main.java` ``` @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } ``` No need to import `R class`. When I create another package, `com.example.test.something` and a new class in there `Something.java`, I have to import my own `R.class` ``` import com.example.test.R; //...other code... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } ``` This happens automatically when I move a Java file from root to non-root package. Why is this so important?

Original source