编译Android项目遇到的bugs

249 阅读1分钟

编译遇到的bug集合

  • Gradle 8.0

  • AGP 7.2.0

  • 依赖方式

    dependencies {
        implementation("androidx.core:core-ktx:1.10.1")
        implementation("androidx.appcompat:appcompat:1.6.1")
        implementation("com.google.android.material:material:1.11.0")
        implementation(projects.lib)
    }
    

    假如想以implementation(projects.lib)这样的方式来依赖本地库,则需要在settings.gradle.kts文件中添加如下代码

    enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")//主要的配置
    include(
        "app",
        "lib"//自己项目中创建的Android Library的名字
    )
    
  • lib库中写的kotlin类/接口文件,在主程中也能引用到,但是一编译就报错Unresolved reference: xxx image.png 此时需要检查lib库是否添加了kotlin

    plugins {
        id("com.android.library")
        kotlin("android")//把这行加上就可以了
    }
    
  • 'compileDebugJavaWithJavac' task (current target is 1.8) and 'compileDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.

    这个错误是需要统一compileDebugJavaWithJavaccompileDebugKotlin使用的jdk版本

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    //看看是不是这个忘了加了
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }