我查了一下这个问题: setGroup() is undefined for the type NotificationCompat.Builder?
但它没有帮助。
我有一个从eclipse导入的项目。在跑步时我得到错误:
错误:找不到符号 .setGroup( “基团”) ^ symbol:方法setGroup(String)
在Google上搜索我发现有人建议更新android-support库。虽然导入项目我使用了选项来保留项目中的jar文件,而不是将它们转换为android studio.Hence中的依赖项我假设相同的支持jar文件正在从项目的libs文件夹中使用。相同的代码在eclipse上运行良好。在检查时,我看到两个项目的支持jar文件完全相同。那么为什么一个方法在项目中未定义但在另一个方法中可见。 Android Studio是否有可能从其自己的jar文件中替换支持jar文件?
代码:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(text)
.setContentText(text)
.setStyle(inboxStyle)
.setNumber(stack.size())
.setSmallIcon(R.drawable.launcher)
.setGroup("group")
.setGroupSummary(true);
编辑:
我根据以下建议更改了依赖关系,以使用动态支持库而不是jar。但仍然看到相同的错误,我的build.gradle现在看起来像这样。
compile project(':facebookSDK')
compile project(':googleplayservices_lib')
compile 'com.android.support:support-v13:25.1.0'
compile files('libs/activation.jar')
compile files('libs/additionnal.jar')
// compile files('libs/android-support-v13.jar')
compile files('libs/eat.jar')
compile files('libs/gcm-server-1.0.2.jar')
compile files('libs/google-api-client-1.10.3-beta.jar')
compile files('libs/google-api-client-android2-1.10.3-beta.jar')
compile files('libs/google-http-client-1.10.3-beta.jar')
compile files('libs/google-http-client-android2-1.10.3-beta.jar')
compile files('libs/google-oauth-client-1.10.1-beta.jar')
compile files('libs/gson-2.1.jar')
compile files('libs/jackson-core-asl-1.9.4.jar')
compile files('libs/json-simple-1.1.1.jar')
compile files('libs/jsr305-1.3.9.jar')
compile files('libs/mail.jar')
EDIT2:
根据以下答案,我在v13旁边添加了v4库,但仍然存在错误。还有更多线索吗?我当前的依赖项看起来像
dependencies {
compile project(':facebookSDK')
compile project(':googleplayservices_lib')
compile files('libs/activation.jar')
compile files('libs/additionnal.jar')
compile files('libs/android-support-v13.jar')
compile 'com.android.support:support-v4:25.1.0'
compile files('libs/eat.jar')
compile files('libs/gcm-server-1.0.2.jar')
compile files('libs/google-api-client-1.10.3-beta.jar')
compile files('libs/google-api-client-android2-1.10.3-beta.jar')
compile files('libs/google-http-client-1.10.3-beta.jar')
compile files('libs/google-http-client-android2-1.10.3-beta.jar')
compile files('libs/google-oauth-client-1.10.1-beta.jar')
compile files('libs/gson-2.1.jar')
compile files('libs/jackson-core-asl-1.9.4.jar')
compile files('libs/json-simple-1.1.1.jar')
compile files('libs/jsr305-1.3.9.jar')
compile files('libs/mail.jar')
}
edit4:
我也尝试过这种组合,似乎没有任何效果:(
// compile files('libs/android-support-v13.jar')
compile 'com.android.support:support-v13:25.1.0'
compile 'com.android.support:support-v4:25.1.0'
我也注意到一件事,即使我从依赖项中删除了v4,导入NotificationCompat类时也没有出现编译错误。怎么可能呢?是不是它正在从其他模块中获取一些其他库?
编辑:5
根据我与Martin的讨论,我想分享一些有助于我们所有人的见解。根据Android Studio,它会选择第一个库(或任何库,我不确定) 从任何模块开始在模块中使用它。在如此多的层面上这是错误的!我会解释你如何,请在任何地方纠正我,因为这会帮助我们所有人。
导致许可问题:在这里,我在项目中使用供应商库模块(在本例中为Facebbok SDK),它正在使用一些库用于它自己的目的(在这种情况下为v4 lib)。 现在作为开发人员,我不知道我的供应商库使用该第三方库的许可条款是什么。但是,我能够在我的模块中使用相同的库,即使我没有在我自己的依赖中定义它,也违反了许可协议。
引起混淆:想象一下,我甚至不知道我正在使用第三方库中的函数。我看到我没有定义这个库我的build.gradle。但在内部我可能已经使用了一个来自随机库的函数。编辑器当然会静默导入该方法的类,开发人员不会知道它调用哪个库。 这正是这里发生的事情,因为我不知道这个方法来自v4库,因为我没有在build.gradle文件中导入它!
无法更新到最新版本:这是我在这里遇到的另一个问题。经过以下讨论,我知道AS 已使用facebooks库,但现在我无法更新库供我使用。为什么?因为facebook使用了旧图书馆 在发展的时候。我无法更新facebook的库(技术上我可以,但我不应该),我不想更新faceboook sdk由于一些其他约束。此外,没有理由更新facbook sdk,因为我的所有当前需求都是从这个sdk实现的。
所有这一切只是因为AS认为它比我更了解我应该在自己的项目中使用哪个库。
答案 0 :(得分:1)
好吧,根据您的编辑,您没有遵循我的完整建议,因此错误现在更加明显:NotificationCompat.Builder未在支持v13.app中实现。*
您需要使用现代支持v4版本,或者您可以使用从v4扩展的支持v7,这取决于您当前或将来的需求以及其他库依赖项。但是使用其中一个必须使setGroup()可用,因为两个库都提供NotificationCompat.Bulder帮助器类。
https://developer.android.com/reference/android/support/v7/app/NotificationCompat.html
关于准确的版本号,允许您跟进版本的官方文档(及其各自的更改)在此处:
https://developer.android.com/topic/libraries/support-library/revisions.html
但根据我的经验,最好定期检查build.gradle依赖项,因为每次从Google下载和升级SDK包时,Android Studio都会突出显示旧的Google依赖项,提供要升级的新版本。