Resources in Android Library - android

I have problems with accessing resources in my android library. I have created library project with some resources (com.library) and then I imported module into application project (com.app). So I have Android Studio project with library and application.
When I want to access some library resource (com.library.R.string.label) I get error during compilation
package com.library.R does not exist
When I want to run some library method from application which contains/uses R.string.label I get
java.lang.NoClassDefFoundError: com.library.R$string
I added library to application gradle file using
compile project(':Library')
and from IDE perspective looks everything fine and R.java is created with references to resources. I want same usage as I'm using for example android.R.string.cancel or similar libraries in my application project.Where I'm doing mistake? Thank you for help.
PS: In the future I want to have my library project as aar package.

As soon as you add a library to your project all resources will be "copied" to the R-file of your app. So if you want to access a string from your library you don't do something like getString(com.library.R.string.some_string) you simply call getString(R.string.some_string) instead.
In terms of the library method: Would you mind sharing some code with us? Currently I can't imagine what's going wrong.

Problem was in wrong package name in gradle configuration files (after renaming).

Related

Android Library Project Export

I am developing first time Android Library Project that have external dependences of different libraries like retrofit and ormlite-android. The problem i am facing right now is when i use my Android Library Project .aar file inside other project the class not found exception occur when run the code. Please can any one guide me how to fix such issues. Do i have to add external libraries reference inside my dependent app ?

How to get reference of application project into library project?

I am going to make application with library project. My application project referred a library project.
When we build the dependent application project, library projects are compiled and merged with the application project, so application project has all the resources which library have.
But my requirement is slightly reverse from above. I want application project resources reference into library project. I want to access R.java of application project into reference library project.
Is there any way to get reference of dependent project into library project?
Thanks in advanced.
No, there is no way as far as I know. And it is also not the idea of "Lib" projects that they depend on a App Project. It is only made for a vice versa dependency.

How to refer to a resource not in the Library Project, but only in the dependent Application Project?

I'm turning my one app into several similar apps and using a base library project. I want to load a video that will be in res/raw/welcome.m4a in the dependent applications.
This is how is was being done before using the library project:
vv.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.welcome));
This produces an error in my library project, because res/raw/welcome.m4v doesn't exist, it will only be in the App Project res/ folder, and will be different for each app.
My current workaround is to create a dummy res/raw/welcome.m4v in the base library project so it stops complaining.
So my question is how do I have code in the Library Project that refers to resources that don't exist in the Library Project?
If it is only happening infrequently, you can try this:
vv.setVideoURI(Uri.parse("android.resource://"+getPackageName()+"/"+getResources().getIdentifier("welcome", "raw", getPackageName())));
Not as performant as using the id, but it should work.

How to test an android library?

I am writing an android library project and trying to test it. I found testing a library project
more difficult than it has to be using my current method.
Right now, I am exporting the library project into jar file. Then I put it into the libs folder of the test project and the test target project. Then I add it to build path of both projects and run the test.
Every time I make a change to the library I have to do this again. Is there easier way to do this?
There is some reference to this in the google docs. Check out http://developer.android.com/tools/projects/index.html#testing
Basically, the easiest way is to write an application that uses the library project and then add unittests to that application.
When creating the tests via eclipse, you can choose to test modules from within your library project. This way, changes to the library project are automatically applied.

Issues with referencing Android library projects

I'm trying to use an ActionBar library project in my app but I keep getting errors when trying to add the library reference. I do it as described on the Android Developers website (here) but it only copies the classes across to a folder called "actionbar_src". The error then comes when it tries to find the library's resources within the classes, which it didn't copy across. What am I doing wrong?
Here's a screenshot:
Under 'Java Build Path -> Projects' did you also add the project there? I find I have to do both for the project to compile and run.

Categories

Resources