问题描述
我已经创建了 Android 库作为 Android Studio 模块.添加为我的根模块的依赖项.编码时,我可以从库包中导入任何类,但是当我尝试运行应用程序时,出现错误 package some.mylibrary.project 不存在
.
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 根模块
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 库模块
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'
附言不得不提一下,该项目是从Eclipse IDE导出的,所以项目结构与默认不同.
P.S. I have to mention that the project was exported from Eclipse IDE so the project structure is different from default one.
推荐答案
For Android Studio 2.2.2
是的,在库模块中,不能使用模块定义中的apply plugin: com.android.application
语句,是的,使用apply plugin: com.android.library
代替.(仍在 lib 模块中)
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:
- 在 Gradle 文件中为两个模块公开相同的 SDK 版本.
- 右键单击您的项目app"模块文件夹,然后单击 -> 打开模块设置
- 点击依赖项"标签
- 点击 + 号添加新的依赖项并选择模块依赖项"
- 查找您需要的库并添加它.
同时在命名你的 lib 模块时避免大写.
Also while naming your lib module avoid capitals.
这篇关于Android Studio 库“错误:包不存在"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!