An overview of the

How to decompile KT files into Java files

1 Click Android Studio Tools

Click to Decompile

Kt file

Decompiled Java file

With this operation, you can quickly understand the concepts in Kotlin

Package-level functions, package-level properties

Kotlin differs from Java in that functions and attributes do not need to be defined in classes

Kotlin defines a package-level function and package-level properties

const val name : String  = "hahha"

var age :Int = 12


fun saySome(a){
    println("Say something.")}class AAA {
    fun A(a){
        saySome()

        println(name+ age)
    }
}
Copy the code

Decompile to Java

public final class AAA {
   public final void A(a) {
      AAAKt.saySome();
      String var1 = "hahha" + AAAKt.getAge();
      boolean var2 = false; System.out.println(var1); }}// AAAKt.java
package com.example.lib.hanshu;

import kotlin.Metadata;
import org.jetbrains.annotations.NotNull;


public final class AAAKt {
   @NotNull
   public static final String name = "hahha";
   private static int age = 12;

   public static final int getAge(a) {
      return age;
   }

   public static final void setAge(int var0) {
      age = var0;
   }

   public static final void saySome(a) {
      String var0 = "Say something.";
      boolean var1 = false; System.out.println(var0); }}Copy the code

Java class aaAkt.java, package-level functions and properties, are compiled as static properties and methods of aaAkt.java. The external table call is actually a static method of aaAkt.saysome ()

That is, package-level properties and methods are eventually compiled as static methods and properties

object

Kotlin makes singletons simpler by referring to type declarations defined using the object keyword and generally used as singletons and associated objects

object Text {
    var name: String = "xiaoming"

    fun get(a) {
        println("hhaha")}}fun main(a){
    Text.get()}Copy the code

The above code is compiled into Java as follows:

public final class Text {
   @NotNull
   private static String name;
   @NotNull
   public static final Text INSTANCE;

   @NotNull
   public final String getName(a) {
      return name;
   }

   public final void setName(@NotNull String var1) {
      Intrinsics.checkNotNullParameter(var1, "
      
       "
      ?>);
      name = var1;
   }

   public final void get(a) {
      String var1 = "hhaha";
      boolean var2 = false;
      System.out.println(var1);
   }

   private Text(a) {}static {
      Text var0 = new Text();
      INSTANCE = var0;
      name = "xiaoming"; }}// TextKt.java

public final class TextKt {
   public static final void main(a) {
      Text.INSTANCE.get();
   }

   // $FF: synthetic method
   public static void main(String[] var0) { main(); }}Copy the code

You can think of a class defined by the object keyword as a singleton

Associated object

Associated objects are better understood as objects created with a static factory

class Myclass {

    var name: String = "aaa"

    companion object Factory {
        fun create(a): Myclass = Myclass()
    }

    fun text(a) {
        println("Call method")}}fun main(a) {
    var myclass = Myclass.create()
    // The two expressions are equivalent
    var myclass1 = Myclass.Factory.create()

    println(myclass.name)

    myclass1.text()
}
Copy the code

The output

Aaa call methodCopy the code

Decompile into Java

public final class MyclassKt {
   public static final void main(a) {
      Myclass myclass = Myclass.Factory.create();
      Myclass myclass1 = Myclass.Factory.create();
      String var2 = myclass.getName();
      boolean var3 = false;
      System.out.println(var2);
      myclass1.text();
   }

   // $FF: synthetic method
   public static void main(String[] var0) { main(); }}// Myclass.java


public final class Myclass {
   @NotNull
   private String name = "aaa";
   @NotNull
   public static final Myclass.Factory Factory = new Myclass.Factory((DefaultConstructorMarker)null);

   @NotNull
   public final String getName(a) {
      return this.name;
   }

   public final void setName(@NotNull String var1) {
      Intrinsics.checkNotNullParameter(var1, "
      
       "
      ?>);
      this.name = var1;
   }

   public final void text(a) {
      String var1 = "Call method";
      boolean var2 = false;
      System.out.println(var1);
   }

   public static final class Factory {
      @NotNull
      public final Myclass create(a) {
         return new Myclass();
      }

      private Factory(a) {}// $FF: synthetic method
      public Factory(DefaultConstructorMarker $constructor_marker) {
         this(a); }}}Copy the code

Create a static Facetory object and myClass.factory.create () to retrieve the Myclass object

Omits the name of the associated object

Here is an example of an associated object that has no name

class Myclass1 {
    companion object {
        fun text(a){
            println("text")}}}fun main(a){
    Myclass1.Companion.text()
    // The two expressions are equivalent
    Myclass1.text()
}
Copy the code

As you can see, you can call a method directly from the Myclass1. Text () class name, without the need for an object, much like a static method in Java

Decompile to Java

public final class Myclass1 {
   @NotNull
   public static final Myclass1.Companion Companion = new Myclass1.Companion((DefaultConstructorMarker)null);


   public static final class Companion {
      public final void text(a) {
         String var1 = "text";
         boolean var2 = false;
         System.out.println(var1);
      }

      private Companion(a) {}// $FF: synthetic method
      public Companion(DefaultConstructorMarker $constructor_marker) {
         this(a); }}}// Myclass1Kt.java

public final class Myclass1Kt {
   public static final void main(a) {
      Myclass1.Companion.text();
      Myclass1.Companion.text();
   }

   // $FF: synthetic method
   public static void main(String[] var0) { main(); }}Copy the code

Java code is more intuitive, should be able to understand, not repeat

@JvmStatic

Using this tag compiles static methods

So, for example, the first example that we had up there, let’s change it

object Text {
    var name: String = "xiaoming"
    // add @jvmstatic
   @JvmStatic
    fun get(a) {
        println("hhaha")}}fun main(a){
    Text.get()
}
Copy the code

Decompile to Java

public final class Text {
   @NotNull
   private static String name;
   @NotNull
   public static final Text INSTANCE;

   @NotNull
   public final String getName(a) {
      return name;
   }

   public final void setName(@NotNull String var1) {
      Intrinsics.checkNotNullParameter(var1, "
      
       "
      ?>);
      name = var1;
   }

   / / comment 1
   @JvmStatic
   public static final void get(a) {
      String var0 = "hhaha";
      boolean var1 = false;
      System.out.println(var0);
   }

   private Text(a) {}static {
      Text var0 = new Text();
      INSTANCE = var0;
      name = "xiaoming"; }}// TextKt.java
package com.example.lib.danli;

import kotlin.Metadata;


public final class TextKt {
   public static final void main(a) {
      Text.get();
   }

   // $FF: synthetic method
   public static void main(String[] var0) { main(); }}Copy the code

A method marked with @jvmStatic becomes static