【问题标题】:Adding dependency to androidx.fragment:fragment-ktx:1.3.0-beta01 causes runtime exception将依赖项添加到 androidx.fragment:fragment-ktx:1.3.0-beta01 会导致运行时异常
【发布时间】:2020-12-01 15:28:02
【问题描述】:

我需要在使用 Android 导航和动态功能的应用中添加对 androidx.fragment:fragment-ktx:1.3.0-beta01 的依赖项。我需要调用setFragmentResult 的依赖项,但应用程序将停止工作,但出现异常

“找不到类定义”

在运行时。请帮忙...

更新

这是我的应用构建 gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: "androidx.navigation.safeargs.kotlin"

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "it.kfi.lorikeetmobile"
        minSdkVersion 25
        targetSdkVersion 29
        versionCode 2
        versionName "1.0b"
        multiDexEnabled true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        /*javaCompileOptions {
            annotationProcessorOptions {
                arguments = [
                        "room.incremental":"true",
                        "room.expandProjection":"true"]
            }
        }*/
    }

    //kotlin DSL
//    configurations.all {
//        resolutionStrategy {
//            force("org.antlr:antlr4-runtime:4.7.1")
//            force("org.antlr:antlr4-tool:4.7.1")
//        }
//    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

// To inline the bytecode built with JVM target 1.8 into
// bytecode that is being built with JVM target 1.6. (e.g. navArgs)


    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

    /*dataBinding {
        enabled = true
    }*/

    kapt { generateStubs = true }

    buildFeatures {
        dataBinding = true
        viewBinding = true
    }

    dynamicFeatures = [":jay", ":stock", ":meddler", ':goods', ':flash', ':inventory', ':harry', ':louis', ':avery']
    buildToolsVersion buildToolsVer
    //kotlin DSL
    /*configurations.all {
        resolutionStrategy {
            force("org.antlr:antlr4-runtime:4.7.1")
            force("org.antlr:antlr4-tool:4.7.1")
        }
    }*/
}

dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'com.google.android.material:material:1.2.1'

    implementation 'androidx.viewpager2:viewpager2:1.0.0'

    api 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:$desugar_ver"

    api "androidx.navigation:navigation-ui-ktx:$nav_version"
    api "androidx.navigation:navigation-fragment-ktx:$nav_version"
    api "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9"

    //ROOM
    kapt "androidx.room:room-runtime:$room_version"
    //kapt "android.arch.persistence.room:compiler:$room_version"
    kapt "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor

    // optional - Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:$room_version"
    //implementation "androidx.room:room-coroutines:$room_version"

    // optional - RxJava support for Room
    implementation "androidx.room:room-rxjava2:$room_version"

    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation "androidx.room:room-guava:$room_version"

    // Test helpers
    testImplementation "androidx.room:room-testing:$room_version"


    //PAGING
    //implementation "androidx.paging:paging-runtime:$paging_version"
    implementation "androidx.paging:paging-runtime-ktx:$paging_version"

    kapt "com.android.databinding:compiler:3.1.4"

    api 'com.squareup.retrofit2:converter-gson:2.9.0'
    api 'com.squareup.retrofit2:retrofit:2.9.0'
    api 'com.squareup.okhttp3:logging-interceptor:4.9.0'

    api "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

    implementation project(path: ':avatarLib')
    implementation project(path: ':ScanditCaptureCore')
    implementation project(path: ':ScanditLabelCapture')

    implementation "androidx.recyclerview:recyclerview-selection:1.1.0-rc03"
    implementation "androidx.cardview:cardview:1.0.0"
    implementation "androidx.paging:paging-runtime-ktx:$paging_version"

    implementation "androidx.fragment:fragment-ktx:1.3.0-beta01"
}

更新 2

如果我将依赖项从实际使用它的动态功能移动到主应用程序(应用程序 gradle),则应用程序不会启动,并且出现以下异常:

无法开始活动

组件信息{it.kfi.lorikeetmobile/it.kfi.lorikeetmobile.ui.MainActivity}: android.view.InflateException:二进制 XML 文件第 12 行:二进制 XML 文件第 11 行:膨胀类片段时出错

....我不明白该怎么办了

【问题讨论】:

  • 请添加您的 build.gradle
  • 我已经添加了。发送

标签: android kotlin android-fragments navigation


【解决方案1】:

您似乎需要将implementation "androidx.activity:activity-ktx:1.1.0" 添加到您的依赖项列表中才能工作

【讨论】:

  • 不,没有它应该可以工作,因为 setFragmentResult() 的导入是 import androidx.fragment.app.setFragmentResult
  • fragment-ktx 库确实依赖于 activity 库 - 我不确定它们在哪里,但 1.3.0-alpha08 更改日志 (developer.android.com/jetpack/androidx/releases/…) 提到它使用了activity 1.2.0-alpha08 中引入的 API,因此您可能也需要至少那个版本(不过可能是当前的1.2.0-beta01 - 这些都是实验版本,而且 alpha 版本特别松散)
  • @cactustictac 不,对活动的依赖没有区别。
  • @Sarah 当我移动应用程序 gradle 中的依赖项而不是像应用程序在启动时崩溃之前那样移动动态功能 gradle 文件时,我已经用异常消息更新了我的帖子
猜你喜欢
  • 2011-02-10
  • 2015-01-03
  • 2019-10-15
  • 2016-03-06
  • 2016-10-18
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多