我想在 Android 中使用 safeargs 進行導航。不幸的是,我總是從 Grandle 檔案中得到一個錯誤,告訴“無法獲得型別為 org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler 的物件的未知屬性‘nav_version’”。
實際上我認為問題可能是因為混合使用 Java 和 Kotlin?我將一些 kotlin 檔案轉換為 Java,但在 build.gradle 檔案中插入 safeargs 的引數時收到此錯誤訊息。所以實際上我在 build.grandl 檔案中不再需要任何與 Kotlin 相關的東西,我試圖洗掉它們,但后來我收到了相同的錯誤訊息。你知道我該如何解決這個問題嗎?
這是我的 build.gradle 檔案:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.10"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
這里是第二個:
plugins {
id 'com.android.application'
id 'kotlin-android'
}
apply plugin: "androidx.navigation.safeargs"
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.nikhiljain.canvasdrawingsample"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
viewBinding {
enabled = true
}
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'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
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.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4. '
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
}
uj5u.com熱心網友回復:
我認為您需要在此行下方添加此內容 ext.kotlin_version = "1.4.10"
ext.nav_version = "2.3.5"
其中 2.3.5 是所需的版本,但對此我不太確定。
并且不確定您是否發布了整個 gradle 檔案,但是您在發布的部分中遺漏了這一點。
與您使用的方式相同ext.kotlin_version = "1.4.10",然后在您呼叫的依賴項部分
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
它也完全平等
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10"
你也可以這樣做
buildscript {
repositories {
google()
}
dependencies {
val nav_version = "2.3.5"
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
}
}
或者只是洗掉$kotlin_versionbuild.gradle中的所有內容并替換為 2.3.5 例如。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/350489.html
