Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams All of the answers on this topic are uniform in the sentence like "run the JVM with the -ea option", however I cannot find how to actually do that in Android Studio. Googling "kotlin android studio set jvm option" or similar yields no solution. EDIT: After a while of question rephrasing I found the solution, if anyone will be wondering the same Where to add compiler options like -ea in IntelliJ IDEA? Bojan P. Jun 21, 2019 at 9:30

In Kotlin , unlike Java , assertions can be enabled/disabled only on top-level, i.e. by specifying -ea option to JVM.

Behind the scenes, assert() in Kotlin is a function, defined in AssertionsJVM.kt file. ENABLED variable in the _Assertions object is used to determine if assertions are enabled.

@PublishedApi
internal object _Assertions {
    @JvmField @PublishedApi
    internal val ENABLED: Boolean = javaClass.desiredAssertionStatus()

The ENABLED variable is assign to true if assertions enabled for the _Assertions class.

As a consequence, to turn on asserts in Kotlin they have to be enabled in JVM for

  • all classes -ea
  • or one particular class -ea:kotlin._Assertions
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.