1

I'm developing an Android Library that has strings and layout resources.

How can I get them without creating an Activity class?

I don't want to access to "main project" resources, I read a lot of questions about that :P, I'm in a "plain class" inside the module and I need the module resources.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
rubdottocom
  • 8,110
  • 10
  • 39
  • 59
  • http://stackoverflow.com/questions/9421244/accessing-application-resources-from-the-library-project – Marco Acierno Apr 18 '14 at 10:25
  • @MarcoAcierno Well... that's exactly that I DON'T need to do, this question talks about how library can access application resources, I need that library access their own resources. Also I don't know how to get Context, I don't want the application context, I need the library context – rubdottocom Apr 18 '14 at 10:29

1 Answers1

2

I was able to accomplish this by addressing the resources as you normally would, from inside of my library, to create a reusable custom dialog:

customView = inflater.inflate(R.layout.fragment_dialog, container, false);

The catch here seems to be how you build/import this library. I think that by default, marking the project as a library packages it up as a jar file. However, I was able to get at these resources by packaging as Android Application Resource (aar) file. If you compare the structures of these files, you'll see that an aar will have everything under your res/ direcotry, rather than just the class files (as in a jar).

I'm using maven, so I used built my package like this:

<groupId>com.mycompany</groupId>
<artifactId>my-library</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>aar</packaging>
<name>My Library</name>

Then, when importing my library into my project, I made sure to specify the type:

<dependency>
    <groupId>com.mycompany</groupId>
    <artifactId>my-library</artifactId>
    <version>1.0-SNAPSHOT</version>
    <type>aar</type>
</dependency>

This is possible with Gradle as well - you can specify aar as your archive type if necessary:

compile 'my.company:my-library:1.0-SNAPSHOT@aar'
lase
  • 2,508
  • 2
  • 24
  • 35