【问题标题】:Why Android Studio uses Java classes instead of Kotlin为什么 Android Studio 使用 Java 类而不是 Kotlin
【发布时间】:2022-01-09 13:29:13
【问题描述】:

我是 kotlin 的新手。于是我在Android Studio中创建了一个kotlin项目,并导入了Fragment-ktx和Activity-ktx依赖。因此,在片段中,当我右键单击片段然后转到->实现时,它只会打开 Fragment.java 文件。使用 Fragment-ktx 依赖项时这是正常的吗? 任何帮助都会很棒。

这是我的 app.gradle 文件

android {
    compileSdkVersion 31
    defaultConfig {
        applicationId "com.xxxxxxx.xxxxxxxxxx"
        minSdk 23
        targetSdk 31
        versionCode 117
        versionName "3.2.11"
        vectorDrawables.generatedDensities = []
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/proguard/androidx-annotations.pro'
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {
    implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
    implementation platform('com.google.firebase:firebase-bom:29.0.3')
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-crashlytics-ktx'
    implementation 'com.google.firebase:firebase-messaging-ktx'
    implementation "androidx.core:core-ktx:1.7.0"
    implementation "androidx.activity:activity-ktx:1.4.0"
    implementation "androidx.fragment:fragment-ktx:1.4.0"
    implementation "androidx.collection:collection-ktx:1.2.0"
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation "androidx.preference:preference-ktx:1.1.1"
    implementation "androidx.biometric:biometric-ktx:1.2.0-alpha04"
    implementation 'androidx.work:work-runtime-ktx:2.7.1'
    implementation 'com.android.billingclient:billing:4.0.0'
    implementation 'com.android.volley:volley:1.2.1'
    implementation 'com.google.android.gms:play-services-ads:20.5.0'
    implementation 'com.google.android.play:core-ktx:1.8.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
    implementation 'com.github.lzyzsd:circleprogress:1.2.1'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
    implementation 'com.nabinbhandari.android:permissions:3.8'
    implementation 'com.nex3z:toggle-button-group:1.2.3'
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
    implementation "io.github.iamthevoid.batteryview:batteryview:0.6.1"
}

【问题讨论】:

    标签: java android kotlin


    【解决方案1】:

    没什么好惊讶的。整个原生 SDK 和 大多数 Jetpack 库都是纯 Java。

    fragment-ktx 在继承androidx.fragment 包的基础上仅包含一堆片段管理器和视图模型的扩展。

    【讨论】:

    • 那么改用 Kotlin 会更好吗?性能明智?
    • @YasiruNayanajith 没有太大区别,kotlin 和 java 源都编译成 jvm 字节码。
    猜你喜欢
    • 1970-01-01
    • 2019-01-11
    • 2014-10-25
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 2018-11-04
    相关资源
    最近更新 更多