Issue
How may the compiled Jar file for an Android app built with Android Studio be accessed?
Use case - MockK for JUnit 5 local unit testing in Kotlin, viewing the output of Kotlin compiled to Java.
Android Studio version - 3.5
@oleksiyp, outlines here, how top-level functions are compiled into Java in this Medium series and in this StackOverflow post.
Examining how the Kotlin files are compiled into Java will allow for mocking more advanced top-level functions where the output is not as straightforward as the below.
For instance, the following example:
// Code.kt source file package pkg fun lowercase(str: String): String {...}
translates to:
package pkg; class CodeKt { public static String lowercase(String str) {...} }
Solution
oleksiyp notes in this GitHub issue that the Jar may be found under the build directory in Android Studio.
- In the Android Studio navigation window switch the view from Android to Project mode.
- Go to directory app > build > outputs > apk > debug > app-debug.apk
- In app-debug.apk if there are multiple classes.dex files such as classes.dex2 and classes.dex3, find the one with your app's classes inside of it (ie - classes.dex2 > app > yourAppName.
- Under each class name it shows a list of the methods created in Java from Kotlin. 🧙♂
Using the Coinverse Open App as a sample here are the methods generated for the ContentViewModel.
Answered By - Adam Hurwitz