Obfuscate Kotlin source
Requires Codefend 3.3.0 or later for the recommended m_ preset.
This Kotlin/JVM example renames an application-owned helper and its parameters before compilation. It calculates an order total while preserving main and Kotlin's println API.
1. Create the source
Install Node.js and Codefend, a JDK, and the Kotlin command-line compiler. In a new directory, run:
npm install --global codefendCreate source/Main.kt:
private fun m_total(m_unitPrice: Int, m_count: Int, m_discount: Int): Int {
return m_unitPrice * m_count - m_discount
}
fun main() {
println(m_total(20, 4, 5))
}Expected obfuscated source
With the configuration in the next step, Codefend produces:
private fun Ox0(Ox1: Int, Ox2: Int, Ox3: Int): Int {
return Ox1 * Ox2 - Ox3
}
fun main() {
println(Ox0(20, 4, 5))
}The program's expected output remains 75. Generated identifier numbers can change when the source or Codefend version changes.
Only names belonging to this example use m_. Keep main, Int, and println unchanged. This is a standalone Kotlin/JVM console program, not an Android or Kotlin/Native integration.
2. Configure the transformation
Create .codefendrc.json in the project root:
{
"generation": {
"inputDir": "source",
"outputDir": "release"
},
"parser": {
"name": "startWithMAndUnderscore"
}
}The startWithMAndUnderscore preset selects this example's m_ names, leaving unrelated underscore names alone. See parser presets for the exact rule and why we recommend this convention.
The plain Main.kt filename stays unchanged. The default empty pool uses generated Ox identifiers.
Keep source text-only and release as disposable sibling output. Codefend removes an existing output directory. Keep JARs, downloaded libraries, and build caches outside the input tree.
3. Compile both versions
Run from the project root:
kotlinc source/Main.kt -include-runtime -d original.jar
java -jar original.jar
codefend --obfuscate
kotlinc release/Main.kt -include-runtime -d generated.jar
java -jar generated.jarBoth programs should print:
75The compiler's -include-runtime option includes the Kotlin runtime in the example JAR. The official command-line guide describes this workflow.
Larger Kotlin projects
Keep names used by Java callers, reflection, serialization, framework annotations, Android components, resources, and generated code stable unless every relevant reference is updated. Parameter names can also be part of Kotlin named-argument calls. This example calls its private helper positionally and does not expose those parameters as a public API.
Codefend works on text; it does not understand Kotlin symbols, JVM metadata, or Android build rules. For a Gradle/Android project, introduce a separate generated-source step only after verifying generated files, source sets, and runtime behavior. Review current limitations; no Android compatibility guarantee follows from this console example.
Add Codefend to your workflow
Place Codefend before Kotlin compilation in a build script or CI job. For Gradle projects, keep dependency and source-set configuration intact while selecting the generated source for the intended build and tests.
codefend --obfuscate
kotlinc release/Main.kt -include-runtime -d generated.jar
java -jar generated.jarIn CI, stop on a failed step and package only validated generated output. See build and deployment for the shared workflow.