Using Activities from Library projects
Asked Answered
S

5

16

I have a Library project set up and a project which depends on this library project - Everything compiles fine and I've had the Dependent project running fine however;

I'm have an intermittent problem however while using an Activity from the Library project.

My Library project can be "unchecked" as a library project and the "picker" Activity can be run in isolation. Running the Library project in it's own right works fine with no problems.

When I use the Library project (and the "picker" Activity) from the dependent project it gives me either NullPointerExceptions (or historically DexOp) errors saying that the "picker" Activity can't find it's resources.

Can anybody tell me if they have experienced this before?

EDIT

Manually copying the layout.xml used by the Library project activity to the dependent project makes the project open the "picker" Activity correctly. Is this required?! Surely not? This further makes me wonder if there is a problem with merging/referencing the integer id's from R.java in the Library project.

EDIT

I'm not sure if this is of any consequence, but one of my Library projects is a wrapper around a set of custom views used in different front end projects.

These custom views each have an Activity so that they can be tested in isolation.

One of these view's has custom attributes, which while running in isolation work very well. However, it doesn't seem like the dependent project correctly pull's in the attributes and keep's them in their original namespace, causing the imported layout's to throw compilation errors:

[2012-05-16 12:07:28 - Project] D:\opt\workspace\CustomGlowList\res\layout\main.xml:14: error: No resource identifier found for attribute 'listId' in package 'com.company.library.glowlist'
[2012-05-16 12:07:28 - Project] D:\opt\workspace\CustomGlowList\res\layout\main.xml:14: error: No resource identifier found for attribute 'type' in package 'com.company.library.glowlist

EDIT

DexOp errors were solved by removing a duplicate file name which was present in the Dependent project. Not sure if the file name was the problem or the contents of the file.

EDIT

I've been unable to find a solution to this problem as of yet (although @yorkw's answer certainly helps with the xml attributes! Thanks).

I have managed to make this problem intermittent however:

I have gone through each of the library projects and ensured they all had their own namespace (ie, com.company.library.component1, com.company.library.component2 etc). After a couple of clean's (each project ordered with the correct precedence) this problem resolves itself, however does eventually come back when switching around library/not a library to test components.

Strow answered 1/5, 2012 at 13:33 Comment(5)
What is the problem you are talking about exactly? It is not clearly described in the question, I don't think others can help much if you can't put more details. you should post at least the full error message shown in Eclipse for the "problem" you referred in this question.Lilylivered
@Lilylivered The error is the usual NullPointerException caused by accessing a view which has not been found because the specified layout resource wasn't found (in a findViewById(R.id.whatever)). Since for the moment the problem isn't in evidence I can't give you copy paste, I will add them next time it crops up though.Strow
I can't give you more shot with current details you posted, in the meantime, take a deep read here. Latest version of SDK is quite robust now and should take care of library project (compiling source, merge resource and etc.) properly when clean/build the dependent project. You mentioned you have multiple library projects, do you by any chance have two level library project dependency, i.e. library project 1 is depend on library project 2?Lilylivered
Yes, I'd give a complete example but I have 4 "independent" libraries which can run on their own in isolation. A dependent library which runs 2 of the independent libraries and a dependent library which runs a dependent library and 2 of the other independent libraries.Strow
Check out the red important change in this link, it explains some considerations about two level library project dependency. For instance, if you have libA (com.example.liba) depend on libB (com.example.libb) and explicitly using import com.example.libb.R; in some class in libA, it doesn't work. As a good practice, it is not recommend to explicit using import com.example.libb.R in your code, as it is not necessary and all resources is always included in main project's own R file when clean/build main project.Lilylivered
S
1

The problem was being caused by the R files from each of the Library projects not correctly being built and referenced. The resource files are built and referenced directly from the calling project.

Each of the Libraries need to have unique package as defined in the AndroidManifest.xml. This leads to each of it's resources being compiled in that unique namespace within the calling project alongside the Library jar which contains the class files.

The <code>R</code> files located in situation

The problem becomes intermittent when switching between a library and a runnable project because a clean and build needs to take place to regenerate these files as it isn't done automagically when unclicking the Use as library checkbox, where as the jar (and java classes) don't require as much coaxing for them to be correctly referenced as the library projects references them when acting as a Library.

This can lead to intermittent and also varying errors including missing references, DexOp and NullPointerExceptions depending on to what degree the R.java files have been mangled or partially built and what conflicts are taking places between packages.

Strow answered 29/5, 2012 at 8:46 Comment(0)
L
14

Cause:

Issue 9656: Library projects don't support custom XML attributes for custom classes

Solution:

Upgrade to latest SDK & ADT version (fixed was released since r17) and use http://schemas.android.com/apk/res-auto as custom attributes' namespace URI, see Revisions for ADT 17.0.0:

Added support for custom views with custom attributes in libraries. Layouts using custom attributes must use the namespace URI http://schemas.android.com/apk/res-auto instead of the URI that includes the app package name. This URI is replaced with the app specific one at build time.

Related Topic:

Help with a custom View attributes inside a Android Library Project

Lilylivered answered 16/5, 2012 at 22:10 Comment(5)
this is weird. i've done such things for a long time and never had to set this namespace . they say it's a "must" , but i never used this namespace and never had a problem with it . on the custom preferences i think i had problems.Fawnia
thanks @yorkw, this certainly sorted the problem with the custom attributes.Strow
@Strow if problem is sorted, then you should accept this answer.Michaelis
@djaqeel This doesn't answer the question - it answers one of the symptoms which wasn't cause of the problem.Strow
I've awarded the bounty to this answer as I would have needed to change the custom attribute package anyway. However, it does not solve the problem completely.Strow
F
2

not sure how you use the android library , but i've done this thing a lot of times without any problems , so please have a reading of the next things regarding using android libraries:

  1. in order for an android project to use an android library project , you can either edit the "project.properties" and add the path to the library project there , or , much easier, right click the project (that uses the library project) , choose "android" category , and choose there "add" and choose the library project.

  2. in order to make the android project to always open the android library project , also choose "java build path"->"projects" and choose the library project.

  3. on the "project.properties" , try to set the target to the same target for both projects , and to the newest one that you have (latest now is 15) ,even if you can't test the app on this android version .

  4. source files and all resources files (in the res folder) are ok to be used on the library projects.

  5. assets folders cannot be used inside the library project . only on the android project that actually runs , the assets folder can be used . same thing goes to the proguard file .

  6. the manifest of the library projects almost doesn't need to have almost anything . see this post for more information: https://mcmap.net/q/131001/-for-android-library-projects-is-lt-uses-sdk-gt-meaningful-in-manifest . if there are activities on the library project that you wish to be able to access via the project that use it , don't forget to include them in the manifest of the android project (no need to write them in the manifest of the android library) .

  7. remember to run lint before you export the app . it gives you plenty of warnings , but the most important warning is when you use too-new functions that cannot run on the range of APIs that you target . this is important for libraries since it's easy to get lost if there are multiple sources .

  8. if you wish to extend activities on the android library , you need some kind of a method to go between them . the only way i know of is to extend the application and let it be the manager of how to create an intent for each of the activities . i think you can do other tricks , but they are all probably similar to this one.

  9. if you have the same resource (any resource on the "res" folder) on both the android library and the android project, the one on the android project will replace the other one . this has advantages and disadvantages . remember that you can't have different file extensions for the same resource .

Fawnia answered 16/5, 2012 at 20:25 Comment(3)
7. library files can be inside the library project , but don't forget to link to them on both projects , even if it doesn't give you any compilation warnings. <- This is wrong, the folder libs is scanned and compiled into the Library project as of adt 17.Strow
Can you explain #8 a bit more? What do you mean a method in between? Where would this method reside ? i.e. - who owns it?Boeotian
@Boeotian I meant a way to switch between the new activities. The way I suggested to do so is by extending the Application class, which will be the one that gives you the correct activity to go to. Of course it depends on your needs. You might be able to just put a function on each of the activities that need to go to the new activities, that will be overridden if needed.Fawnia
S
1

The problem was being caused by the R files from each of the Library projects not correctly being built and referenced. The resource files are built and referenced directly from the calling project.

Each of the Libraries need to have unique package as defined in the AndroidManifest.xml. This leads to each of it's resources being compiled in that unique namespace within the calling project alongside the Library jar which contains the class files.

The <code>R</code> files located in situation

The problem becomes intermittent when switching between a library and a runnable project because a clean and build needs to take place to regenerate these files as it isn't done automagically when unclicking the Use as library checkbox, where as the jar (and java classes) don't require as much coaxing for them to be correctly referenced as the library projects references them when acting as a Library.

This can lead to intermittent and also varying errors including missing references, DexOp and NullPointerExceptions depending on to what degree the R.java files have been mangled or partially built and what conflicts are taking places between packages.

Strow answered 29/5, 2012 at 8:46 Comment(0)
P
1

I have same issue like, I have one library project and it has some activity. When I was calling library project's Activity from my Main project, layout files resources were not getting loaded properly for the libray project's activity and giving "nullpointer exception" or "class could not be loaded", etc.

Solution: I have noticed that same layout file name is getting used in my Library project and Main project. I have just renamed those activity and it is solved.

May be this will help you.

Platy answered 22/11, 2012 at 12:50 Comment(0)
A
0

I had similar issue when referencing Library project inside the Android App project. I deleted the R.java file from Library project so it generated again and App picked up the generated file. You should see the generated R.java file under your app project->gen->library_namespace_R.java.

Armed answered 23/9, 2013 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.