Git Product home page Git Product logo

Comments (28)

ruqqq avatar ruqqq commented on August 10, 2024

I fixed it by changing

compile 'org.parceler:parceler-api:0.2.7'
provided 'org.parceler:parceler:0.2.7'

to

compile 'org.parceler:parceler-api:0.2.7'
apt 'org.parceler:parceler:0.2.7'

in build.gradle.

Is this the correct behaviour?

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

What version of the android-gradle plugin are you using?

from parceler.

ruqqq avatar ruqqq commented on August 10, 2024

In my top level build.gradle, I have

classpath 'com.android.tools.build:gradle:0.9.+'

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

Hmm, provided should work. There was a bug with a previous version of the gradle android plugin that didn't work well with annotation processors, but versions 0.8.+ seem to work. The gradle example we have in the examples folder uses classpath 'com.android.tools.build:gradle:0.8.+'. Can you try to build that one? Also, can you check your annotation processor output directory for org.parceler.Parceler$$Parcels which should contain a map that associates your annotated class with a factory to build the ParcelWrapper.

Other than that, It's up to you if you want to use apt instead of provided.

from parceler.

ruqqq avatar ruqqq commented on August 10, 2024

I haven't got time to build your example. But in my current project setup, using provided instead of apt would not generate any classes by Parceler.

It's working now so I guess I wouldn't touch it unless some other issue pops out. Thanks. Great job on the library. :)

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

Fair enough, as long as you have something working. Thanks for the kudos. Close the issue?

from parceler.

czellweg avatar czellweg commented on August 10, 2024

I had a slightly different problem but the exact same stacktrace. My setup involves Eclipse, m2e and ADT.

Setup:

  • Abc-App: Android app, has dependency on Abc-library
  • Abc-library: standard Java Maven project, contains the transfer object (ParcelerTO) which is annotated with @parcel

Code snippet of the failing call to Parceler.wrap() is attached below:

Message response = Message.obtain(null, 2, 0, 0);
Bundle returnData = new Bundle();

ArrayList<Parcelable> list = new ArrayList<Parcelable>();
for (ParcelerTO to : data) {
  list.add(Parcels.wrap(to));
}
returnData.putParcelableArrayList("data", list);
response.setData(returnData);

The call to Parceler.wrap() then caused the same stacktrace as the one described by the OP.

Solution:
You have 2 options to go about it:

  1. add the generated source code in target/generated-sources/annotations/** as a source folder in Eclipse. This makes sure that the required classes are on the classpath and can be found at runtime. Make sure to have m2e's workspace resolution enabled.
  2. make sure to have a jar dependency to Abc-library, i.e. don't use m2e's workspace resolution feature. You can either disable workspace resolution or close the project.

I hope this helps anyone in the future.

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

Thanks for the input @czellweg. Glad someone found a solution using Eclipse.

By the way, we recently added the ability to wrap/unwrap List<@Parcel>. So your code above could be reworked to the following:

Message response = Message.obtain(null, 2, 0, 0);
Bundle returnData = new Bundle();
returnData.putParcelable("data", Parcels.wrap(data));
response.setData(returnData);

Assuming data is a List of @Parcel annotated classes.

from parceler.

czellweg avatar czellweg commented on August 10, 2024

Ah nice, wasn't aware of that. Thanks for that input.
And great job on the whole project, it's really easy and intuitive to use! Keep up the good work, I will have to checkout Transfuse soon too ;)

from parceler.

trinitron01 avatar trinitron01 commented on August 10, 2024

Got a very similar situation as @czellweg with slight differences :

We are using Android Studio and our App also contains objects annotated with @Parcel, both build.gradle files contain

  compile 'org.parceler:parceler-api:0.2.7'
  provided 'org.parceler:parceler:0.2.7'

During compilation I get an

 DexException Multiple dex files define Lorg/parceler/Parceler$$Parcels$1

When I remove line provided 'org.parceler:parceler:0.2.7' from our library project compilation completes successfully however Parcels for library classes are not generated and when our applciation starts I end up with

org.parceler.ParcelerRuntimeException: Unable to create ParcelableFactory for {CLASS FROM LIBRARY PROJECT}

I tried adding source dir in app build.gradle file but it generated duplicates of those library project classes adnotated with @Parcel

Any ideas ?

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

@trinitron01, I see. Library project would be a tricky case because Parceler relies on the Parceler$$Parcels class as a mapping of annotated classes to generated classes.

Could you try using the @ParcelClass(es) annotation in your application (not library) to call out the @Parcel classes you need? I wonder if there is a better solution than this though for library projects.

from parceler.

trinitron01 avatar trinitron01 commented on August 10, 2024

Using

@ParcelClass

with version 0.2.7 was throwing a

java.lang.NoClassDefFoundError: android/os/Parcel

but it worked with the 0.2.10-SNAPSHOT, just like you mentioned in

#34

Thanks and great job with the library

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

Thanks @trinitron01. I've release 0.2.10 this evening to maven central so you can depend on a non-SNAPSHOT.

from parceler.

trinitron01 avatar trinitron01 commented on August 10, 2024

Are you planning on finding some other solution, more convenient than using @ParcelClass in this case ?

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

What would you like to see?

from parceler.

trinitron01 avatar trinitron01 commented on August 10, 2024

I guess that instead of @ParcelClass solution it would be perfect to just use parceler normally in build.gradle both in library project as well as the main project

    compile 'org.parceler:parceler-api:0.2.7'
    provided 'org.parceler:parceler:0.2.7'

However now if I do that and I have classes annotated by @parcel in library project as well as in the main project I get this :

Error Code:
    2
Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lorg/parceler/Parceler$$Parcels$1;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
        at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
        at com.android.dx.command.dexer.Main.run(Main.java:230)
        at com.android.dx.command.dexer.Main.main(Main.java:199)
        at com.android.dx.command.Main.main(Main.java:103)

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

The only thing keeping this from working OOTB is the fact that Parceler generates a common mapping class (Parceler$$Parcels$1) in both the main apk and the library. If there is a way to ignore the Parceler$$Parcels$1 generated in the library (Im sure there is with Gradle) then Parceler should pick up the @Parcel generated in the library.

from parceler.

trinitron01 avatar trinitron01 commented on August 10, 2024

It seems that adding

 dexOptions {
        preDexLibraries = false
    }

in the application build.gradle file solves the problem without using @ParcelClass annotation for library classes. In our project both application and library build.gradle files contain

    compile 'org.parceler:parceler-api:0.2.10'
    provided 'org.parceler:parceler:0.2.10'

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

Thanks @trinitron01 . I may want to note that in the docs.

from parceler.

xMikeTx avatar xMikeTx commented on August 10, 2024

So @trinitron01 s, workaround worked for me until i used the new buildToolsVersion of lollipop.

buildToolsVersion '20.0.0’
Exception in thread "pool-1-thread-4" java.lang.IllegalArgumentException: already added: Lorg/parceler/Parceler$$Parcels$1;
-> but it works if you set the preDexLibraries = false

buildToolsVersion '21.0.2'
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lorg/parceler/Parceler$$Parcels$1;

Would be great if you could take a look at this.

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

What it looks like is your build is compiling things multiple times with Parceler running in each pass. Parceler is designed only to run once, preferably when you compile your entire codebase.

from parceler.

xMikeTx avatar xMikeTx commented on August 10, 2024

@johncarl81 That is correct, since I use the parceler in a library project of mine which is a gradle artifact of its own.

That is why I had to use the workaround of @trinitron01 in the first place. So what you are saying is, that you cannot use the parceler annotation in library projects and use the annotated classes in the main project.

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

@xMikeTx, there may be another workaround with a library project: You can reference the classes in your library project from the main project via the @ParcelClass annotation.

I could add a parameter to @Parcel to avoid adding it to the common mapping class, effectively not creating it.

I'd hope there is a way to exclude or avoid dexing specific classes in library projects. Any ideas on this?

from parceler.

xMikeTx avatar xMikeTx commented on August 10, 2024

I think this is tricky. I probably refactor my library project. Just to be on the safe side.
Nice library though. Good job.

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

I think I'd like to add this parameter nevertheless... something like this:

@Parcel(parcelsIndex = false)
class Example{}

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

@xMikeTx, I went ahead and added the parameter I mentioned in the previous post (parcelsIndex = false). If you add this to every one of your @Parcel annotated beans it will not generate a Parceler$$Parcels$1 in your library project. If you want to give this a try you can try out 0.2.14-SNAPSHOT and please let me know if this works for you.

from parceler.

xMikeTx avatar xMikeTx commented on August 10, 2024

Well, that was quick. I didn't expect such a fast execution :) I have already refactored my library, but tried out your piece of code, and it seems to work now.

I just tested it on a small example. Good job.

from parceler.

johncarl81 avatar johncarl81 commented on August 10, 2024

Great! Thanks for the idea.

from parceler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.