Unresolved refarence may occur at build time when referencing Java class from Kotlin

A story that it is better not to use symbolic links under app / src

Assumption: Symblic Link was used for code reference between Flavors

When you're developing an app in Android Studio and need to separate a third-party SDK library for production and sandboxing, I used Flovor to divide the build, and sandbox also uses the same code of product, so I used symbolic links to refer to the code.

app/build.gradle


android {

 (abridgement)

 Flavors {
    product {}
    sandbox {}
  }

 (abridgement)

}

app/src


% ln -s product sandbox
% ls
  product
  sandbox -> product

If you do like this, it will work normally on Android Studio, and the project tree will also be displayed properly with the flavor name.

However, when I refer to the Java class that is the destination of Symbolic Link in Kotlin, an error occurs at build time.

So, after a while, It was a project written in Java, but when adding functions, I decided to write a new part in Kotlin.

Therefore, it is also necessary to refer to Java classes from Kotlin, When the flavor is specified as sandbox, the Java class of the Symbolic link destination may be referred to. It should have been referenced and complemented normally on Android Studio ...

error


Unresolved reference: packagehoge
Unresolved reference: classmoge

I get an error like this when building.

There is no problem if the code structure is Java only or Kotlin only. It seems to happen when I try to reference a Java class from Kotlin.

Solution: Use sourceSets properly

Let's set it properly without wearing sideways

app/build.gradle


android {

 (abridgement)

 Flavors {
    product {}
    sandbox {}
  }

 (abridgement)

  sourceSets {
    sandbox {
      sandbox.manifest.srcFile 'src/product/AndroidManifest.xml'
      sandbox.assets.srcDirs += 'src/product/assets'
      sandbox.java.srcDirs += 'src/product/java'
      sandbox.res.srcDirs += 'src/product/res'
    }
  }

 (abridgement)

}

By making the above settings, the reference using Symbolic Link has disappeared, and the build can pass safely.

Recommended Posts

Unresolved refarence may occur at build time when referencing Java class from Kotlin
Behavior when calling Java variadic methods from Scala / Kotlin / Java
Take a look at Kotlin from an Effective Java perspective