Android studio - App with library project fails to

2019-01-13 17:47发布

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:

Project Structure

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

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.

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.

Some extra info:

  • Android Studio v0.3.6
  • Android SDK Build-tools rev 19
  • Gradle version 1.8

Any ideas on how to fix this?

2条回答
时光不老,我们不散
2楼-- · 2019-01-13 18:36

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.

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.

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.

查看更多
甜甜的少女心
3楼-- · 2019-01-13 18:36

Just explicitly tells gradle that your library project must not being minified by adding/modifying section

android/buildTypes/debug

of your library project's build.gradle file like this (minifyEnabled false is the key):

android {
...
    buildTypes {
        debug {
            debuggable true
            minifyEnabled false
        }
...
    }
...
}

Note:

Here, I also instruct explicitly gradle to make my 'debug' build debuggable (debuggable true).

查看更多
登录 后发表回答