9

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'
}
1
  • 2
    can you share your manifest and gradle ? Commented Jul 27, 2018 at 2:53

4 Answers 4

15

You set your package name to kotlin.

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

5

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.

3

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 this

com.amirreza.mykotlinproject
1

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"]
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.