Changing java file contents using gradle for different build types in Android

I am working on an Android project where I need to replace the contents of the java class for only one type of assembly (i.e. type of assembly) before gradle. I do not want to remove or replace the original contents for different types of assemblies.

As an example, I have two different types of assembly:

  • Debug
    • Original Test.java
  • Release
    • Modify the contents of Test.java

What I have tried so far is to delete the contents of Test.java and rewrite some other content with groovy (gradle). But once the assemblies are completed, Test.java is already overridden by the new content and cannot be returned back to its original state. Is there any other way to achieve a mechanism in which I can store different content for different types of assemblies in a single file?

Thanks in advance.

+4
source share
2 answers

- src/debug src/release . src/main. , java , . . Android Studio Gradle , , .

+4

if , . Test

public class Test {

  public void doSomething() {
    System.out.println("debug");
  }
}

:

public class Test {

  public void doSomething() {
    System.out.println("release");
  }
}

import grails.util.Environment;

public class Test {

  public void doSomething() {

    if (Environment.getCurrent() == Environment.DEVELOPMENT) {
      System.out.println("debug");
    } else {
      System.out.println("release");
    }
  }
}
0

All Articles