https://kotlinlang.org logo
#glance
Title
# glance
k

Kevin

03/13/2023, 2:39 PM
I’ll post my question in a thread cause the code blocks will bloat the view here
I can’t seem to figure out why my basic startup of a glance widget throws an error. It throws:
Copy code
Error in Glance App Widget
java.lang.AbstractMethodError: abstract method "void androidx.glance.appwidget.GlanceAppWidget.Content(androidx.compose.runtime.Composer, int)"
which to me seems weird cause it looks like the Content function now expects two parameters, but I can’t find those parameters being defined anywhere (also not really in the documentation)… So it looked to me like I had some sort of dependency mismatch or something… In my project’s build.gradle I’ve got
implementation "androidx.glance:glance-appwidget:1.0.0-alpha05"
in my dependencies section, Using
kotlin_version = "1.8.0"
and
Copy code
classpath('com.android.tools.build:gradle:7.4.1')
classpath("com.facebook.react:react-native-gradle-plugin")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
I’ve got a widget:
Copy code
package com.delta.widgets

import androidx.compose.runtime.Composable
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import androidx.glance.text.Text

class BiggestMoversAppWidgetReceiver: GlanceAppWidgetReceiver() {

 override val glanceAppWidget: GlanceAppWidget
  get() = BiggestMoversWidget();

 private class BiggestMoversWidget: GlanceAppWidget() {

  @Composable
  override fun Content() {
   Text(text = "Hello World")
  }
 }
}
with app_widget_info.xml resource file:
Copy code
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="<http://schemas.android.com/apk/res/android>"
  android:minWidth="72dp"
  android:minHeight="36dp"
  android:resizeMode="none"
  android:minResizeWidth="72dp"
  android:minResizeHeight="36dp"
  android:updatePeriodMillis="999999999"
 />
with AndroidManifest
Copy code
<receiver android:name=".widgets.BiggestMoversAppWidgetReceiver" android:exported="true">
 <intent-filter>
  <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
 </intent-filter>

 <meta-data android:name="android.appwidget.provider" android:resource="@xml/app_widget_info" />
</receiver>
m

Marcel Pinto

03/14/2023, 9:48 AM
Hmm not really sure, there are changes in the latest snapshot around Content function but I don’t see how this would happen. Maybe @Willie Koomson knows more
k

Kevin

03/14/2023, 12:00 PM
Ok, after looking into this for about 6 hours (I’m a js react-native dev who just wanted to throw himself into making a widget, so these environments are pretty exotic to me). I don’t know what the exact factor is what fixed it because I just kept trying to build until it succeeded and tackled every new error one by one, but I’d like to share what I changed so maybe it might bring any insights for you what might’ve gone wrong in my case. I added
kotlinOptions
with
jvmTarget = "1.8"
as described in the docs, but had an error
Could not find method kotlinOptions() for arguments ...
, I think I fixed this with the missing line
apply plugin: 'kotlin-android'
. I added
compose = true
in
buildFeatures
and also
Copy code
composeOptions {
    kotlinCompilerExtensionVersion = "1.4.3"
  }

  compileOptions {
      sourceCompatibility = JavaVersion.VERSION_1_8
      targetCompatibility = JavaVersion.VERSION_1_8
  }
I also added (not sure if required)
Copy code
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
and next to the already present
implementation "androidx.glance:glance-appwidget:1.0.0-alpha05"
I added
implementation "androidx.glance:glance:1.0.0-alpha05"
. Seems logical to add for me And also added
apply plugin: 'org.jetbrains.kotlin.android'
So not sure what exactly was the culprit here, but I can finally try out Glance 😍 Thanks anyway for checking out the issue and trying to pass it on, glad I was able to get it running.
m

Marcel Pinto

03/14/2023, 12:03 PM
Oh yeah you need compose enabled and that requires some of these wiring. Since its still in alpha there is no documentation for it yet so it's hard to figure these things out I recommend to check this compose tutorial https://developer.android.com/courses/jetpack-compose/course
k

Kevin

03/14/2023, 2:59 PM
Alright, definitely will go through the tutorial, thanks! Yeah I normally try to refrain from using alpha stages, but creating a widget with regular java and RemoteViews was too limited for me, so I would be psyched if Glance opens these doors for me! 🙏
267 Views