Android studio - 带有库项目的应用程序无法构建 [英] Android studio - App with library project fails to build

查看:25
本文介绍了Android studio - 带有库项目的应用程序无法构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试构建我的应用项目时遇到了巨大的麻烦.我有主应用程序模块和库项目模块,如下所示:

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:

这是每个模块的 gradle.build:

This is the gradle.build for each of the modules:

主应用:

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')
}

这是图书馆项目:

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.+'
}

最后,这是项目 settings.gradle 文件.

Finally, This is the project settings.gradle file.

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

我已经尝试解决这个问题 3 天了,并且已经用尽了我可以在这里找到的几乎所有类似问题的解决方案.我对为 android 开发非常有信心,对 gradle 不太有信心,而且可能做了一些明显错误的事情.

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.

一些额外的信息:

  • Android Studio v0.3.6
  • Android SDK 构建工具修订版 19
  • Gradle 1.8 版

关于如何解决这个问题有什么想法吗?

Any ideas on how to fix this?

推荐答案

当 Gradle 构建库项目时,它会构建发布类型,即使您正在构建主应用程序的调试类型(这是一个 错误).在您的库项目中,您为发布构建类型配置了 Proguard,而 Proguard 正在混淆符号名称,使它们对您的应用不可见.

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.

由于您控制库代码,因此最好不要在您的库构建中运行 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 规则来公开库的公共符号,DatePickerDialog 就是其中之一.

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.

这篇关于Android studio - 带有库项目的应用程序无法构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆