Android studio - 带有库项目的应用程序无法构建
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20117884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android studio - App with library project fails to build
提问by Gyroscope
I'm having massive trouble trying to get my app project to build. I have the main app module and a library project module as shown below:
我在尝试构建我的应用程序项目时遇到了巨大的麻烦。我有主应用程序模块和库项目模块,如下所示:
This is the gradle.build for each of the modules:
这是每个模块的 gradle.build:
Main App:
主应用程序:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion '19.0.0'
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
}
buildTypes {
release {
runProguard true
proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
}
}
productFlavors {
defaultFlavor {
proguardFile 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:support-v13:19.0.+'
compile 'com.google.android.gms:play-services:4.0.+'
compile project(':libraries:datetimepicker')
}
And this one is for the library Project:
这是图书馆项目:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
}
release {
runProguard true
proguardFile 'proguard-rules.txt'
proguardFile getDefaultProguardFile('proguard-android-ptimize.txt')
}
}
dependencies {
compile 'com.android.support:support-v4:19.0.+'
}
Finally, This is the project settings.gradle file.
最后,这是项目 settings.gradle 文件。
include ':App', ':libraries:datetimepicker'
I am able to successfully import packages from the library to my App code and use them, however when I try to compile I get the following:
我能够成功地将包从库导入我的应用程序代码并使用它们,但是当我尝试编译时,我得到以下信息:
Gradle: Execution failed for task ':App:compileDefaultFlavorDebug'.
> Compilation failed; see the compiler error output for details.
E:\blah\blah\MyClass.java
Gradle: error: cannot find symbol class DatePickerDialog
Gradle: error: package DatePickerDialog does not exist
Gradle: error: cannot find symbol class DatePickerDialog
Gradle: error: cannot find symbol class DatePickerDialog
Gradle: error: cannot find symbol variable DatePickerDialog
Gradle: error: method does not override or implement a method from a supertype
I've been trying to fix this for 3 days now and have exhausted almost all of the similar question solutions I could find on here. I'm pretty confident with developing for android, not so confident with gradle and have probably done something obviously wrong.
我已经尝试解决这个问题 3 天了,并且已经用尽了我可以在这里找到的几乎所有类似问题的解决方案。我对为 android 开发非常有信心,对 gradle 不太有信心,并且可能做了一些明显错误的事情。
Some extra info:
一些额外的信息:
- Android Studio v0.3.6
- Android SDK Build-tools rev 19
- Gradle version 1.8
- 安卓工作室 v0.3.6
- Android SDK 构建工具修订版 19
- 摇篮版本 1.8
Any ideas on how to fix this?
有想法该怎么解决这个吗?
回答by Scott Barta
When Gradle builds the library project, it's building the release type even if you're building the debug type for your main app (this is a bug). In your library project, you have Proguard configured for your release build type, and Proguard is obfuscating the symbol names, making them invisible to your app.
当 Gradle 构建库项目时,它会构建发布类型,即使您正在构建主应用程序的调试类型(这是一个错误)。在您的库项目中,您为发布构建类型配置了 Proguard,而 Proguard 正在混淆符号名称,使它们对您的应用不可见。
Since you control the library code, the best thing is to not run Proguard in your library build, and just run it for release builds of your main app. It will obfuscate all code, including the dependencies.
由于您控制库代码,因此最好不要在您的库构建中运行 Proguard,而只在主应用程序的发布构建中运行它。它将混淆所有代码,包括依赖项。
If you really want to obfuscate the library code independently, you'll need to set up the Proguard rules to expose the public symbols of the library, DatePickerDialog
being one.
如果你真的想独立混淆库代码,你需要设置 Proguard 规则来公开库的公共符号,DatePickerDialog
作为一个。
回答by Pascal
Just explicitlytells gradle that your library project must not being minifiedby adding/modifying section
只是明确地告诉 gradle 不能通过添加/修改部分来缩小您的库项目
android/buildTypes/debug
of your library project's build.gradlefile like this (minifyEnabled falseis the key):
像这样的库项目的build.gradle文件(minifyEnabled false是关键):
android {
...
buildTypes {
debug {
debuggable true
minifyEnabled false
}
...
}
...
}
Note:
笔记:
Here, I also instruct explicitlygradle to make my 'debug' build debuggable (debuggable true).
在这里,我还明确指示gradle 使我的“调试”构建可调试(debuggable true)。