【问题标题】:Autocompletion not working in Android Studio Arctic Fox inside buildSrc自动补全在 buildSrc 中的 Android Studio 北极狐中不起​​作用
【发布时间】:2021-10-04 19:19:08
【问题描述】:

我正在使用 brew 安装的 Android Studio Arctic Fox 在 buildSrc 中编写一个自定义插件。经过一些测试,导入的自动完成存在问题。它在app/build.gradle.kts 内工作,我可以在import com.android.build.api.dsl.* 内工作,但在buildSrc 内,这在android 命名空间上引发了Unresolved reference

build.gradle.kts

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        extra["kotlin_version"] = "1.4.32"

        classpath("com.android.tools.build:gradle:7.0.2")
        classpath(kotlin("gradle-plugin", version = "1.4.32"))

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        google()
        mavenCentral()
    }
}
tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

build.gradle.kts 在 buildSrc 下:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.4.31"
    `kotlin-dsl`
}

repositories {
    google()
    mavenCentral()
}

tasks.withType<KotlinCompile>().configureEach {
    kotlinOptions {
        jvmTarget = "11"
        apiVersion = "1.4"
    }
}

dependencies {
    implementation("com.android.tools.build:gradle:7.0.2")
    implementation("com.android.tools.build:gradle-api:7.0.2")
    implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32")
    implementation(kotlin("stdlib"))
    gradleApi()
}

build.gradle.ktsapp:

import com.example.pythonLibs

plugins {
    id("com.android.application")
    kotlin("android")
    id("com.example.plugin")
}

android {
    compileSdk = 30
    ndkVersion = "23.0.7599858"

    sourceSets {
        getByName("main") {
            pythonLibs {
                srcDir("src/main/pythonLibs")
            }
        }
    }

    defaultConfig {
        applicationId = "com.example.embedpythontest"
        minSdk = 23
        targetSdk = 30
        versionCode = 1
        versionName = "1.0.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        // consumerProguardFiles = [file("consumer-rules.pro")]
        externalNativeBuild {
            cmake {
                //arguments("-DANDROID_STL=c++_shared")
                //cppFlags("")
            }
        }
    }

    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }

    externalNativeBuild {
        cmake {
            path = file("src/main/cpp/CMakeLists.txt")
            version = "3.21.3"
        }
    }

    compileOptions {
        sourceCompatibility(JavaVersion.VERSION_11)
        targetCompatibility(JavaVersion.VERSION_11)
    }

    kotlinOptions {
        jvmTarget = "11"
        apiVersion = "1.4"
    }
}

dependencies {
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
    implementation("androidx.appcompat:appcompat:1.3.1")
    implementation("com.google.android.material:material:1.4.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.1")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.3")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
}

buildSrc/src/main/kotlin/** 下的所有文件中,在执行import com.android.build.api.dsl.* 时,会显示一个问题,但构建项目是可以的。但是如果我在buildSrc/build.gradle.kts 中导入,它就不会被编译。但同样的事情可以在app/build.gradle.kts 内部完成。

为什么 Android Studio 找不到类?

【问题讨论】:

    标签: android-studio gradle


    【解决方案1】:

    这个问题是由于不熟悉 Gradle 中的依赖关系造成的。将dependncy 块更改为:

    dependencies {
       implementation("com.android.tools.build:gradle-api:7.0.2")
        implementation(kotlin("stdlib"))
        gradleApi()
    }
    

    here 复制的可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 2021-10-04
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      相关资源
      最近更新 更多