Kotlin Obfuscator
Renames local variables and parameters, encodes string literals with a matching runtime decoder, and strips comments from your .kt source — entirely in your browser, built for Android projects. Every output is re-checked for structural integrity before it's shown to you.
Kotlin obfuscation rewrites readable .kt source into a form that is harder for a human to follow, while preserving the exact runtime behaviour of the original program. This tool uses a hand-written Kotlin tokenizer — aware of string templates, raw strings, and nested block comments — to find identifiers it can safely rename, and performs a structural validation pass on its own output before showing it to you.
Read this before you rely on it: Kotlin compiles to JVM or Dalvik/ART bytecode, which carries its own class and member metadata independent of your original source file. Renaming identifiers in a .kt file does nothing to protect a built APK — a decompiler such as jadx reconstructs readable code straight from the bytecode. Android's release build already runs R8 (or ProGuard) to shrink and obfuscate your compiled code — that's what actually protects a shipped app. This tool protects source text that is shared or exposed as-is — library source, build scripts, snippets — not a compiled artifact.
Table of Contents
What is Kotlin Obfuscation?
At the source level, Kotlin obfuscation transforms a .kt file into an equivalent file that is harder to read or copy, without changing what it does once compiled. This is distinct from bytecode-level obfuscation performed by R8/ProGuard (and commercial tools like DexGuard), which operate on the compiled app and actually shrink, inline, and rename at the class-file level — the layer that determines what a decompiled APK looks like.
This tool operates purely at the source level. It's the right fit when the thing you're protecting is the .kt text itself, not a binary you've already built and signed.
Techniques Used by This Tool
Scope-Aware Local Renaming
A brace/paren classification pass distinguishes type bodies (class/interface/object/companion/enum) from function, accessor, init, and lambda bodies — including Kotlin's common parenthesis-less trailing-lambda idiom (run { ... }, apply { ... }). Only identifiers declared inside a function-like body are renamed; primary-constructor val/var properties, data class fields, and companion constants are always left alone.
Template-Reference Protection
Before renaming anything, the tool scans every string for $name and ${expr} references and permanently excludes those identifiers from renaming — so a local variable used inside an interpolated string can never be left dangling with its old name.
String Literal Encoding
Plain double-quoted string literals with no template interpolation are encoded as Base64 bytes XORed with a per-build key, decoded at runtime through a small generated object using java.util.Base64. Raw/triple-quoted strings and anything containing $ are left untouched.
Comment Removal
Handles Kotlin's // line comments and properly nested /* */ block comments (Kotlin allows nesting them, unlike Java/C#), without touching comment-like text inside strings.
How the Tool Works
- Tokenize. A hand-written tokenizer splits the source into identifiers, keywords, string/raw-string/char literals (with template-aware brace tracking), numbers, backtick identifiers, operators, and comments.
- Scan protected names. A first pass walks the brace-classification stack to collect every type, function, and property name, plus every identifier referenced inside a string template — none of these are ever renamed.
- Find rename candidates. A second pass, using the same brace stack plus a parenthesis-kind stack (to tell a parameter list apart from an
if/for/when/catchcondition), finds localval/vardeclarations, for-loop and catch variables, and explicit lambda parameters. - Encode strings. Eligible string literal tokens are replaced with calls into a generated decoder object, appended once at the top of the file.
- Rename identifiers. Every identifier token matching a candidate is replaced with a short generated name, consistently everywhere that exact name appears.
- Reconstruct source. Tokens are re-emitted in order; whitespace and line breaks are preserved by default since Kotlin treats a newline as a statement separator.
- Validate structurally. The output is re-tokenized and checked for balanced braces/brackets/parentheses and properly terminated literals. See the note below on what this does and doesn't catch.
Output Validation
A browser cannot run the Kotlin compiler, so this tool cannot fully verify that its output compiles. Instead it re-tokenizes the obfuscated result and confirms every brace, bracket, parenthesis, and string/char/raw-string literal is balanced and correctly terminated — the class of error most likely to be introduced by a renaming/reconstruction bug.
What's Safe vs Unsafe to Rename
| Construct | Renamed? | Reason |
|---|---|---|
Local val/var inside a function/accessor/init body | Yes | Not observable outside that body |
for (item in items) loop variable | Yes | Scoped to the loop body |
catch (e: Exception) variable | Yes | Scoped to the catch block |
Explicit lambda parameters { x, y -> ... } | Yes | Scoped to the lambda |
| Function/constructor parameters | Opt-in, off by default | Safe positionally, but can break named-argument call sites |
Primary-constructor val/var properties | No | Public API — backs equals(), copy(), destructuring, external access |
| Class / interface / object / enum / companion names | No | Part of the public type surface |
| Function and property names | No | May be called externally, overridden, or used via reflection |
Identifiers referenced in a $name/${expr} template | No | Template contents aren't rewritten, so the reference must stay valid |
Destructuring targets val (a, b) = pair | No | Safely tracking the component mapping needs type information this tool doesn't have |
| Raw / triple-quoted strings | Never encoded | Typically multi-line/formatted text, often templated |
Use Cases
Library Modules Shared as Source
Internal or open-source-adjacent Kotlin modules that ship as source rather than a compiled .aar benefit from obscured method-body logic while keeping their public API untouched.
Gradle Kotlin DSL Scripts
Hide implementation details of custom build logic (build.gradle.kts, buildSrc) when sharing snippets externally.
Coursework & Interview Exercises
Obscure a reference solution's local logic while keeping its public function signatures intact for grading or discussion.
Pre-Build Source Hardening
One step before your real protection: obfuscate shared source, then let your standard Android release pipeline run R8/ProGuard on the compiled APK/AAB as usual.
Limitations & Legal
- Not APK protection. The single most important limitation — see the callout above. A shipped Android app is protected by R8/ProGuard (or a commercial tool like DexGuard) at the bytecode level, not by source renaming.
- Named-argument call sites. If you enable parameter renaming, any caller using
foo(paramName = value)against a renamed parameter will fail to compile. This is why parameter renaming defaults to off. - Not encryption. String encoding here is reversible by design (the decoder ships right alongside it). Never embed real secrets, API keys, or signing credentials in any Kotlin file.
- Destructuring declarations.
val (a, b) = pairand destructured lambda parameters{ (a, b) -> ... }are left untouched rather than risk corrupting the component mapping. - Local (nested) functions. A function declared inside another function keeps its own name, though its parameters are still renamable when the option is on.
- Legal. Only obfuscate code you own or have rights to modify. Do not obfuscate open-source code in ways that violate its license.
Related Obfuscators
Frequently Asked Questions
What is a Kotlin obfuscator?
Does this protect my APK?
Then what is this tool actually useful for?
Is my code uploaded to your server?
Does obfuscation break my code?
Will renaming parameters break named-argument calls?
Does it work with data classes?
Does it work with Jetpack Compose?
Does it handle coroutines and suspend functions?
Which Kotlin versions are supported?
Can obfuscated Kotlin source be reversed?
Will it break public classes, functions, or properties?
What does string encoding do?
Does the decoder work on older Android API levels?
Can I use this commercially?
What identifiers are safe to rename?
Will it break destructuring declarations?
Does it support Kotlin Multiplatform (KMP)?
How is the output validated?
Can obfuscation be detected?
Conclusion
Source-level Kotlin obfuscation is a narrow, honest tool: it makes a .kt file less readable without changing what it does, is careful about Kotlin-specific traps (primary-constructor properties, string templates, named arguments, destructuring), and is upfront that it is not a substitute for R8/ProGuard protecting your compiled Android app. Combine it with proper secret management (never in source, obfuscated or not) and your standard release build pipeline for anything you actually ship.