Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 384
Robertas Setkus Avatar asked Oct 17 '14 10:10

Robertas Setkus


4 Answers

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.

like image 129
CodeToLife Avatar answered Nov 15 '22 23:11

CodeToLife


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

like image 35
Scott Barta Avatar answered Nov 16 '22 01:11

Scott Barta


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

like image 6
Mehmet Agah Balbay Avatar answered Nov 16 '22 01:11

Mehmet Agah Balbay


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 this error is referring 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. And on Android Studio, create manually each filename as previous, and copy the code inside from the old file. This will defiantly solve this error.

like image 2
Miguel Silva Avatar answered Nov 16 '22 01:11

Miguel Silva