简体   繁体   English

从kotlin引用时,Java文件的未解析引用具有不同的风格

[英]Unresolved reference for Java file in a different flavor when referencing from kotlin

Problem: 问题:

This question has likely been answered implicitly from all the other similar questions but I can't seem to get it to work. 这个问题很可能已经从所有其他类似的问题中得到了回答,但我似乎无法让它发挥作用。

  • If I reference a particular java file in another flavor (different source set) from a java file in the main source set it works. 如果我从主源集中的java文件引用另一种风格(不同的源集)中的特定java文件,它就可以工作。

  • 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"). 如果我尝试从主源集中的Kotlin文件引用同一个文件,则会抱怨它无法看到它(“未解析的引用”)。

  • If I move the same file to the main source set it works (so it's not the file itself). 如果我将相同的文件移动到主源设置它是有效的(所以它不是文件本身)。

  • 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. 如果我将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. 根据我的谷歌搜索它似乎与Gradle中的源集相关,但我不想在Java文件夹旁边有一个单独的Kotlin文件夹,所以我不确定我是否需要这个。 Also, another project where I have both Java and Kotlin doesn't have this configured and works fine. 另外,我同时拥有Java和Kotlin的另一个项目没有配置并且工作正常。

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). 我正在使用带有flavorDimensions的productFlavors,并且有一个名为“dimension1Dimension2”的源集(这是我试图引用的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. 这个项目主要是Java,所以我刚刚开始添加Kotlin。 I can't see any differences with my other project that is mostly Kotlin and some Java. 我看不出与我的其他项目有什么不同,主要是Kotlin和一些Java。

One other thing that is weird is that it does work for one of the three flavors. 另一件奇怪的事情是它适用于三种口味中的一种。 There are two types of dimension1 and three types of dimension2. 维度1有两种类型,维度2有三种类型。 Just one of the dimension2 types works. 只有其中一个维度类型有效。

Maybe code is clearer: 也许代码更清晰:

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"
        }
    }

Setup: 建立:

  • Kotlin version: 1.2.21 Kotlin版本:1.2.21
  • Gradle version: 3.0.1 Gradle版本:3.0.1
  • Plugins: 插件:
    • apply plugin: 'kotlin-android' apply plugin:'kotlin-android'
    • apply plugin: 'kotlin-kapt' 申请插件:'kotlin-kapt'
    • apply plugin: 'kotlin-android-extensions' apply plugin:'kotlin-android-extensions'
  • Project level dependencies: 项目级依赖项:
    • classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath“org.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version”
  • App level dependencies: 应用程序级依赖项:
    • implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 实现“org.jetbrains.kotlin:kotlin-stdlib-jdk8:$ kotlin_version”
  • Using Java 8 in project settings 在项目设置中使用Java 8

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. 所以我遇到了同样的问题,因为我们的应用程序是一个生产应用程序,我不能随时改变Kotlin的所有内容,我们也没有时间立即改用Kotlin - 遗留代码现在应该保留Java最小。

I reported a bug to Google regarding this: https://issuetracker.google.com/issues/110100405 我向Google报告了一个错误: https//issuetracker.google.com/issues/110100405

And lo and behold, a Google engineer actually answered my bug report quite fast :-) 而且,一位Google工程师实际上非常快地回答了我的错误报告:-)

If you don't want to read the conversation we had, I'll make it short: 如果你不想阅读我们的对话,我会简短地说:

TL;DR version: TL; DR版本:

The reason this doesn't work with Kotlin is because your packages are wrongly set up. 这与Kotlin无法合作的原因是因为您的软件包设置错误。 I will bet you, that in some of your flavors you've done the same mistake as me and created directories like com.example.mypackage instead of creating the parent directory called com and then a sub-directory with the name example and then a sub-sub-directory called mypackage . 我敢打赌,在你的某些风格中,你和我一样犯了同样的错误并创建了com.example.mypackage这样的directories而不是创建名为com的父目录,然后创建一个名为example的子目录,然后是子目录名为mypackage

So if your folders look com.example.mypackage change them to com with sub-directories and all should be fine. 因此,如果您的文件夹看起来像com.example.mypackage将它们更改为com与子目录,所有应该没问题。

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. 另一个可能最简单的解决方案是在不同的版本中创建名为com.example.mypackage包,但只有当您选择了要为其创建包的样式时才能这样做。

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. 至于为什么,这不会产生Java的问题,我不知道,谷歌人也没有,但它可能是一个IntelliJ问题,我将向JetBrains报告,除非他们已经意识到它。

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编译器只是吐出一个模糊的错误,说明Unresolved reference ,但这是另一天的谈话和另一个错误报告或增强。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM