Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: "Only the Kotlin standard library is allowed to use the 'kotlin' package"

I want to build my first kotlin project in Android studio 3.0.1 . but i got these 2 errors :

  1. Only the Kotlin standard library is allowed to use the 'kotlin' package .

  2. Error:Execution failed for task ':app:compileDebugKotlin'.

how can i use or add the Kotlin standard library?

manifest:

<?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>

gradle:

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'
}
like image 311
Amirreza N Avatar asked Jul 27 '18 02:07

Amirreza N


4 Answers

You set your package name to kotlin.

Change all package kotlin statements in your .kt files to something like package mykotlintest

like image 167
Alexander Egger Avatar answered Nov 12 '22 06:11

Alexander Egger


It's possible to work around this issue by adding the -Xallow-kotlin-package argument when kotlinc is invoked, that's what Kotlin does to build itself. But changing the package name is a cleaner solution.

like image 28
Emmanuel Bourg Avatar answered Nov 12 '22 06:11

Emmanuel Bourg


As error says :

Only the Kotlin standard library is allowed to use the 'kotlin' package

You can not use 'kotlin' for your package name and your Package (applicationId) is :

kotlin.amirreza.mykotlinproject

So you must change it to something else with renaming or creating new applicaton with another package name

like image 2
Radesh Avatar answered Nov 12 '22 05:11

Radesh


As @EmmanuelBourg correctly mentioned, below there is some code to do this

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions.freeCompilerArgs += ["-Xallow-kotlin-package"]
}
like image 1
Vlad Avatar answered Nov 12 '22 05:11

Vlad