Android Studio 未在编译时检测到支持库

Posted

技术标签:

【中文标题】Android Studio 未在编译时检测到支持库【英文标题】:Android Studio not detecting support libraries on Compilation 【发布时间】:2015-03-06 06:36:15 【问题描述】:

由于 android Studio 将成为 Android 开发的默认 IDE,我决定将现有项目迁移到 Android-studio。项目结构似乎不同,我的项目中的文件夹层次结构如下:

Complete Project
 ->.idea
 -> build
 -> Facebook SDK
 -> MainProject
 -> ... (Other Libraries)
 build.gradle
 local.properties
 settings.gradle
 ...
External Libraries
 -> Android API 8 Platform
 -> Android API 18 Platform
 -> Android API 19 Platform
 -> 1.7 Java
 -> support-v4-19.1.0

我的 MainProject 有一个 libs 文件夹,其中包含项目中使用的不同 jar。令人惊讶的是,它不包含我的 eclipse 项目中存在的 android-support-v4 jar。因此,似乎根目录下的外部库文件夹必须处理它。

但导入后,当我尝试编译项目时,开始为某些与 android 支持库相关的类抛出“未找到符号错误”。

例如:Android Studio 中的自动完成功能为我提供了来自 android.support.v4.app.NotificationCompat 的 NotificaitonCompat 建议,但是当我尝试编译我的项目模块时它显示

错误:(17, 30) 错误:找不到符号类 NotificationCompat 错误:任务 ':app:compileDebugJava' 执行失败。> 编译失败;有关详细信息,请参阅编译器错误输出。

对于相同的支持库,这也发生在许多其他类中。我尝试插入一个 jar 并在 MainProject 的 build.gradle 中进行了相同的更改,但错误仍然存​​在。

我什至尝试重新启动并再次构建项目,但没有任何改变。

编辑: 我在 MainProject 中附加 Gradle 文件

MainProject 模块中的build.gradle

apply plugin: 'com.android.application'

android 
compileSdkVersion 19
buildToolsVersion "21.1.2"

defaultConfig 
    applicationId "package.app"
    minSdkVersion 8
    targetSdkVersion 19


buildTypes 
    release 
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    



configurations 
all*.exclude group: 'com.android.support', module: 'support-v4'


dependencies 
compile project(':facebookSDK')
compile project(':library')
compile project(':volley')
compile 'com.google.android.gms:play-services:+'
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
compile 'com.android.support:support-v4:19.1.0'
compile files('libs/FlurryAnalytics_3.3.3.jar')
compile files('libs/universal-image-loader-1.8.4.jar')
....

【问题讨论】:

你在 Build.gradle (Module App) 中添加依赖了吗? 当我将项目从 Eclipse 导入到 android-studio 时,默认情况下它们就在那里。 去 Android SDK\extras\android\m2repository\com\android\support\support-v4 看看你有哪个版本的支持库? 我有 18.0.0、19.0.1、19.1.0、21.0.0、21.0.2 等的支持库 【参考方案1】:

构建文件的这一部分:

configurations 
all*.exclude group: 'com.android.support', module: 'support-v4'

告诉构建系统忽略 support-v4,这就是它不编译的原因。删除它。

在您的构建文件中,您有这个,这是包含支持的正确方法:

compile 'com.android.support:support-v4:19.1.0'

如果您的任何模块的 libs 目录中有支持库 jar 文件,请将其删除并确保以这种方式引用它 - 如果您将库作为 jar 包含,您可能会遇到多次包含 jar 的问题,这将导致 dex 错误。

【讨论】:

因为配置 ... 在依赖项 ... 之前,不应该解决库中的冲突。如果我删除配置,它会抛出此处提到的错误 (***.com/questions/27855722/…) 正如我在回答中所说,您应该检查并确保您没有多次链接支持库 jar。 “多个 dex 文件定义”错误就是因为这个。【参考方案2】:

如果您在“libs”目录中有 jar 文件,您可以在 build.gradle 文件中指定它:

dependencies
    compile fileTree(dir: 'libs', include: ['*.jar']
    compile 'com.android.support:support-v4:21.0.3'  //to automatically add up-to-date support lib

希望这会有所帮助!

【讨论】:

以上是关于Android Studio 未在编译时检测到支持库的主要内容,如果未能解决你的问题,请参考以下文章

基于Android Studio的内存泄漏检测与解决全攻略

Flutter App 未在 Android Studio 中运行

使用 Android Studio 检测内存泄漏与解决内存泄漏问题

Android 模拟器未在 Visual Studio 中显示

NDK 未在 android studio 中配置问题

print() 语句未在 Android Studio 中的颤动 iOS 应用程序中打印到控制台