johncarl81 / parceler

:package: Android Parcelables made easy through code generation.
http://parceler.org
Apache License 2.0
3.56k stars 273 forks source link

Problem with Android Studio 3.0 Beta 4 #313

Open carmas123 opened 7 years ago

carmas123 commented 7 years ago

I've a big probject and use Parceler with success, thank you. I've a problem when I try to make project in debug: If I edit my code and run debug when I use Parceler I got an exception with Class not found. If I recompile this probject the problem has gone and all work fine. Can anyone help me please (to recompile the project every time it take 2min :( )

This is the exception:

Caused by: org.parceler.ParcelerRuntimeException: Unable to find generated Parcelable class for com.example.data.model.MyClass, verify that your class is configured properly and that the Parcelable class com.example.data.model.MyClass$$Parcelable is generated by Parceler.

johncarl81 commented 7 years ago

Can you share MyClass?

carmas123 commented 7 years ago

My Class is a simple class model used with DBFlow:

@Parcel
@Table(database = DBLocal::class)
class MyClass : AbstractModel() {
    @Column(length = 3)
    @PrimaryKey

    var codice: String = ""

    @Column(length = 30)
    var descrizione: String = ""

    @Column
    var percentuale: Float = 0F

    @Column
    var noImponibile: Int = 0
}

The exception was raised into:

        public ParcelableFactory findClass(Class clazz){
            try {
                Class parcelWrapperClass = Class.forName(buildParcelableImplName(clazz));  <--HERE
                return new ParcelableFactoryReflectionProxy(clazz, parcelWrapperClass);
            } catch (ClassNotFoundException e) {
                return null;
            }
        }

Class.forName("com.example.MyClass$$Parcelable") <-- this cause the exception

I suppose that the problem is Gradle and Kapt, I use Kotlin and my gradle config is this: implementation "org.parceler:parceler-api:1.1.9" kapt "org.parceler:parceler:1.1.9"

carmas123 commented 7 years ago

I found the problem:

The problem appaer when mixing into gradle "annotationProcessor" and "kapt" if any library need annotationProcessor and use kotlin to solve the problem is need to add:

android {
...
  defaultConfig {
   ...
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath false
            }
        }
  }
}

this solve the problem.

carmas123 commented 7 years ago

Sorry...but again...my solution does not work

johncarl81 commented 7 years ago

using kapt doesn't work?

carmas123 commented 7 years ago

no

johncarl81 commented 7 years ago

Can you provide a simple example project that I can use to diagnose?

carmas123 commented 7 years ago

I'm sorry but this occurs on my big project.

carmas123 commented 7 years ago

I think that the problem is like this: https://github.com/realm/realm-java/issues/4087

carmas123 commented 7 years ago

I also see that this problem does not appear when I resync gradle before run debug

johncarl81 commented 7 years ago

Are you using proguard?

MilkBiscuit commented 6 years ago

Same problem with carmas123

MilkBiscuit commented 6 years ago

CurrentWeatherResponse.kt

package com.cheng.weatherdemo.models

import com.google.gson.annotations.SerializedName
import org.parceler.Parcel

@Parcel
class CurrentWeatherResponse {
    val id: Long = 0
    val weather: List<WeatherCondition>? = null
    val main: MainData? = null

    @SerializedName("name")
    val cityName: String? = null
    @SerializedName("dt")
    val time: Long = 0
}

Both WeatherCondition.kt and MainData.kt both extend java.io.Serializable

And CurrentWeatherFragment.kt

...
class CurrentWeatherFragment : Fragment() {
    private var currentWeather: CurrentWeatherResponse? = null

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)

//        outState!!.putParcelable(KEY_CURRENT_WEATHER, Parcels.wrap<CurrentWeatherResponse>(currentWeather))
        outState!!.putParcelable(KEY_CURRENT_WEATHER, Parcels.wrap(currentWeather))
    }

    ...
}
10-30 04:37:40.223 2027-2027/com.cheng.weatherdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cheng.weatherdemo, PID: 2027
org.parceler.ParcelerRuntimeException: Unable to find generated Parcelable class for com.cheng.weatherdemo.models.CurrentWeatherResponse, verify that your class is configured properly and that the Parcelable class com.cheng.weatherdemo.models.CurrentWeatherResponse$$Parcelable is generated by Parceler.
 at org.parceler.Parcels$ParcelCodeRepository.get(Parcels.java:153)
 at org.parceler.Parcels.wrap(Parcels.java:72)
 at org.parceler.Parcels.wrap(Parcels.java:56)
 at com.cheng.weatherdemo.ui.TodayWeatherFragment.onSaveInstanceState(TodayWeatherFragment.kt:67)
 at android.support.v4.app.Fragment.performSaveInstanceState(Fragment.java:2526)
 at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:2862)
 at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:2923)
 at android.support.v4.app.FragmentController.saveAllState(FragmentController.java:125)
 at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:528)
 at android.support.v7.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:509)
 at com.cheng.weatherdemo.ui.MainActivity.onSaveInstanceState(MainActivity.kt:43)
 at android.app.Activity.performSaveInstanceState(Activity.java:1414)
 at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1300)
 at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:4541)
 at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4492)
 at android.app.ActivityThread.-wrap19(ActivityThread.java)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:6119)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
MilkBiscuit commented 6 years ago

Note: If I change CurrentWeatherResponse.kt to CurrentWeatherResponse.java, everything works fine.

johncarl81 commented 6 years ago

If anyone can put together a simple demonstration project and share it on github it would help me diagnose the issue.

MilkBiscuit commented 6 years ago

Use mine:

https://github.com/MilkBiscuit/WeatherDemo

You can see the problem on development branch when you rotate in MainActivity, it will crash in CurrentWeatherFragment.onSaveInstanceState().

master branch works fine because ForecastResponse and CurrentWeatherResponse class was written in java.

johncarl81 commented 6 years ago

Fantastic @MilkBiscuit, I'll take a look.

jjhesk commented 6 years ago

Error:(33, 20) error: package org.parceler does not exist I have found the same issue as i upgraded to buildtool 3.0.0 with API 26.1.0 support

tomtharakan commented 6 years ago

org.parceler.ParcelerRuntimeException: Unable to find generated Parcelable class for , verify that your class is configured properly and that the Parcelable class $$Parcelable is generated by Parceler. at org.parceler.Parcels$ParcelCodeRepository.get(Parcels.java:153)

LorienOlive commented 6 years ago

I'm having the same issue(when I run debug). I tried to fix it by changing annotationProcessor to kapt in my build.gradle file, but when I do that I get a series of errors in the gradle console that suggest that none of the annotations are now being read (i.e. that parceler is not working at all). Has anyone else had that issue when they'd switched from annotationProcessor to kapt?

johncarl81 commented 6 years ago

@MilkBiscuit, I was able to get Parceler working by chancing the build scope to kapt:

dependencies {
    ...
    annotationProcessor 'org.projectlombok:lombok:1.14.8'
    kapt 'org.parceler:parceler:1.1.9'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

The result is still an error, but you'll need to resolve these in kotlin:

> Task :app:compileDebugJavaWithJavac
app: Original kapt is deprecated. Please add "apply plugin: 'kotlin-kapt'" to your build.gradle.
Processor path was modified by kapt. Previous value = configuration ':app:debugAnnotationProcessorClasspath'
Destination for generated sources was modified by kapt. Previous value = /mnt/hd1/home/john/dev/WeatherDemo/app/build/generated/source/apt/debug
error: Parceler: No @ParcelConstructor annotated constructor and no default empty bean constructor found.
error: Parceler: Unable to find read/write generator for type com.cheng.weatherdemo.models.ForecastResponse.City for com.cheng.weatherdemo.models.ForecastResponse.city
error: Parceler: Unable to find read/write generator for type java.util.List<com.cheng.weatherdemo.models.ThreeHourForecast> for com.cheng.weatherdemo.models.ForecastResponse.list
3 errors
ivotai commented 6 years ago
@Parcel
class Person @ParcelConstructor constructor(val name: String)

Add @ParcelConstructor constructor may solve the problem.

enginebai commented 6 years ago

We fix this by replacing annotationProcessor 'org.parceler:parceler:1.1.11' with kapt 'org.parceler:parceler:1.1.11'.

johnjohndoe commented 5 years ago

I added kaptTest "org.parceler:parceler:$parcelerVersion" to the build.gradle file which resolved the issue for me.