0

I tried creating a new file in my android app project in the android studio named "TestCode.java" with code

public class TestCode {

    public static void main(String[] args) {
        int myStr = 11;
        System.out.println(String.format("%06d",myStr));
    }
}

when I ran this particular static method by right-clicking on this file, it still ran Gradle build and gave me the errors from my main project and didn't print the result of this file.

Is there any way to just test this simple java code independently in the same project and avoid Gradle Build for the whole project.

currently, I have to open another program "IntelliJ" to test these simple java codes.

beginner
  • 2,366
  • 4
  • 29
  • 53
  • 1
    Does this answer your question? [Run single java file with standard main(String \[\] args) method - Android Studio](https://stackoverflow.com/questions/27137479/run-single-java-file-with-standard-mainstring-args-method-android-studio) – deadshot Mar 10 '20 at 11:51
  • @RajuKomati I tried running JUnit test but it still gives me the error from my project, so that solution didn't work for me – beginner Mar 10 '20 at 11:52

1 Answers1

1

You can run Unit Test in Android in a following way:

class ExampleUnitTest {
    @Test
    fun format_string() {
        val myStr = 11
        println(String.format("%06d", myStr))
    }
}

Output:

000011

If you are still facing issues, I would suggest to have a look to this answer

You need to modify your configuration to use Make, no error check option instead of Make.

You can also have a look to this Vic's answer

Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42