最小 Android 应用程序中正常方法调用的奇怪静态方法错误

Strange static method error for normal method invocation in minimal Android app

在使用 Android 编译后,在我的 Android 10 设备上 运行 一些非常简单的代码(8 行方法;只有 2 行相关)时,我得到一个奇怪的运行时异常Studio Canary 7(或 6)使用 Jetpack Compose alpha12。

错误:

java.lang.NoSuchMethodError: No static method copy-0d7_KjU$default(JFFFFILjava/lang/Object;)J in class Landroidx/compose/ui/graphics/Color; or its super classes (declaration of 'androidx.compose.ui.graphics.Color' appears in /data/app/com.example.myapplication-GbQdisqKhWdQawA6_DsPkQ==/base.apk)
    at com.example.myapplication.MainActivity.onCreate(MainActivity.kt:15)
    at android.app.Activity.performCreate(Activity.java:7955)

代码:

package com.example.myapplication

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.activity.compose.setContent
import androidx.compose.ui.graphics.Color

class MainActivity : AppCompatActivity() {
 override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  val x = Color(0xFFBB86FC)
  val y = x.copy() // ******* line 15 *******
  setContent {
   Surface(color = MaterialTheme.colors.background) {
    Text(text = "hello world")
   }
  }
 }
}

顶级 build.gradle:

buildscript {
    ext {
        compose_version = '1.0.0-alpha12'
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.0-alpha07"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30-RC"
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

模块级别 build.gradle:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 29
        targetSdkVersion 30
        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'
        useIR = true
        freeCompilerArgs += [
                "-P",
                "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
        ]
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_version
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.activity:activity-compose:1.3.0-alpha02'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

罪魁祸首是行 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30-RC",它应该是没有 -RC1.4.30。我有初始 -RC 版本的原因是因为最新的 Android Studio Kotlin 插件版本确实是 -RC(令人沮丧的是 IntelliJ 没有发布正确的版本,即使它看起来是在 gradle 中可用),并且使用 1.4.30 给出了一个警告,它与 IDE 版本不同。忽略该警告并使用发布版本解决了该问题。

唉!