Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Alternative method for providing R ids with Android 4 #20

Description

@pyricau

In Android 4, the fields of the R inner classes won't be final anymore for library projects (and only for library projects).

This means that those ids cannot be used for switches any more (and we don't really care), but also cannot be use into annotations. That's a problem for us, because the compiler won't allow this any more :

@ViewById(R.id.myIdFromMyLibraryProject)

So I think we'll have to provide an alternative way to specify ids, using strings and complete qualified names :

@ViewById(alt="com.mylibrary.R.id.myIdFromMyLibraryProject")

This is not perfect (not typesafe any more). Ids may become invalid and you won't notice. We can help with our compile time framework, because when can check that the "com.mylibrary.R.id.myIdFromMyLibraryProject" field exists, and issue a compile error otherwise. Runtime frameworks won't be that lucky (RoboGuice, ACRA...).

The Android team introduced this change to improve the build perfs when using library projects. More on this subject :

http://tools.android.com/tips/non-constant-fields
https://plus.google.com/u/0/109385828142935151413/posts
http://code.google.com/p/acra/issues/detail?id=85

Activity

  1. pyricau commented on Feb 2, 2012

    @pyricau
    ContributorAuthor

    Well in fact we should be able to find the R class, so we could probably do this :

    @ViewById(alt="myIdFromMyLibraryProject")

    @EActivity(alt="myLayout")

  2. naixx commented on Jun 15, 2012

    @naixx
    Contributor

    Should be very useful. Vote +

  3. pyricau commented on Jun 21, 2012

    @pyricau
    ContributorAuthor

    Quoting myself in #78 :

    We could generate an R_ class that would be a copy of the R class (with negative ids) but with final fields, and at compile time we would reference the right R field in the generated code. This would mean we wouldn't need to do any change to @EActivity, and when referencing library project ids you would do @EActivity(R_.layout.someLayout).

    I don't know why I wanted to use negative ids. There's no point doing that, we can use the real ids anyway.

  4. pyricau commented on Jun 21, 2012

    @pyricau
    ContributorAuthor

    Hum. We in fact cannot do that. I just tried, spent some time implementing it and.. the problem is that when doing a full build, the R_ class is removed, and therefore the annotation value cannot be found, leading to a java.lang.reflect.InvocationTargetException.

    We'll have to fallback to String attributes.

  5. pyricau commented on Jun 21, 2012

    @pyricau
    ContributorAuthor

    I'm on it... this is going to be a mega commit :)

  6. pyricau commented on Jun 25, 2012

    @pyricau
    ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions