13

I am trying to build a library. I have an Android library project and some resources under the res directory that I want to access in the library project's code. The Android docs says:

source code in the library module can access its own resources through its R class

But I just can't figure out how to do this. Because it's a library and intended to be used from other applications, not run itself, I have no Activity, so I can't get a Context to use getResources(). How can I access these resources explicitly without a context?

sqshemet
  • 143
  • 1
  • 7
  • By library you mean library project or JAR/AAR file? You can access resources only from library projects or AAR files, not JAR files. – shkschneider Jan 19 '15 at 16:02
  • Library project, not a JAR file. – sqshemet Jan 19 '15 at 16:07
  • To be fair, I don't get your application setup. You have "no Activity" nor "Context"? Are you building an Android application sir? – shkschneider Jan 19 '15 at 16:12
  • I'm building an Android library. A set of classes intended to be used in other Android applications. If a library project is not the correct way to do this, what is? To quote from the docs again, "If you have source code and resources that are common to multiple Android projects, you can move them to a library module so that it is easier to maintain across applications and versions" – sqshemet Jan 19 '15 at 16:15
  • and "If you are developing multiple related applications that use some of the same components, you move the redundant components out of their respective application module and create a single, reusable set of the same components in a library module." – sqshemet Jan 19 '15 at 16:15
  • Ok, so you're trying to build a library. That was unclear to me. – shkschneider Jan 19 '15 at 16:17
  • 6
    Did you find a solution ? – An-droid Mar 29 '16 at 15:05

1 Answers1

2

Without an Activity, it doesn't seem possible to use the R class. If you have a test application within your library, the test application will be able to access R, but not from the lib itself.

Still, you can access the resources by name. For instance, I have a class like this inside my library,

public class MyContext extends ContextWrapper {
  public MyContext(Context base) {
    super(base);
  }

  public int getResourceId(String resourceName) {
    try{
        // I only access resources inside the "raw" folder
        int resId = getResources().getIdentifier(resourceName, "raw", getPackageName());
        return resId;
    } catch(Exception e){
        Log.e("MyContext","getResourceId: " + resourceName);
        e.printStackTrace();
    }
    return 0;
  }
}

(See https://stackoverflow.com/a/24972256/1765629 for more information about ContextWrappers)

And the constructor of an object in the library takes that context wrapper,

public class MyLibClass {
  public MyLibClass(MyContext context) {
    int resId = context.getResourceId("a_file_inside_my_lib_res");
  }
}

Then, from the app that uses the 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, they all live inside the library project.

I hope it helps.

Community
  • 1
  • 1
endavid
  • 1,781
  • 17
  • 42