Raj*_*nan 8 android android-gradle-plugin android-module android-jetpack-compose
我正在为基本对话框创建一个 Android 库。现在,我所拥有的只是一个basic-dialogs模块和一个app模块。我basic-dialogs从模块Activity中调用模块中的可组合项,当我尝试运行应用程序时,app我不断收到下面有关 a 的异常错误消息。No static method代码可能有什么问题?
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)
Run Code Online (Sandbox Code Playgroud)
活动
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.")
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
基本对话框可组合
@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)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
项目 Gradle 文件
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
}
Run Code Online (Sandbox Code Playgroud)
应用程序模块 Gradle 文件
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"
}
Run Code Online (Sandbox Code Playgroud)
basic-dialog 模块成绩文件
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"
}
Run Code Online (Sandbox Code Playgroud)
Raj*_*nan 37
需要将以下内容添加到basic-dialogs模块的build.gradle文件中:
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
Run Code Online (Sandbox Code Playgroud)
整个 Gradle 文件
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"
}
Run Code Online (Sandbox Code Playgroud)
希望我能帮别人节省我刚刚花费的 3 个小时。如果您在多模块项目中使用 Compose BOM,并且需要与 BOM 中指定的各个依赖项不同的更新版本,并且您的模块也共享 BOM,那么在模块之间共享依赖项时要非常小心。
在我的例子中,我有:
implementation(platform(libs.androidx.compose.bom))
api("androidx.compose.material3:material3:1.1.0-rc01") // Relatively new and needed for date & time pickers
Run Code Online (Sandbox Code Playgroud)
在一个模块中,然后加载依赖于此模块的其他模块。其中一些模块还指定:
implementation(libs.androidx.compose.material3)
Run Code Online (Sandbox Code Playgroud)
这对应用程序造成了严重破坏,其中一个模块完全正常,而其他不相关的模块在运行时崩溃,告诉我他们不知道 TextField 是什么。
| 归档时间: |
|
| 查看次数: |
12735 次 |
| 最近记录: |