Android Studio library “error: package does not ex

2019-02-07 17:52发布

问题:

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.

回答1:

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



回答2:

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.



回答3:

In build-gradle app, add this row:

implementation project(":your_name_library_here")