I'm creating an Android Library for basic dialogs. Right now, all I have is a basic-dialogs module along with the app module. I'm calling a composable in the basic-dialogs module from the Activity in the app module, and I keep getting the exception error message below about a No static method when I try to run the app. What could be wrong with the code?
java.lang.NoSuchMethodError: No static method BasicDialog(Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V in class Lcom/<domain>/basic_dialogs/BasicDialogKt; or its super classes (declaration of 'com.<domain>.basic_dialogs.BasicDialogKt' appears in /data/app/~~DOqVbMGsnzDWFS7_YdNgtw==/com.<domain>.composedialogs-_5IuirMOFh5F8pL884Urnw==/base.apk!classes2.dex)
Activity
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeDialogsTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
BasicDialog(onDismiss = { }, onConfirmClick = { }, title = "Test Dialog", confirmButtonText = "Open") {
Text("This is a test dialog.")
}
}
}
}
}
}
BasicDialog Composable
#OptIn(ExperimentalComposeUiApi::class)
#Composable
fun BasicDialog(
title: String = "",
confirmButtonText: String,
onDismiss: () -> Unit,
onConfirmClick: () -> Unit,
content: #Composable () -> Unit
) {
Surface(
shape = MaterialTheme.shapes.medium,
color = MaterialTheme.colors.surface,
modifier = Modifier
.padding(4.dp)
) {
Dialog(
onDismissRequest = onDismiss,
properties = DialogProperties(
dismissOnBackPress = true,
dismissOnClickOutside = true,
usePlatformDefaultWidth = false
),
) {
if (title != "") {
Text(title)
Spacer(Modifier.padding(5.dp))
}
content()
Spacer(Modifier.padding(5.dp))
TextButton(onClick = onDismiss) {
Text(text = stringResource(id = R.string.dialog_cancel), fontSize = 16.sp)
}
TextButton(onClick = { onConfirmClick() }) {
Text(text = confirmButtonText, fontSize = 16.sp)
}
}
}
}
Project Gradle file
buildscript {
ext {
compose_version = '1.1.0-beta01'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.2' apply false
id 'com.android.library' version '7.2.2' apply false
id 'org.jetbrains.kotlin.android' version '1.5.31' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app module Gradle file
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.<domain>.composedialogs"
minSdk 27
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
implementation project(path: ':basic-dialogs')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
}
basic-dialog module Grade file
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
minSdk 27
targetSdk 32
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
}
The following needs to be added to the basic-dialogs module's build.gradle file:
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
Entire Gradle file
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
minSdk 27
targetSdk 32
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
}
dependencies {
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
}
Cleaning build, then rebuilding fixed the issue for me.
Related
There are two warning:-
warning: unknown enum constant ISO.DATE_TIME
reason: class file for org.springframework.format.annotation.DateTimeFormat$ISO not found
One or more classes has class file version >= 56 which is not officially supported.
The Gradle file is:-
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-parcelize'
id 'com.google.dagger.hilt.android'
id 'kotlin-kapt'
}
def oktaProperties = new Properties()
rootProject.file("okta.properties").withInputStream { oktaProperties.load(it) }
android {
compileSdk 33
defaultConfig {
applicationId ""
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
multiDexEnabled = true
...
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
freeCompilerArgs += [
'-opt-in=kotlin.RequiresOptIn',
]
}
}
kotlinOptions {
jvmTarget = '11'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation files('libs/abc-models.jar')
implementation files('libs/abc-models.jar')
...
I think it's coming from the .jar file that I imported. I tried different Java Versions too but it's not working.
I want to use LottieAnimationView in my project but it doesn't show in my library and i can't add it.
My Module/build.gradle file:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android' }
android {
compileSdk 32
defaultConfig {
applicationId "com.example.denemecalisma"
minSdk 23
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
sourceSets {
main {
assets {
srcDirs 'src\\main\\assets'
}
}
} }
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
implementation 'com.airbnb.android:lottie:5.2.0' }
I added it in depencies and synced build.gradle but i can't see it. Can you help me about this ?
Make sure you added MavenCentral in you gradle
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
I added compose to an existing project.
I rewrote a fragment ui with compose, and when I'm starting the
fragment it takes a long time to start.
Adding a fragment from another fragment:
val fragment = FragmentWithComposeUi()
requireActivity().addFragment(fragment, R.id.fragment_container, "FragmentWithComposeUi")
addFragment() function to add the fragment.
fun FragmentActivity.addFragment(fragment: Fragment, container: Int, tag:String) {
val currentFragment = supportFragmentManager.findFragmentByTag(tag)
if (currentFragment == null) {
supportFragmentManager.beginTransaction()
.setReorderingAllowed(true)
.add(container, fragment, tag)
.addToBackStack(tag)
.commit()
}
}
FragmentWithComposeUi class :
class FragmentWithComposeUi: Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply{
setContent {
//some ui
}
}
}
build.gradle
I tried using the new compose version 1.3.0-alpha01 either, it doesn't help.
buildscript {
ext {
compose_version = '1.1.1'
compose_compiler_version = '1.2.0'
}
repositories {
mavenCentral()
}
dependencies {
//
}
}
plugins {
id 'com.android.application' version '7.2.0' apply false
id 'com.android.library' version '7.2.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle/app
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.e...."
minSdk 26
targetSdk 32
versionCode 1
versionName "1.0"
multiDexEnabled = true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
viewBinding {
enabled = true
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
resources.excludes.add("META-INF/*")
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_compiler_version
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation("androidx.multidex:multidex:2.0.1")
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.lifecycle:lifecycle-reactivestreams-ktx:2.5.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
//compose
implementation 'androidx.activity:activity-compose:1.3.1'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
...
}
Any reasons why it happens?
Thanks !
I just created a project and chose Compose Activity. When I try to click and run the Preview I get the following error:
androidx.compose.ui.tooling.PreviewActivity is not an Activity subclass or alias
Default code:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DemoComposeTheme {
// A surface container using the 'background' color from the theme
Surface(color = MaterialTheme.colors.background) {
Greeting("Android")
}
}
}
}
}
#Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
}
#Preview(showBackground = true)
#Composable
fun DefaultPreview() {
DemoComposeTheme {
Greeting("Android")
}
}
With the default gradle configuration:
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdk 30
defaultConfig {
applicationId "com.personaldemo.democompose"
minSdk 28
targetSdk 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
useIR = true
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
kotlinCompilerVersion '1.5.10'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
}
I have a new Android project and I want to make use of Jetpack Compose, but following the steps outlined here gives me the following build error:
Unable to load class 'org.jetbrains.kotlin.gradle.tasks.KotlinCompile'.
This is an unexpected error. Please file a bug containing the idea.log file.
Perhaps I'm compounding the issue by using Kotlin DSL build.gradle.kts, but here's my gradle setup:
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("kotlin-kapt")
id("dagger.hilt.android.plugin")
}
android {
compileSdkVersion(30)
defaultConfig {
applicationId = "..."
minSdkVersion(24)
targetSdkVersion(30)
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility(JavaVersion.VERSION_1_8)
targetCompatibility(JavaVersion.VERSION_1_8)
}
buildFeatures {
dataBinding = true
compose = true
}
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
composeOptions {
kotlinCompilerVersion = "1.4.20"
kotlinCompilerExtensionVersion = "1.0.0-alpha08"
}
}
dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
//...
}
Try '1.0.0-alpha09'and Kotlin version "1.4.21" and you should be good to go.
Have you got this in your buildscript?
buildscript {
ext {
compose_version = '1.0.0-alpha09'
}
ext.kotlin_version = "1.4.21"