Android 4 : no more static R fields for lib projects

724 views
Skip to first unread message

Pierre-Yves Ricau

unread,
Oct 20, 2011, 3:12:13 AM10/20/11
to robo...@googlegroups.com
Hey guys,

The ACRA dev shared this information with me (for AndroidAnnotations), and I realized this will also impact RoboGuice:

In Android 4, in a library project, the fields of the R inner classes won't be final anymore.

So referencing these fields in annotations won't work anymore, because annotations only accept final values.

For example, this :
@InjectView(R.id.thumbnail)        ImageView thumbnail; 

Won't work if thumbnail comes from a library project (compile time error).

Here is a more precise description of the issue, and a potential solution.


Manfred Moser

unread,
Oct 20, 2011, 11:54:22 PM10/20/11
to robo...@googlegroups.com
Thanks a lot for sharing that. Definitely a bummer. At least it will still be fine for non library project. 

As a solution/workaround maybe we can come up with a second value for the InjectView annotation that represents the tag and we can use that to find the view 

from the main view of an activity. Of course that would mean that the library author has to make sure that the tag is unique for the library which could be achieved e.g. with a package name.

I have no idea what the performance implications of that could be though.. 

manfred

--
You received this message because you are subscribed to the Google Groups "roboguice" group.
To post to this group, send email to robo...@googlegroups.com.
To unsubscribe from this group, send email to roboguice+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/roboguice?hl=en.

Michael Burton

unread,
Oct 21, 2011, 7:03:59 AM10/21/11
to robo...@googlegroups.com
I like that idea, I'll give it a try in an upcoming snapshot.  I've never quite understood why Android has IDsTag, and also Tags.  The first two have associated findByXXX() methods, but the last one doesn't.  Seems like these could have been all joined into one id type, or at least the last two could have been combined if one is concerned about supporting XML id formats.

Jake Wharton

unread,
Nov 10, 2011, 4:17:22 AM11/10/11
to robo...@googlegroups.com
You could also provide an optional String-based lookup coupled with Resources#getIdentifier to perform the constant resolution. Something like @InjectView("main") where the fact that it's a layout is implied.

Unfortunately you lose compile-time verification of these resources with this method. I'm not sure why Google didn't just opt for one of the tricks to prevent finals from being inlined instead...

Michael Burton

unread,
Nov 10, 2011, 8:32:07 PM11/10/11
to robo...@googlegroups.com
That's a good alternative to tags.  I'll have to think about whether tags or getIdentifier seems better, or whether it makes sense to do both.

Neat tricks regarding final.  That would have been convenient.

On Nov 10, 2011, at 1:17 AM, Jake Wharton wrote:

You could also provide an optional String-based lookup coupled with Resources#getIdentifier to perform the constant resolution. Something like @InjectView("main") where the fact that it's a layout is implied.

Unfortunately you lose compile-time verification of these resources with this method. I'm not sure why Google didn't just opt for one of the tricks to prevent finals from being inlined instead...

--
You received this message because you are subscribed to the Google Groups "roboguice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/roboguice/-/6F4Xo32Op1cJ.

Saurav Shah

unread,
Jun 5, 2012, 10:03:06 AM6/5/12
to robo...@googlegroups.com
Is there a good solution to this? I don't want to use tags as they would require me to change my layout files as well.
To unsubscribe from this group, send email to roboguice+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages