Without Activity, it is not possible to use the R class. If you have a test application in your library, the test application will be able to access R, but not from the library itself.
However, you can access resources by name. For example, I have such a class inside my library,
public class MyContext extends ContextWrapper { public MyContext(Context base) { super(base); } public int getResourceId(String resourceName) { try{
(see fooobar.com/questions/24999 / ... for more information on ContextWrappers)
And the constructor of the object in the library takes this context wrapper,
public class MyLibClass { public MyLibClass(MyContext context) { int resId = context.getResourceId("a_file_inside_my_lib_res"); } }
Then from the application using lib, I have to pass the context,
public class MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { MyLibClass a = new MyLibClass(new MyContext(this)); } }
MyContext, MyLibClass and a_file_inside_my_lib_res, all of them live inside the library project.
Hope this helps.
source share