使用库构建Android gradle,找不到符号

时间:2015-10-28 13:30:14

标签: android gradle android-library

我正在尝试构建一个使用我也创建的Android库的Android应用程序。项目结构如下:

/mainProject
    |- /myLibrary
        |- build.gradle
        |- (Java Sources and files)
    |- /app
        |- build.gradle
        |- (Java Sources and files)
    |- settings.gradle
    |- build.gradle

mainProject / settings.gradle包含:

include ':app', ':myLibrary'

mainProject / build.gradle包含:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

mainProject / myLibrary / build.gradle如下所示:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        debug{
            buildConfigField "String", "LIBRARY_ENDPOINT", "\"https://dev-library.company.com/\""
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField "String", "LIBRARY_ENDPOINT", "\"https://library.company.com/\""
        }
    }
}

repositories {
    jcenter()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.+'
    compile 'com.github.satyan:sugar:1.3'
    testCompile 'junit:junit:4.12'
    compile 'commons-io:commons-io:2.4'
    androidTestCompile 'junit:junit:4.12'
}

mainProject / app / build.gradle文件包含:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.company.app"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            buildConfigField "String", "APP_ENDPOINT", "\"https://dev-app.company.com/\""
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            buildConfigField "String", "APP_ENDPOINT", "\"https://app.company.com/\""
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:21.+'
    compile 'com.google.android.gms:play-services-location:8.1.0'
    compile 'commons-io:commons-io:2.4'
    compile 'com.joanzapata.pdfview:android-pdfview:1.0.4@aar'
    compile 'com.github.clans:fab:1.6.1'
    compile 'joda-time:joda-time:2.8.2'
    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile project(':myLibrary')
}

我最初在IDE中遇到错误,但添加最终的编译项目行清除了这些错误,因此IDE根本不显示任何错误。但是,当我运行gradle构建时,我看到了:

:app:compileDebugJavaWithJavac
/AndroidStudioProjects/mainProject/app/src/main/java/com/company/app/MainActivity.java:17: error: cannot find symbol
import com.company.mylibrary.LibraryClass;
                                    ^
  symbol:   class LibraryClass
  location: package com.company.mylibrary
/AndroidStudioProjects/mainProject/app/src/main/java/com/company/app/MainActivity.java:29: error: cannot find symbol
        LibraryClass.setup("562e7d58628ee16894db98df", getApplicationContext());
        ^
  symbol:   variable LibraryClass
  location: class MainActivity
2 errors

 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 8.143 secs

我很困惑,因为IDE没有显示任何错误,我相信我告诉gradle应用程序项目在编译时确实需要myLibrary项目。我在这里找到的答案似乎都没有同样的问题。

有人看到我做错了吗?

1 个答案:

答案 0 :(得分:7)

终于弄明白了这个问题。我的图书馆正在使用proguard,并且出于某种原因,这会让事情搞得一团糟。我从库项目中的build.gradle中删除了minify和proguard行,但它确实有效。