Android Studio 库项目引用未解析的符号

标签 android gradle

我终于将我的 eclipse android 项目迁移到 Android Studio。我创建了一个包含两个模块的项目。一个模块是一个库项目。另一个是包含库模块的普通 Activity Project。

Project/
   app/     (Activity Project)
   br_lib/  (Library Project)

在应用程序的“模块设置”中,我添加了一个 dependency到 lib 模块。 lib 模块项目构建正常,但每次我引用其中一个 lib 模块类时,我的应用程序模块构建失败:

import org.apache.http.client.HttpClient;
import org.cddevlib.breathe.setup.Utils <--  // lib refrence;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Utils.convertName(""); // <--- lib module static class;
    }
}

错误:

Error:(9, 34) error: cannot find symbol class Utils
Error:(25, 9) error: cannot find symbol variable Utils

怎么了?我错过了什么?谢谢

这是应用模块 gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

defaultConfig {
    applicationId "pro.breathe.cddev.org.br_gold"
    minSdkVersion 22
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.pro'
        }
    }
}

dependencies {
    compile project(':br_lib')
    compile fileTree(dir: 'libs', include: ['*.jar']) 
}

这是 lib 模块 gradle:

    apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0"

defaultConfig {
    minSdkVersion 9
    targetSdkVersion 9
}

dependencies {
    compile 'com.android.support:support-v4:22.2.1'
    compile 'com.android.support:design:22.2.1'
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:cardview-v7:22.2.1'
    compile 'com.android.support:gridlayout-v7:22.2.1'
    compile 'com.android.support:recyclerview-v7:22.2.1'
    compile 'com.google.android.gms:play-services:7.8.0'
}

buildTypes {
    release {
        minifyEnabled true
        proguardFiles 'proguard.cfg'
      }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

这是项目 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
}
}

allprojects {
repositories {
    jcenter()
}
}

设置 gradle 是:

include ':app', ':br_lib'

最佳答案

今天在一个有 5 个模块(其中 2 个是 com.android.library 模块)的项目中遇到了同样的问题。在对我的 build.gradle 文件进行大量试验和错误更改后,解决方案最终与在库模块的 gradle 文件中包含 publishNonDefault true 属性有关。

我发现这篇文章非常有帮助: StackOverflow - Product Flavors and Library Publication

我的应用程序 gradle 文件最终看起来像(故意省略代码):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.2'

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

    defaultConfig {
    }

    productFlavors {
        funapp {
            applicationId "com.funapp.package"
        }
        funappbeta {
            applicationId "com.funapp.beta"
        }
    }

    signingConfigs {
        googleplay {
        }
    }

    buildTypes {
        debug {
            minifyEnabled false
            debuggable true
            signingConfig signingConfigs.debug
        }
        release {
            debuggable false
            signingConfig signingConfigs.googleplay
        }
    }
}

configurations {
    funappDebugCompile
    funappReleaseCompile
    funappbetaDebugCompile
    funappbetaReleaseCompile

    // Establish debug compile dependencies for build variants
    [funappDebugCompile, funappbetaDebugCompile].each {
        it.extendsFrom commonFunSdkDebugCompile
    }
    // Establish release compile dependencies for build variants
    [funappReleaseCompile, funappbetaReleaseCompile].each {
        it.extendsFrom commonFunSdkReleaseCompile
    }
    // Common Android and JUnit testing dependencies (JUnit tests can be run
    // using only a JVM; Android tests require a device or emulator)
    [androidTestCompile, testCompile].each {
        it.extendsFrom commonTestCompile
    }
}

dependencies {
    commonFunSdkDebugCompile project   (path: ':fun-sdk', configuration: 'debug')
    commonFunSdkReleaseCompile project (path: ':fun-sdk', configuration: 'release')

我的库 gradle 文件如下所示:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.2'

    publishNonDefault true // <-- This happened to be the key component
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    defaultConfig {
    }

    buildTypes {
        debug {
        }
        release {
        }
    }
}

configurations {
    debug
    release
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

关于Android Studio 库项目引用未解析的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32346574/

相关文章:

android - 这个 ListAdapter 编辑文本有什么问题?

android - 如何配置 LongClick 的响应时间?

android - 如何解决 Android 中的重复类错误

java - Gradle 构建配置的签名者

java - 在多项目构建中升级新配置

android-TextView setText in Html.fromHtml 显示图片和文字

java - 删除广告时调整布局大小

maven - Gradle无法识别Cumulocity Microservice SDK软件包

android - 为什么当前版本的Gradle插件不支持按需配置?

gradle - Gradle:发布带有项目文件/依赖项子集的 Artifactory