问题
I have created a custom property dedicated to holding properties of the view that require invalidate() call for one of my projects:
class InvalidatingProperty<T>(private var _value: T) {
operator fun getValue(thisRef: View, property: KProperty<*>): T {
return _value
}
operator fun setValue(thisRef: View, property: KProperty<*>, value: T) {
_value = value
thisRef.invalidate()
}
}
In another project, I wanted to reuse some of my previous work, so i copied the views and their dependencies, including the InvalidatingProperty, to a new project. In the new project though, using the InvalidatingProperty causes the preview render to fail with a following exception:
java.lang.NoSuchMethodError: kotlin.jvm.internal.MutablePropertyReference1Impl.<init>(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;I)V
at com.gradesimulator.views.SummaryView.<clinit>(SummaryView.kt)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:403)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:186)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:144)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:309)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:416)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:427)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:331)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1123)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1097)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:501)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:348)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:404)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:678)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$8(RenderTask.java:809)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
The source projcet (where the preview does work) and the target project, both have the same android version - 4.0.1, use the same version of kotlin and kotlin gradle plugin - 1.4.0
Also, the important fact is that on the emulator or the real device the views work completly fine
I've already tried the Invalidate Chaches/Restart, and tried to downgrade kotlin as i found some issues regarding the 1.4.0 version that caused similar exception.
EDIT: Upon further inspection, sometimes a different exception seems to occur:
java.lang.NoClassDefFoundError: Could not initialize class com.gradesimulator.views.SummaryView
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:403)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:186)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:144)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:309)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:416)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:427)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:331)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1123)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1097)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:501)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:348)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:404)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:678)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$8(RenderTask.java:809)
at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Compressed SummaryView Code:
@Suppress("MemberVisibilityCanBePrivate", "unused", "PropertyName")
class SummaryView : View {
companion object {
private const val TAG = "SummaryView"
//primary circle
const val DEFAULT_PRIMARY_CIRCLE_RADIUS_SCALE: Float = 1F
const val DEFAULT_PRIMARY_CIRCLE_DISPLACEMENT: Float = 0.3F
//secondary circle
const val DEFAULT_SECONDARY_CIRCLE_RADIUS_SCALE: Float = 1F
const val DEFAULT_ANIMATE_IN_SECONDARY_CIRCLE_DELAY: Long = 100
const val DEFAULT_SECONDARY_CIRCLE_DISPLACEMENT: Float = 0.2F
//both circles
const val DEFAULT_CIRCLES_SHADOW_RADIUS: Float = 0F
const val DEFAULT_CIRCLES_SHADOW_DISPLACEMENT: Float = 0F
const val DEFAULT_ANIMATE_IN_CIRCLES_ANIMATION_DURATION: Long = 600
//bigAverage
const val DEFAULT_BIG_AVERAGE_SHADOW_DISPLACEMENT_X: Float = 0F
const val DEFAULT_BIG_AVERAGE_SHADOW_DISPLACEMENT_Y: Float = 0F
const val DEFAULT_BIG_AVERAGE_SHADOW_RADIUS: Float = 0F
//smallAverage
const val DEFAULT_SMALL_AVERAGE_SHADOW_DISPLACEMENT_X: Float = 0F
const val DEFAULT_SMALL_AVERAGE_SHADOW_DISPLACEMENT_Y: Float = 0F
const val DEFAULT_SMALL_AVERAGE_SHADOW_RADIUS: Float = 0F
//average
const val DEFAULT_AVERAGE_MARGIN: Float = 0.4F
//progressArc
const val DEFAULT_ANIMATE_IN_PROGRESS_ARC_ANIMATION_DURATION: Long = 1200
const val DEFAULT_PROGRESS_ARC_MARGIN: Float = 0.2F
const val DEFAULT_PROGRESS_ARC_WIDTH: Float = 0.1F
const val DEFAULT_PROGRESS_ARC_START_ANGLE: Float = 10F
const val DEFAULT_PROGRESS_ARC_VALUE: Float = 0.96F
//progressArcDot
const val DEFAULT_PROGRESS_ARC_DOT_RADIUS: Float = 1.5F
//progressArcContent
const val PROGRESS_ARC_CONTENT_DEFAULT_MARGIN = 0.4F
const val DEFAULT_PROGRESS_ARC_CONTENT_TEXT = "Of the success!"
const val DEFAULT_PROGRESS_ARC_CONTENT_INTERLINE = 0.1F
const val PARTICLE_DATA_REGEX = ";"
}
//primaryCircle
val DEFAULT_PRIMARY_CIRCLE_COLOR: Int = Color.GRAY
//SecondaryCircle
val DEFAULT_SECONDARY_CIRCLE_COLOR: Int = Color.DKGRAY
//Both circles
val DEFAULT_CIRCLES_SHADOW_COLOR: Int = Color.BLACK
//bigAverage
val DEFAULT_BIG_AVERAGE_SHADOW_COLOR: Int = Color.BLACK
//smallAverage
val DEFAULT_SMALL_AVERAGE_SHADOW_COLOR: Int = Color.BLACK
//Averages
val DEFAULT_AVERAGE_COLOR: Int = Color.WHITE
//progressArc
val DEFAULT_PROGRESS_ARC_COLOR: Int = Color.WHITE
//progressArcDot
val PROGRESS_ARC_DOT_DEFAULT_COLOR = Color.WHITE
//progressArcContent
val DEFAULT_PROGRESS_ARC_CONTENT_COLOR = Color.WHITE
//particles
val DEFAULT_PARTICLE_COLOR = Color.DKGRAY
//View
var animateInCirclesAnimationDuration = DEFAULT_ANIMATE_IN_CIRCLES_ANIMATION_DURATION
var animateInProgressArcAnimationDuration = DEFAULT_ANIMATE_IN_PROGRESS_ARC_ANIMATION_DURATION
var animateInSecondaryCircleDelay = DEFAULT_ANIMATE_IN_SECONDARY_CIRCLE_DELAY
//Circles
private val primaryCirclePaint: Paint = Paint()
private val secondaryCirclePaint: Paint = Paint()
var primaryCircleCenterDisplacement: Float by InvalidatingProperty(
DEFAULT_PRIMARY_CIRCLE_DISPLACEMENT
)
var secondaryCircleCenterDisplacement: Float by InvalidatingProperty(
DEFAULT_SECONDARY_CIRCLE_DISPLACEMENT
)
var primaryCircleColor: Int
set(value) {
primaryCirclePaint.color = value
invalidate()
}
get() = primaryCirclePaint.color
var secondaryCircleColor: Int
set(value) {
secondaryCirclePaint.color = value
invalidate()
}
get() = secondaryCirclePaint.color
var primaryCircleRadiusSCale: Float by InvalidatingProperty(1F)
var secondaryCircleRadiusScale: Float by InvalidatingProperty(1F)
fun setCirclesShadow(radius: Float, dx: Float, dy: Float, color: Int) {
primaryCirclePaint.setShadowLayer(radius, dx, dy, color)
secondaryCirclePaint.setShadowLayer(radius, dx, dy, color)
}
fun setBigAverageShadow(radius: Float, dx: Float, dy: Float, color: Int) =
bigAveragePaint.setShadowLayer(radius, dx, dy, color)
fun setSmallAverageShadow(radius: Float, dx: Float, dy: Float, color: Int) =
smallAveragePaint.setShadowLayer(radius, dx, dy, color)
//Big Average
private val bigAveragePaint: TextPaint = TextPaint()
var bigAverageText: String by InvalidatingProperty("")
//SmallAverage
private val smallAveragePaint: TextPaint = TextPaint()
var smallAverageText: String by InvalidatingProperty("")
//Both averages
var averageMargin: Float by InvalidatingProperty(DEFAULT_AVERAGE_MARGIN)
var averageColor: Int
get() = bigAveragePaint.color
set(value) {
bigAveragePaint.color = value
smallAveragePaint.color = value
invalidate()
}
//Progress Arc
private val progressArcPaint: Paint = Paint()
var progressArcMargin: Float by InvalidatingProperty(DEFAULT_PROGRESS_ARC_MARGIN)
var progressArcStrokeWidth: Float by InvalidatingProperty(DEFAULT_PROGRESS_ARC_WIDTH)
var progressArcStartAngle: Float by InvalidatingProperty(DEFAULT_PROGRESS_ARC_START_ANGLE)
var progressArcValue: Float by InvalidatingProperty(DEFAULT_PROGRESS_ARC_VALUE)
private val progressArcRectF: RectF = RectF()
//ProgressArcDot
private val progressArcDotPaint = Paint()
var progressArcDotRadius: Float by InvalidatingProperty(DEFAULT_PROGRESS_ARC_DOT_RADIUS)
//Progress Arc Content
private val progressArcContentPaint: TextPaint = TextPaint()
val progressArcContentMargin: Float by InvalidatingProperty(PROGRESS_ARC_CONTENT_DEFAULT_MARGIN)
var progressArcContentColor: Int
set(value) {
progressArcContentPaint.color = value
invalidate()
}
get() = progressArcContentPaint.color
var progressArcContentText: String by InvalidatingProperty(DEFAULT_PROGRESS_ARC_CONTENT_TEXT)
var progressArcContentTextInterline: Float by InvalidatingProperty(
DEFAULT_PROGRESS_ARC_CONTENT_INTERLINE
)
//Particles
private val particlePaint: Paint = Paint()
private val particles: ParticleSet =
ParticleSet(this)
var particlesColor: Int
set(value) {
particlePaint.color = value
invalidate()
}
get() = particlePaint.color
//Util
private val textBoundsRect: Rect = Rect()
private val textBoundsRect2: Rect = Rect()
constructor(context: Context?) : super(context) {
initView(null)
}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
initView(attrs)
}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
initView(attrs)
}
constructor(
context: Context?,
attrs: AttributeSet?,
defStyleAttr: Int,
defStyleRes: Int
) : super(context, attrs, defStyleAttr, defStyleRes) {
initView(attrs)
}
private fun initView(attrs: AttributeSet?) { //code }
override fun onDraw(canvas: Canvas?) { // code }
回答1:
Changing the kotlin-reflect version to 1.3.72 has worked for me:
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.72"
来源:https://stackoverflow.com/questions/63474372/kotlin-delegate-property-causes-a-preview-rendering-error-in-android-studio