【问题标题】:Android gradle build with library, cannot find symbol带有库的Android gradle构建,找不到符号
【发布时间】:2016-01-28 07:19:47
【问题描述】:

我正在尝试构建一个使用我也创建的 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 build 时,我看到了:

: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 app 项目在编译时确实需要 myLibrary 项目。我在这里找到的答案似乎都没有相同的问题。

有人看到我做错了吗?

【问题讨论】:

    标签: android gradle android-library


    【解决方案1】:

    终于解决了问题。我的图书馆正在使用 proguard,出于某种原因,这把事情搞砸了。我从库项目中的 build.gradle 中删除了 minify 和 proguard 行,它起作用了。

    【讨论】:

    • 不错的发现。我遇到了完全相同的问题,并使用 proguard 规则修复了它:-keep public class **.BuildConfig { *; }
    猜你喜欢
    • 1970-01-01
    • 2013-05-28
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 2019-11-01
    • 2020-03-06
    相关资源
    最近更新 更多