android import ressources from library project

13,642

Solution 1

So it looks like

int id = ressources.getIdentifier(title, "string", "com.package");

is returning 0, meaning it can't find the specified resource. The subsequent call to ressources.getIdentifer() causes the exception, since 0 is not a valid resource id.

Here are some debug/alternative ideas:


  • You've probably already done this a dozen times, but it doesn't hurt mentioning: first recheck spelling of everything:

    • package spelling is correct (both in library project and in client project),
    • resource string is correct (library project and client project),
    • is library spelling correct in the uses-library element the AndroidManifest.xml,
    • etc.

  • Can you access any resources in that library or is the problem specific to that resource (title) or is it specific to that type of resource(strings)? Can you access the resources of another library?

  • Are you accessing the library as a jar file? You can jar the code, but you can't access resources from a jar.

Android - Is it possible to create a custom library to use across several applications?


  • Did you try the alternate name format:

Code

String fullyQualifiedResourceName = "com.package:string/sample";
int id = ressources.getIdentifier(title, null, null);
if (id == 0) {
    Log.e(TAG, "Lookup id for resource '"+fullyQualifiedResourceName+"' failed";
    // graceful error handling code here
}

  • You could try using reflection:

Code

final String resourceName= "sample";
final int id;
try {
    final Class resourceType = com.package.R.string.class;
    final Field field = resourceType.getField(title);
    id = field.getInt(null);
} catch (final Exception e) {
    Log.e(TAG, "Lookup id for resource '"+resourceName+"' failed";
    // graceful error handling code here
}

Android, getting resource ID from string?

http://daniel-codes.blogspot.com/2009/12/dynamically-retrieving-resources-in.html

Solution 2

In your project code, import the R class:

import com.application.libraryproject.R

Then you can reference any string or other xml-defined resource like this:

String mystring = getString(R.string.app_name);

or similar.

Solution 3

I had a similar issue and I solved it with the getPackageName() method within the Project Library.

In your case, it should be:

String title = "sample";
int id = ressources.getIdentifier(title, "string", getPackageName());
Share:
13,642
clamp
Author by

clamp

hello

Updated on June 23, 2022

Comments

  • clamp
    clamp almost 2 years

    is it possible to use ressources like strings that are defined in library projects in the application-projects? if so, how? because i cant seem to resolve the strings i would like to resolve like this:

    String title = "sample";
    int id = ressources.getIdentifier(title, "string", "com.package");
    

    gives me this exception

    WARN/ResourceType(278): No package identifier when getting value for resource number 0x00000000 WARN/System.err(278): android.content.res.Resources$NotFoundException: String resource ID #0x0

    the string ("sample") i am looking for is definitely in this package provided in the strings.xml of the library project. i can even see it in the R.java

  • clamp
    clamp over 13 years
    ok, but what if i want to get the string with this function getResources().getString( int id )
  • Nick Campion
    Nick Campion over 13 years
    Its exactly the same as using the standard R class, getResources().getSTring(R.string.somestringyouwanttouse)
  • clamp
    clamp over 13 years
    thanks, actually my previous comment was wrong. i am trying to find the id by its name. i have now corrected my original post.
  • clamp
    clamp over 13 years
    thanks, actually my question was wrong. my problem is infact to get the id of the ressource, which i try to find using getIdentifier and it's name.
  • clamp
    clamp over 13 years
    thanks for your detailed answer! the library i am using is not a jar, it is merely another project in eclipse which i have referenced using the android project properties. using the version with the fullyQualifiedResourceName gives the same error.
  • clamp
    clamp over 13 years
    the version with reflection did infact work but i still would like to understand why the 'official' one didnt.
  • Bert F
    Bert F over 13 years
    @clamp - I tried tracing through the source for clues, but the code goes into native code pretty quick and I have more trouble following beyond there. android.git.kernel.org/?p=platform/frameworks/… ; android.git.kernel.org/?p=platform/frameworks/… ; android.git.kernel.org/?p=platform/frameworks/…
  • Pierre-Yves Ricau
    Pierre-Yves Ricau about 12 years
    Thanks for that answer, it helped!