问题描述
这个问题可能已经从所有其他类似的问题中隐式回答了,但我似乎无法使其正常工作.
This question has likely been answered implicitly from all the other similar questions but I can't seem to get it to work.
-
如果我从主源集中的Java文件中引用了另一种口味(不同源集)的特定Java文件,则它起作用.
If I reference a particular java file in another flavor (different source set) from a java file in the main source set it works.
如果我尝试从主要来源集中的Kotlin文件引用同一文件,它会抱怨看不到它(未解决的引用").
If I try to reference that same file from a Kotlin file in the main source set it complains that it can't see it ("Unresolved reference").
如果我将同一文件移到主要源集,它将起作用(因此它不是文件本身).
If I move the same file to the main source set it works (so it's not the file itself).
如果我将Java文件转换为Kotlin,它可以工作(这是我当前的解决方案),但是我想知道为什么它不起作用,因为它并不总是那么容易转换,并且在没有它的情况下应该可以工作转换.
If I convert the Java file to Kotlin it works (and this is my current solution) but I'd like to know why it doesn't work since it's not always so easy to convert and it should work without converting.
基于我的谷歌搜索,似乎它与Gradle中的源集有关,但是我不希望Java文件夹旁边有单独的Kotlin文件夹,因此我不确定我是否需要此文件夹.另外,在我同时拥有Java和Kotlin的另一个项目中,没有对此进行配置并且可以正常工作.
Based on my Googling it seems it's related to the source sets in Gradle but I don't want a separate Kotlin folder alongside the Java folder so I'm not sure I need this. Also, another project where I have both Java and Kotlin doesn't have this configured and works fine.
我正在将productFlavors与flavourDimensions一起使用,并且有一个名为"dimension1Dimension2"的源集(这是我要引用的Java文件所在的位置).
I'm using productFlavors with flavorDimensions and have a source set called "dimension1Dimension2" (that's where the Java file I'm trying to reference is located).
看看下面的设置,我可能做错了什么或丢失了什么?这个项目主要是Java,因此我刚刚开始向其中添加Kotlin.我看不到与其他项目(主要是Kotlin和某些Java)有任何区别.
Looking at my setup below, what am I possibly doing wrong or missing? This project is mostly Java so I'm just starting to add Kotlin to it. I can't see any differences with my other project that is mostly Kotlin and some Java.
另一件事很奇怪,那就是它确实适用于三种口味中的一种.有两种类型的Dimension1和三种类型的Dimension2.只有Dimension2类型之一有效.
也许代码更清晰:
flavorDimensions "product", "mode"
productFlavors {
mock { // only this one works (e.g. <company_name>Mock is the source set)
applicationIdSuffix = ".mock"
dimension "mode"
}
dev {
applicationIdSuffix = ".dev"
dimension "mode"
}
prod {
dimension "mode"
}
demo {
applicationIdSuffix = ".demo"
dimension "product"
buildConfigField "String", "UPDATE_DIRECTORY", "\"/release/\""
buildConfigField "boolean", "SHOW_STREAM_STATUS", "false"
}
<company_name> {
applicationIdSuffix = ".<company_name>"
dimension "product"
}
}
设置:
- 科特林版本:1.2.21
- 版本版本:3.0.1
- 插件:
- 应用插件:"kotlin-android"
- 应用插件:"kotlin-kapt"
- 应用插件:"kotlin-android-extensions"
- Kotlin version: 1.2.21
- Gradle version: 3.0.1
- Plugins:
- apply plugin: 'kotlin-android'
- apply plugin: 'kotlin-kapt'
- apply plugin: 'kotlin-android-extensions'
- classpath"org.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version"
- 实现"org.jetbrains.kotlin:kotlin-stdlib-jdk8:$ kotlin_version"
推荐答案
所以我遇到了同样的问题,由于我们的应用程序是生产应用程序,所以我不能随便将所有内容都更改为Kotlin,我们负担不起是时候立即更改为Kotlin了-遗留代码至少应该暂时保留在Java中.
So I had the same issue and I couldn't just change everything on the fly to Kotlin as our app is a production app and we couldn't afford the time to change to Kotlin right away - the legacy code should stay Java for now at least.
我向Google报告了一个与此有关的错误: https://issuetracker.google.com/issues/110100405
I reported a bug to Google regarding this: https://issuetracker.google.com/issues/110100405
瞧,一位Google工程师实际上很快回答了我的错误报告:-)
And lo and behold, a Google engineer actually answered my bug report quite fast :-)
如果您不想阅读我们的谈话,我将简短介绍:
If you don't want to read the conversation we had, I'll make it short:
TL; DR版本:
不适用于Kotlin的原因是因为您的软件包设置错误.我敢打赌,在某些情况下,您犯了与我相同的错误,并创建了
directories
之类的directories
,而不是创建名为com
的父目录,然后创建了名为,然后是名为mypackage
的子目录.The reason this doesn't work with Kotlin is because your packages are wrongly set up.I will bet you, that in some of your flavors you've done the same mistake as me and created
directories
likecom.example.mypackage
instead of creating the parent directory calledcom
and then a sub-directory with the nameexample
and then a sub-sub-directory calledmypackage
.因此,如果您的文件夹看上去
com.example.mypackage
,请将其更改为带有子目录的com
,则一切都很好.So if your folders look
com.example.mypackage
change them tocom
with sub-directories and all should be fine.另一种解决方案,可能最简单的方法是使用不同的口味创建名称为
com.example.mypackage
的包装,但这只有在选择了要为其创建包装的口味后才能实现.Another solution, which is probably the easiest is to create packages with the name
com.example.mypackage
in the different flavors, but this can only be done, when you've selected the flavor you want to create a package for.关于为什么,这不会在Java中造成问题,我也不知道,Google的人也没有,但这可能是IntelliJ问题,除非他们已经意识到,否则我将向JetBrains报告它.
As to why, this doesn't create issues with Java, I have no idea and the Google guy didn't either, but it's probably an IntelliJ issue, which I'll report to JetBrains unless they're already aware of it.
Kotlin编译器吐出一个含糊不清的错误消息
Unresolved reference
也是一个大问题,但这又是一天的讨论,并且是另一个错误报告或增强功能.It's also a big issue that the Kotlin compiler just spits out a vague error saying
Unresolved reference
, but that's a talk for another day and another bug report or enhancement.这篇关于从kotlin引用时,Java文件的未解析引用具有不同的风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
Setup: