Robertas Setkus
Robertas Setkus

Reputation: 3161

Android Studio library "error: package does not exist"

I have created Android library as Android Studio module. Added as dependency to my root module. While coding I can import any class from library package but while I'm trying run the application I'm getting an error package some.mylibrary.project does not exist.

build.gradle root module

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
    }
}
apply plugin: 'com.android.application'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.android.support:appcompat-v7:20.+'
    compile 'com.google.android.gms:play-services:5.+'
    compile project(':libraries:mylibrary')
}

android {
    compileSdkVersion 17
    buildToolsVersion "20.0.0"

    lintOptions {
        disable 'InvalidPackage'
        checkReleaseBuilds false
        abortOnError false
    }

    ***
}

build.gradle library module

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'idea'

android {
    compileSdkVersion 17
    buildToolsVersion "20.0.0"

     *****    
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

settings.gradle

include ':libraries:mylibrary'

P.S. I have to mention that the project was exported from Eclipse IDE so the project structure is different from default one.

Upvotes: 42

Views: 96757

Answers (7)

Miguel Tomás
Miguel Tomás

Reputation: 1911

This error happens when you change the package name and try to run the code. this occurs because the Android studio still has the cache of your package with an old name.

Also, cross-check all the imports as well as an imported package in your code so that no different package name exists. For example, is common for this error to refer to another imported file near where the error is occurring. Check previous imports near.

To fix this error you can try to do an 'invalidate caches / Restart' option from the File menu in Android Studio. Choose “Invalidate and restart option” and close Android Studio.

Another reason for this error is when one changes the project's path root folder or in any of the modules it depends. In this particular case, to fix this error you need to remove the affected modules, and re-add them again. Next don't forget to do an 'invalidate caches / Restart' option from the File menu in Android Studio. Choose “Invalidate and restart option” and close Android Studio.

Clean your project from Android Studio:

  • “Build -> Clean Project”. This will clear your build folders.
  • Remove your .gradle directory from the root of your project. It contains some Gradle cache files.
  • Delete also the .idea directory (make a backup before). It contains some project configuration files.

Restart Android Studio.

Finally

if the error still persists, you need to move the affected files to an external directory of the project's root folder. On Android Studio, create manually each filename as previously, and copy the code inside from the old file. This will definitely solve this error.

Upvotes: 3

sanya5791
sanya5791

Reputation: 478

in my case kotlin-android plugin was missed in module build.gradle file

// Inside each module using kotlin
plugins {
    ...
    id 'kotlin-android'
}

Please see more details in Add Kotlin to an existing app doc

Upvotes: 3

Mehmet Agah Balbay
Mehmet Agah Balbay

Reputation: 69

The answer above is somewhat lacking If you project java add in Kotlin getting get this error

  • Tools Tab Kotlin and Configure Kotlin
  • (Select Android with Gradle) after select with Modules

Project build.gradle add

ext.kotlin_version = ‘1.3.21’
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

Apps build.gradle

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-android'

Kotlin

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

Referance : https://medium.com/mindorks/enabling-kotlin-support-for-a-current-only-java-android-project-98c75e04384a

Upvotes: 7

Rishabh Sagar
Rishabh Sagar

Reputation: 4117

If you are facing this issue while using Kotlin and have

kotlin.incremental=true
kapt.incremental.apt=true

in the gradle.properties, then you need to remove this temporarily to fix the build.

After the successful build, you can again add these properties to speed up the build time while using Kotlin.

Reference: https://stackoverflow.com/a/58951797/3948854

Upvotes: 0

O Thạnh Ldt
O Thạnh Ldt

Reputation: 1223

In build-gradle app, add this row:

implementation project(":your_name_library_here")

Upvotes: 1

CodeToLife
CodeToLife

Reputation: 4141

For Android Studio 2.2.2

Yes, in library module, it can't use the apply plugin: com.android.application statement in the module definition, yes, use apply plugin: com.android.library instead. (still in lib module)

But then you have to do the following:

  1. Expose the same SDK versions in Gradle files for both modules.
  2. Right click on your projects "app" module folder and click on -> open module settings
  3. Click on the "dependencies" tab
  4. Click on the + sign to add a new dependency and select "Module Dependency"
  5. Look for the library you need and add it.

Also while naming your lib module avoid capitals.

Upvotes: 17

Scott Barta
Scott Barta

Reputation: 80010

If you have a library module, it can't use the apply plugin: 'com.android.application' statement in the module definition, or the build will silently fail as you're seeing. use apply plugin: 'com.android.library' instead.

A bug has been filed to request that the build system fail loudly instead of silently when this happens: https://code.google.com/p/android/issues/detail?id=76725

Upvotes: 15

Related Questions