dre*_*der 4 android kotlin android-architecture-components android-architecture-navigation android-safe-args
我遇到名为的导航组件的问题safeargs
。
我用classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.0.0'
在构建gradle。
在中应用插件androidx.navigation.safeargs.kotlin
时app/build.gradle
,出现以下错误:
原因:androidx.navigation.safeargs只能与androidx项目一起使用
app / build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'androidx.navigation.safeargs.kotlin'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.test.navigationexample"
minSdkVersion 14
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha4'
implementation 'com.google.android.material:material:1.1.0-alpha05'
implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
}
Run Code Online (Sandbox Code Playgroud)
小智 19
另一件要记住的事情是,它apply plugin:"androidx.navigation.safeargs.kotlin"
不应该是apply
build.gradle上最重要的。
这不起作用:
apply plugin: 'androidx.navigation.safeargs.kotlin'
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
Run Code Online (Sandbox Code Playgroud)
这有效:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs.kotlin'
Run Code Online (Sandbox Code Playgroud)
按照“ 迁移到AndroidX”,您的gradle.properties
文件必须包含以下几行:
android.useAndroidX=true
android.enableJetifier=true
Run Code Online (Sandbox Code Playgroud)
第一行,android.useAndroidX=true
是androidx.navigation.safeargs.kotlin
用来确保您正在使用AndroidX项目的用途(Android Studio中的其他工具也使用它来生成正确的类,例如,当您使用任何模板时)。
小智 9
唯一对我有用的是。
在项目的 build.gradle 文件顶部:
buildscript {
repositories {
google()
}
dependencies {
def nav_version = "2.4.2"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}
}
Run Code Online (Sandbox Code Playgroud)
在插件下的同一文件中添加:
plugins {
...
id 'androidx.navigation.safeargs.kotlin' version '2.4.2' apply false
}
Run Code Online (Sandbox Code Playgroud)
在模块的 build.gradle 文件中,在插件下添加:
plugins {
...
id 'androidx.navigation.safeargs.kotlin'
}
Run Code Online (Sandbox Code Playgroud)
将其放入classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0"
您的
项目build.gradlebuildscript.dependencies
部分中。
但放入apply plugin: 'androidx.navigation.safeargs'
您的应用程序build.gradle 中。