rubdottocom
rubdottocom

Reputation: 8298

How to access to android library own resources

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.

Upvotes: 1

Views: 611

Answers (1)

lase
lase

Reputation: 2634

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'

Upvotes: 2

Related Questions