如何正确实施Safeargs?

发布于 2025-02-02 20:00:14 字数 3475 浏览 4 评论 0原文

我正在尝试遵循教程,以实现和使用房间数据库 YouTube-Link 。 我的问题是,与视频中显示的内容相比,我的gradle文件看起来很不一样,以至于我不知道在哪里放置ID “ navigation.safeargs.kotlin”以及

classpath "androidx.navigation-safe-args-gradle-plugin:2.3.0-rc01"

我可能会可能需要。我尝试了很多地方,但它永远无法使用,我找不到答案,因此,如果有人知道,请帮助我。

build.gradle(project:...):

plugins {
        id 'com.android.application' version '7.2.0' apply false
        id 'com.android.library' version '7.2.0' apply false
        id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }

build.gradle(模块:...):

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example. ..."
        minSdk 26
        targetSdk 32
        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'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.exifinterface:exifinterface:1.3.3'
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation fileTree(dir: "libs", include: ["*.jar"])
    // Room components
    implementation "androidx.room:room-runtime:2.4.2"
    kapt "androidx.room:room-compiler:2.4.2"
    implementation "androidx.room:room-ktx:2.4.2"
    androidTestImplementation "androidx.room:room-testing:2.4.2"

    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-common-java8:2.4.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"

    // Kotlin components
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.21"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
}

settings.gradle(...):

    pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "..."
include ':app'

I am trying to follow a tutorial in order to implement and use a room database YouTube-link.
My problem is that my gradle files look so different compared to what is shown in the video that I have no idea where to put the id "navigation.safeargs.kotlin" and the

classpath "androidx.navigation-safe-args-gradle-plugin:2.3.0-rc01"

or anything else that I might need. I have tried a bunch of places but it never works and I couldn't really find an answer, so if anyone knows, pls help me out.

build.gradle(Project:...):

plugins {
        id 'com.android.application' version '7.2.0' apply false
        id 'com.android.library' version '7.2.0' apply false
        id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }

build.gradle(Module:...):

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example. ..."
        minSdk 26
        targetSdk 32
        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'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.exifinterface:exifinterface:1.3.3'
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation fileTree(dir: "libs", include: ["*.jar"])
    // Room components
    implementation "androidx.room:room-runtime:2.4.2"
    kapt "androidx.room:room-compiler:2.4.2"
    implementation "androidx.room:room-ktx:2.4.2"
    androidTestImplementation "androidx.room:room-testing:2.4.2"

    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-common-java8:2.4.1"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"

    // Kotlin components
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.21"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
}

settings.gradle(...):

    pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "..."
include ':app'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相权↑美人 2025-02-09 20:00:14

在您的build.gradle(项目级别)中尝试添加:

plugins {
    id 'com.android.application' version '7.1.3' apply false
    id 'com.android.library' version '7.1.3' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
    id 'androidx.navigation.safeargs.kotlin' version '2.5.1' apply false
}

在build.gradle(模块级别)中添加:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'androidx.navigation.safeargs.kotlin'
}

In your build.gradle (project level) try adding:

plugins {
    id 'com.android.application' version '7.1.3' apply false
    id 'com.android.library' version '7.1.3' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
    id 'androidx.navigation.safeargs.kotlin' version '2.5.1' apply false
}

and in the build.gradle (module level) add:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'androidx.navigation.safeargs.kotlin'
}
鸠魁 2025-02-09 20:00:14

GO 直接到源

代码> build.gradle :

buildscript {
    repositories {
        google()
    }
    dependencies {
        val nav_version = "2.4.2"
        classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
    }
}

app/module build.gradle

plugins {
    id("androidx.navigation.safeargs")
    // or for Kotlin-only projects
    id("androidx.navigation.safeargs.kotlin")
}

因此,在您的顶级文件中,您需要google()repositories中块,依赖项中的classPath block。如果您没有其中一个块,只需创建它 - 它们进入buildscript块中,请参阅? nav_version val只是一个变量,因此您可以通过参考该变量来使所有导航组件使用相同的版本,而实际的版本号仅在一个地方设置。如果您愿意,您只需在依赖项语句中写入数字 - 但是请确保所有导航组件都使用相同的版本!

id(“ androidx.navigation.safeargs”)粘贴在模块的build> build文件 plugins block中。订单有时对其中一些很重要,因此请在底部尝试一下。语法与当前文件中的外观有所不同,但这只是另一种编写方式 - 您可以混合和匹配

Go straight to the source IMO

Top-level build.gradle:

buildscript {
    repositories {
        google()
    }
    dependencies {
        val nav_version = "2.4.2"
        classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
    }
}

App/module build.gradle:

plugins {
    id("androidx.navigation.safeargs")
    // or for Kotlin-only projects
    id("androidx.navigation.safeargs.kotlin")
}

So in your top-level file, you need google() in your repositories block, and the classpath in your dependencies block. If you don't have one of those blocks, just create it - they go inside the buildscript block, see? The nav_version val is just a variable so you can make all your Navigation components use the same version by referring to that, and the actual version number is only set in one place. You can just write the number in the dependency statement if you like - make sure all the Navigation components are using the same version though!

Stick the id("androidx.navigation.safeargs") line in your module's build file's plugins block. The order sometimes matters for some of these, so try it at the bottom. The syntax is a little different from how they look in your current file, but it's just another way of writing it - either's fine, you can mix and match

坦然微笑 2025-02-09 20:00:14

使用这个:

id 'androidx.navigation.safeargs.kotlin' version '2.4.0' apply false

Use this one:

id 'androidx.navigation.safeargs.kotlin' version '2.4.0' apply false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文