Lab 01 — Spot the Kotlin¶
Part of chapter 05 — Kotlin. Sits alongside lab 02 and lab 03.
Objective¶
Given a compiled artefact that mixes Java and Kotlin sources, decide with certainty and without guessing which classes were written in Kotlin, and state the specific evidence for each decision.
By the end you will be able to look at any decompiled class and answer "Kotlin or Java?" in a few
seconds, using five independent signals: the @Metadata annotation, Intrinsics calls, the Companion
nested class, the INSTANCE static field, and DefaultConstructorMarker parameters. You will also
understand which of those signals survive obfuscation and which do not, which matters because the real
APKs you assess will be obfuscated.
Mental Model The compiler cannot hide its own footprints. Every convenience Kotlin gives the developer costs some generated code, and generated code has a fixed shape. You are learning to read a signature, not to guess a style.
Prerequisites¶
- Chapter 04 — Java, specifically how a
.javafile becomes a.classfile. - Chapter 12 — DEX Internals is helpful but not required.
- Comfort running commands in a terminal. If
cd,ls, and paths are unfamiliar, revisit chapter 02 — Linux Basics first.
| Tool | Minimum version | Verify with |
|---|---|---|
| JDK | 17 | java -version |
kotlinc |
1.9 | kotlinc -version |
javac |
17 | javac -version |
| JADX | 1.4 | jadx --version |
| Python 3 | 3.8 | python3 --version |
Estimated time: 45 to 60 minutes.
Setup¶
Create a working directory and two source files — one Java, one Kotlin — that deliberately contain the constructs you are learning to recognise.
# host
mkdir -p ~/kotlin-lab-01/src && cd ~/kotlin-lab-01
Write the Java half first.
// host — save as ~/kotlin-lab-01/src/LegacyStore.java
package com.example.mixed;
import java.util.HashMap;
import java.util.Map;
public class LegacyStore {
private static final LegacyStore SHARED = new LegacyStore();
private final Map<String, String> data = new HashMap<>();
private LegacyStore() { }
public static LegacyStore getShared() { return SHARED; }
public void put(String key, String value) {
if (key == null) { throw new IllegalArgumentException("key"); }
data.put(key, value);
}
public String get(String key) { return data.get(key); }
}
Now the Kotlin half. Every construct here maps to something you will hunt for in the decompiled output.
// host — save as ~/kotlin-lab-01/src/ModernStore.kt
package com.example.mixed
// 1. data class -> generated equals/hashCode/toString/copy/componentN
data class Credential(val user: String, val token: String, val expires: Long = 0L)
// 2. sealed class -> private constructor plus DefaultConstructorMarker
sealed class Result {
data class Ok(val value: String) : Result()
data class Err(val reason: String) : Result()
}
// 3. object declaration -> static INSTANCE field
object Clock {
fun now(): Long = System.currentTimeMillis()
}
// 4. class with a companion object -> nested Companion class
class ModernStore private constructor() {
private val entries = mutableMapOf<String, Credential>()
// 5. non-null parameters -> Intrinsics.checkNotNullParameter
fun save(key: String, cred: Credential) {
entries[key] = cred
}
// 6. default argument -> synthetic load$default bridge
fun load(key: String, fallback: Credential? = null): Result {
val found = entries[key] ?: fallback ?: return Result.Err("missing")
return Result.Ok(found.token)
}
companion object {
private const val VERSION = 3
fun create(): ModernStore = ModernStore()
fun version(): Int = VERSION
}
}
// 7. extension function -> static method on ModernStoreKt taking the receiver first
fun String.masked(): String = if (length <= 4) "****" else take(2) + "****" + takeLast(2)
// 8. top-level main -> class ModernStoreKt
fun main() {
val s = ModernStore.create()
s.save("a", Credential("alice", "abcdef123456"))
println(s.load("a"))
println("abcdef123456".masked())
println(Clock.now())
println(ModernStore.version())
LegacyStore.getShared().put("j", "from java")
println(LegacyStore.getShared().get("j"))
}
Compile both halves into one jar. The Kotlin compiler is told about the Java sources so it can resolve
LegacyStore, and javac is then told about the Kotlin output so the reverse direction resolves too.
# host
mkdir -p out
kotlinc src/ModernStore.kt src/LegacyStore.java -include-runtime -d mixed.jar
If kotlinc refuses the mixed input on your version, compile in two passes instead.
# host — two-pass fallback
kotlinc src/ModernStore.kt src/LegacyStore.java -d out/
javac -cp out/ -d out/ src/LegacyStore.java
cd out && jar cf ../mixed.jar . && cd ..
Confirm the jar runs before you decompile anything. An artefact you have not executed is an artefact you cannot reason about.
# host
java -jar mixed.jar
Steps¶
Step 1 — Inventory the classes before decompiling¶
List the class files inside the jar. Do this first, because the set of class names is itself evidence.
# host
unzip -l mixed.jar | grep -E '^\s+[0-9]+.*com/example/mixed' | awk '{print $4}'
Record which names you did not write. Write down your prediction for what each one is before you look inside. You should see roughly eleven entries for a two-file project, and that ratio alone is a signal.
Step 2 — Decompile with JADX¶
# host
jadx --no-res -d jadx_out mixed.jar
ls jadx_out/sources/com/example/mixed/
JADX will warn about a few synthetic constructs. Warnings are normal here and are not a sign that anything is wrong.
Step 3 — Signal one: the @Metadata annotation¶
This is the strongest and simplest signal. Every class the Kotlin compiler produces carries
kotlin.Metadata; no class javac produces ever does.
# host
grep -rl "kotlin.Metadata" jadx_out/sources/com/example/mixed/
grep -rL "kotlin.Metadata" jadx_out/sources/com/example/mixed/
The first command lists Kotlin-origin classes. The second lists everything else, which should be
LegacyStore.java and nothing more.
Open one hit and look at the annotation itself.
// jadx_out/sources/com/example/mixed/Credential.java — expected shape
@Metadata(d1 = {"\u0000...\n"}, d2 = {"Lcom/example/mixed/Credential;", "", "user", "",
"token", "expires", "", "(Ljava/lang/String;Ljava/lang/String;J)V", ...},
k = 1, mv = {1, 9, 0}, xi = 48)
public final class Credential { ... }
Read the fields as follows.
| Field | Meaning | Values you will see |
|---|---|---|
k |
Kind of class | 1 a real class, 2 a file facade such as ModernStoreKt, 3 a synthetic class such as a lambda, 4 a multifile facade part, 5 a multifile facade |
mv |
Metadata version, tracking the compiler generation | {1, 9, 0} for Kotlin 1.9 |
d1 |
Encoded declaration data | An escaped binary blob; not human-readable |
d2 |
String table for d1 |
Human-readable: original names, JVM signatures, parameter names |
xi |
Extra integer flags | 48 is common on modern releases |
Pentester's Note The
d2array is a free gift. Even when R8 has renamed the class toaand the methods tobandc, thed2strings often still contain the original Kotlin names and signatures, because the default shrinker rules do not rewrite annotation string constants unless explicitly told to. Readd2before you read any code.
Step 4 — Signal two: Intrinsics calls¶
# host
grep -rn "Intrinsics\." jadx_out/sources/com/example/mixed/ | head -30
Open ModernStore.java and find save. The expected shape is this.
// jadx_out/sources/com/example/mixed/ModernStore.java — expected shape
public final void save(String key, Credential cred) {
Intrinsics.checkNotNullParameter(key, "key");
Intrinsics.checkNotNullParameter(cred, "cred");
this.entries.put(key, cred);
}
Note what the second argument gives you: the original Kotlin parameter names, key and cred, as
string constants. Write down every distinct string you find across the whole output. That list is a
partial reconstruction of the application's API surface.
Now look at Credential.equals, which shows the other Intrinsics method you must recognise.
// expected shape — generated by the data class
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof Credential)) return false;
Credential c = (Credential) other;
if (!Intrinsics.areEqual(this.user, c.user)) return false;
if (!Intrinsics.areEqual(this.token, c.token)) return false;
return this.expires == c.expires;
}
Intrinsics.areEqual is a null-safe equals, not reference identity. Reading it as == is a common
beginner mistake.
Step 5 — Signal three: the Companion nested class¶
# host
ls jadx_out/sources/com/example/mixed/ | grep -i companion
grep -rn "Companion" jadx_out/sources/com/example/mixed/ModernStore.java
Expected shape:
// expected shape
public final class ModernStore {
public static final Companion Companion = new Companion(null);
private static final int VERSION = 3;
private final Map<String, Credential> entries = new LinkedHashMap();
private ModernStore() { }
public /* synthetic */ ModernStore(DefaultConstructorMarker $constructor_marker) {
this();
}
public static final /* synthetic */ int access$getVERSION$cp() {
return VERSION;
}
public static final class Companion {
private Companion() { }
public /* synthetic */ Companion(DefaultConstructorMarker $constructor_marker) {
this();
}
public final ModernStore create() {
return new ModernStore(null);
}
public final int version() {
return ModernStore.access$getVERSION$cp();
}
}
}
Three things to internalise here. The companion's members are instance methods on a nested class, not
statics on the outer class, unless @JvmStatic was used. The private constant VERSION is reached
through a synthetic bridge named access$getVERSION$cp, where cp means "companion property". And the
private constructor is paired with a synthetic one taking DefaultConstructorMarker.
Step 6 — Signal four: INSTANCE and the object declaration¶
# host
cat jadx_out/sources/com/example/mixed/Clock.java
// expected shape
public final class Clock {
public static final Clock INSTANCE = new Clock();
private Clock() { }
public final long now() {
return System.currentTimeMillis();
}
}
The recognisable pattern is a final class, a private constructor, and a public static final field of
the class's own type, assigned in the static initialiser. Call sites read Clock.INSTANCE.now().
Be careful: this is also how a hand-written Java singleton looks, which is why LegacyStore in this lab
uses SHARED rather than INSTANCE. On its own the field name is suggestive, not conclusive; combine
it with @Metadata before you commit.
Step 7 — Signal five: DefaultConstructorMarker and $default bridges¶
# host
grep -rn "DefaultConstructorMarker\|\$default" jadx_out/sources/com/example/mixed/
Look at Credential, which has a defaulted third parameter, and at ModernStore.load.
// expected shape — the default-argument bridge on a constructor
public /* synthetic */ Credential(String user, String token, long expires, int i, DefaultConstructorMarker m) {
this(user, token, (i & 4) != 0 ? 0L : expires);
}
// expected shape — the default-argument bridge on a method
public static /* synthetic */ Result load$default(ModernStore store, String key,
Credential fallback, int i, Object obj) {
if (obj != null) {
throw new UnsupportedOperationException(
"Super calls with default arguments not supported in this target, function: load");
}
if ((i & 2) != 0) { fallback = null; }
return store.load(key, fallback);
}
Decode the int at a call site. It is a bitmask where bit n set means "parameter n was omitted".
A call written Credential("alice", "abc") becomes new Credential("alice", "abc", 0L, 4, null),
because bit 2 (value 4) marks the third parameter as defaulted.
Step 8 — Signal six: the file facade and the extension function¶
# host
cat jadx_out/sources/com/example/mixed/ModernStoreKt.java
// expected shape
public final class ModernStoreKt {
public static final String masked(String $this$masked) {
Intrinsics.checkNotNullParameter($this$masked, "$this$masked");
if ($this$masked.length() <= 4) {
return "****";
}
return StringsKt.take($this$masked, 2) + "****" + StringsKt.takeLast($this$masked, 2);
}
public static final void main() { ... }
public static void main(String[] strArr) { main(); }
}
The class name ModernStoreKt exists nowhere in your source. It is the file facade: the compiler's
home for top-level declarations, named after the file with Kt appended. The extension function is a
static method whose first parameter is named $this$masked. This is why searching the String class
for masked finds nothing.
Step 9 — Build the verdict table¶
Produce a table like this for every class in the output, and fill the evidence column with a specific signal rather than an impression.
| Class | Origin | Primary evidence | Secondary evidence |
|---|---|---|---|
LegacyStore |
Java | No @Metadata |
No Intrinsics call anywhere |
Credential |
Kotlin | @Metadata(k = 1) |
component1, copy$default, Intrinsics.areEqual |
Result |
Kotlin | @Metadata(k = 1) |
Abstract, private constructor, DefaultConstructorMarker |
Result$Ok |
Kotlin | @Metadata(k = 1) |
Data-class members, extends Result |
Result$Err |
Kotlin | @Metadata(k = 1) |
Data-class members |
Clock |
Kotlin | @Metadata(k = 1) |
public static final Clock INSTANCE |
ModernStore |
Kotlin | @Metadata(k = 1) |
Companion field, access$getVERSION$cp |
ModernStore$Companion |
Kotlin | @Metadata(k = 1) |
Nested, private constructor, marker overload |
ModernStoreKt |
Kotlin | @Metadata(k = 2) |
Static-only, extension with $this$masked |
Step 10 — Repeat against a real APK¶
Everything above was on a jar you built, where you knew the answer. Now do it blind.
# host — pull any installed app from a connected device
adb shell pm list packages | head -40
adb shell pm path com.some.package
adb pull /data/app/.../base.apk target.apk
# host — score it without decompiling
python3 ../Android-Security-Handbook/05-Kotlin/scripts/detect-kotlin.py target.apk
# host — then confirm with jadx
jadx --no-res -d apk_out target.apk
grep -rl "kotlin.Metadata" apk_out/sources/ | wc -l
grep -rl "kotlin.Metadata" apk_out/sources/com/some/package/ | wc -l
Compare the two counts. The first includes bundled Kotlin libraries; the second tells you whether the application's own code is Kotlin. Reporting "this app is Kotlin" because a bundled library is Kotlin is a mistake that will cost you credibility.
Hints¶
Hint 1 — I get no output from the grep commands
Check your paths. JADX writes decompiled Java under `Hint 2 — JADX shows a class I cannot open, or shows an error comment
JADX falls back to a comment containing raw smali when it cannot reconstruct Java for a method. This happens with some synthetic bridges. It is not a failure: the smali is often *more* informative. If it bothers you, decompile the same input with `--show-bad-code` to force output.Hint 3 — How do I tell a Kotlin `object` from a Java singleton?
Look for `@Metadata` first; that settles it. If the annotation has been stripped, use these secondary tells: the Kotlin form always has a `private` constructor with no parameters, always names the field exactly `INSTANCE`, always assigns it in `Hint 4 — The app I pulled has no `kotlin/` package at all but I am sure it is Kotlin
R8 relocates and shrinks `kotlin-stdlib` aggressively. `kotlin/jvm/internal/Intrinsics` may have been renamed to something like `a/a/a/b`. Fall back to structural signals: a class with a static field of its own type assigned in `Success criteria¶
You have completed this lab when all of the following are true.
- You can produce the verdict table from step 9 for
mixed.jarwithout looking at your source files. - For each of the five signals, you can state one situation in which it is absent even though the class
is Kotlin. (For example,
Intrinsics.checkNotNullParameteris absent from a private function.) - You can decode a
$defaultcall's bitmask argument and say which parameters the caller omitted. - You can explain, in one sentence each, what
access$getVERSION$cpand$this$maskedare. - You have run the exercise against one real APK you did not build, and you can state separately how much of it is Kotlin application code versus Kotlin library code.
Solution¶
Full solution — expected outputs and verdicts
### Class inventory produced by step 1com/example/mixed/Clock.class
com/example/mixed/Credential.class
com/example/mixed/LegacyStore.class
com/example/mixed/ModernStore.class
com/example/mixed/ModernStore$Companion.class
com/example/mixed/ModernStoreKt.class
com/example/mixed/Result.class
com/example/mixed/Result$Err.class
com/example/mixed/Result$Ok.class
$ grep -rl "kotlin.Metadata" jadx_out/sources/com/example/mixed/
jadx_out/sources/com/example/mixed/Clock.java
jadx_out/sources/com/example/mixed/Credential.java
jadx_out/sources/com/example/mixed/ModernStore.java
jadx_out/sources/com/example/mixed/ModernStoreKt.java
jadx_out/sources/com/example/mixed/Result.java
$ grep -rL "kotlin.Metadata" jadx_out/sources/com/example/mixed/
jadx_out/sources/com/example/mixed/LegacyStore.java
// jadx_out/sources/com/example/mixed/Credential.java
@Metadata(d1 = {...}, d2 = {...}, k = 1, mv = {1, 9, 0}, xi = 48)
public final class Credential {
private final long expires;
private final String token;
private final String user;
public Credential(String user, String token, long expires) {
Intrinsics.checkNotNullParameter(user, "user");
Intrinsics.checkNotNullParameter(token, "token");
this.user = user;
this.token = token;
this.expires = expires;
}
public /* synthetic */ Credential(String str, String str2, long j, int i,
DefaultConstructorMarker defaultConstructorMarker) {
this(str, str2, (i & 4) != 0 ? 0L : j);
}
public static /* synthetic */ Credential copy$default(Credential credential, String str,
String str2, long j, int i, Object obj) {
if ((i & 1) != 0) { str = credential.user; }
if ((i & 2) != 0) { str2 = credential.token; }
if ((i & 4) != 0) { j = credential.expires; }
return credential.copy(str, str2, j);
}
public final String component1() { return this.user; }
public final String component2() { return this.token; }
public final long component3() { return this.expires; }
public final Credential copy(String user, String token, long expires) {
Intrinsics.checkNotNullParameter(user, "user");
Intrinsics.checkNotNullParameter(token, "token");
return new Credential(user, token, expires);
}
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof Credential)) return false;
Credential credential = (Credential) other;
return Intrinsics.areEqual(this.user, credential.user)
&& Intrinsics.areEqual(this.token, credential.token)
&& this.expires == credential.expires;
}
public int hashCode() {
return (((this.user.hashCode() * 31) + this.token.hashCode()) * 31)
+ Long.hashCode(this.expires);
}
public String toString() {
return "Credential(user=" + this.user + ", token=" + this.token
+ ", expires=" + this.expires + ")";
}
public final long getExpires() { return this.expires; }
public final String getToken() { return this.token; }
public final String getUser() { return this.user; }
}