【问题标题】:Error: "Only the Kotlin standard library is allowed to use the 'kotlin' package"错误:“只允许 Kotlin 标准库使用 'kotlin' 包”
【发布时间】:2018-07-27 02:48:10
【问题描述】:

我想在 Android studio 3.0.1 中构建我的第一个 kotlin 项目。但我遇到了这两个错误:

  1. 只允许 Kotlin 标准库使用 'kotlin' 包。

  2. 错误:任务 ':app:compileDebugKotlin' 执行失败。

如何使用或添加 Kotlin 标准库?

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kotlin.amirreza.mykotlinproject">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    </manifest>

分级:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "kotlin.amirreza.mykotlinproject"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.0.0-beta1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:0.5'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'
}

【问题讨论】:

  • 你能分享你的清单和 gradle 吗?

标签: android android-studio kotlin


【解决方案1】:

可以通过在调用 kotlinc 时添加 -Xallow-kotlin-package 参数来解决此问题,这就是 Kotlin 构建自身所做的事情。但是更改包名称是一种更简洁的解决方案。

【讨论】:

    【解决方案2】:

    正如@EmmanuelBourg 正确提到的,下面有一些代码可以做到这一点

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions.freeCompilerArgs += ["-Xallow-kotlin-package"]
    }
    

    【讨论】:

      【解决方案3】:

      您将包名称设置为kotlin

      .kt 文件中的所有package kotlin 语句更改为package mykotlintest 之类的内容

      【讨论】:

        【解决方案4】:

        正如错误所说:

        只允许 Kotlin 标准库使用 'kotlin' 包

        您不能使用 'kotlin' 作为您的包名,并且您的包 (applicationId) 是:

        kotlin.amirreza.mykotlinproject

        因此,您必须通过重命名或使用另一个包名称创建新应用程序将其更改为其他内容

        【讨论】:

          猜你喜欢
          • 2021-09-18
          • 2019-12-22
          • 2019-01-22
          • 2019-04-05
          • 2017-09-01
          • 2021-04-01
          • 1970-01-01
          • 2019-10-04
          • 2018-01-24
          相关资源
          最近更新 更多