Skip to content

Guardsquare/kotlin-metadata-printer

Repository files navigation

Kotlin Metadata Printer

The Kotlin metadata printer is a free tool to print the Kotlin metadata in a human-readable format. The printer is built on the ProGuardCORE library and can process class files, zip files, jars or apks.

The Kotlin metadata printer is integrated in ProGuard Playground or you can run it locally.

demo of printer GUI

Building

You can build the Kotlin metadata printer jar by executing the following Gradle command:

./gradlew build

Once built a jar will be created in lib/kotlin-metadata-printer.jar

Executing

You can execute the printer directly through gradle as follows:

./gradlew :kmp-cli:run --args "input.{apk,jar,zip,class}"

Or you can execute the built printer jar as follows:

java -jar lib/kotlin-metadata-printer.jar input.{apk,jar,zip,class}

Options

--filter '<classNameFilter>' class name filter e.g. --filter '!android.**,com.mypackage.**'
--output '<outputFile>'      write output to this file instead of stdout e.g. --output 'myfile.txt'
--json                       output the metadata in a JSON structure
--divider                    a string that is printed between each Kotlin metadata

Example

The following example is a basic Android activity class written in Kotlin:

/**
 * Sample activity that displays "Hello world!".
 */
class HelloWorldActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

and this is its associated metadata as printed by the Kotlin metadata printer:

/**
* Kotlin class.
* From Java class: com.example.HelloWorldActivity
*/
class HelloWorldActivity : android.support.v7.app.AppCompatActivity {

    // Functions

    protected open fun onCreate(savedInstanceState: android.os.Bundle?) { }
}

Using as a library

The project is split into CLI (kmp-cli) and library (kmp-lib) modules. To use the printer programmatically from your project add a dependency on the library and then use the KotlinMetadataPrinter class along with a ProGuardCORE ClassPool. The printed metadata is placed into the processing info field of the Clazz.

import com.guardsquare.proguard.kotlin.printer.KotlinMetadataPrinter;
import proguard.classfile.kotlin.visitor.ReferencedKotlinMetadataVisitor;
import proguard.classfile.visitor.MultiClassVisitor;
import proguard.classfile.util.kotlin.KotlinMetadataInitializer;
import proguard.classfile.ClassPool;

import static proguard.io.util.IOUtil.read;

public class Main
{
    public static void main(String[] args)
    {
        ClassPool programClassPool = read(args[0], false);
        programClassPool.classesAccept(
            new MultiClassVisitor(    
            new KotlinMetadataInitializer((clazz, errorMessage) -> System.err.println(errorMessage)),        
            new ReferencedKotlinMetadataVisitor(
            new KotlinMetadataPrinter(programClassPool)))
        );

        programClassPool.classesAccept(clazz -> System.out.println(clazz.getProcessingInfo()));
    }
}

Contributing

The Kotlin metadata printer is built on the ProGuardCORE library.

Contributions, issues and feature requests are welcome in both projects. Feel free to check the issues page and the contributing guide if you would like to contribute.

License

The Kotlin metadata printer is distributed under the terms of the Apache License Version 2.0.

Enjoy!

Copyright (c) 2002-2023 Guardsquare NV