Inconsistent behavior/performance of Gradle/Android Studio especially with bool variables

I am working on a app that simulates a game of Cricket. My logic was correct but program kept giving wrong output as the changes were not reflected. I invalidated caches and restarted the android studio. But still the glitch of not reflecting the changes kept going on. The changes were specially related to bool variables. Suddenly after two days I run the app and it was behaving as expected. I am puzzled is there any glitch in Gradle build system? All this mess ate a lot of time of mine. I am thinking of switching to some other android IDE. Which one is better and do not use Gradle? Coz I am frustrated with the Android Studio and Gradle. I am not trying to spread the hate against Android Studio and Gradle but I am deeply frustrated with the performance and inconsistency.

Edit: I have a class named Team which has two bool variables that indicate if a team is batting or bowling.

Code looks like this:

public class Team {

private int numberOfRunsScored, numberOfWicketsTaken;
private int numberOfBallsThrown, numberOfOversCompleted;
private String inningTypeString;
private boolean batting,balling;

public boolean isBatting() {
    return batting;
}

public void setBatting(boolean batting) {
    this.batting = batting;
}

public boolean isBalling() {
    return balling;
}

public void setBalling(boolean balling) {
    this.balling = balling;
}

In MainActivity to simulate the game, I made two objects of class Team as “a” and “b”. At the start of the game a random outcome decides which team will bat first and which team will bowl fist. So for first inning I set the behavior as follows:

`public void performToss() {
    tossCallByTeamA = a.tossCall();
    tossOutput.setTossOutcome(tossOutput.doToss());
    boolean tossOutCome = tossOutput.getTossOutcome();
    if (tossOutCome == tossCallByTeamA) {
        a.setInningType(Team.InningType.BATTING);
        b.setInningType(Team.InningType.BOWLING);
        a.setBatting(true);
        b.setBalling(true);
    } else {
        a.setInningType(Team.InningType.BOWLING);
        b.setInningType(Team.InningType.BATTING);
        a.setBalling(true);
        b.setBatting(true);
    }
}`

After first inning the batting and bowling switches between the teams. So I wrote following code to switch the roles.

public void switchViewStubs() {
    //stub1.setVisibility(View.GONE);
    //stub2.setVisibility(View.GONE);
    alloversCompleted = true;
    createViewStubs();
    if (a.getInningType().equalsIgnoreCase("batting")) {
        newStub1.setLayoutResource(R.layout.bowling_team_layout);
        newStub2.setLayoutResource(R.layout.batting_team_layout);
        a.setBatting(false);
        b.setBatting(true);
    }
    if (a.getInningType().equalsIgnoreCase("bowling")) {
        newStub1.setLayoutResource(R.layout.batting_team_layout);
        newStub2.setLayoutResource(R.layout.bowling_team_layout);
        b.setBatting(false);
        a.setBatting(true);
    }
    newStub1.inflate();
    newStub2.inflate();
    instantiateViewsAndSettingListenersToButtons();
    aRuns = 0;
    bRuns = 0;
}

Code to set text to runs TextView was the code that made the trouble and ate my precious 2-3 hours even though I was innocent and correct at writing the logic. Here is the code.

 if (view.equals(oneRun)) {
                //incrementBalls();
                totalRuns++;
                if (a.isBatting()) {
                    //new AlertDialog.Builder(this).setTitle("batting staus").setMessage("a is batting").show();
                    aRuns++;
                    //a.setNumberOfRunsScored(aRuns);
                    if (!alloversCompleted) {
                        incrementBalls();
                        updateRuns(aRuns);
                    }
                    if (alloversCompleted) {
                        updateRuns(0);
                        alloversCompleted = false;
                    }
                    if ((totalRuns > targetRuns) && (secondInning)) {
                        setTextOnFooter("Team A wins");
                    }
                }
                if (b.isBatting()) {
                    //new AlertDialog.Builder(this).setTitle("batting staus").setMessage("b is batting").show();
                    bRuns++;
                    //b.setNumberOfRunsScored(bRuns);
                    if (!alloversCompleted) {
                        incrementBalls();
                        updateRuns(bRuns);
                    }
                    if (alloversCompleted) {
                        updateRuns(0);
                        alloversCompleted = false;
                    }
                    if ((totalRuns > targetRuns) && (secondInning)) {
                        setTextOnFooter("Team B wins");
                    }
                }
            }

The code block of (b.isBatting) in above code was not executing at all even the batting role was switched. But I had written the correct code and logic to change the role of batting to bowling and bowling to batting. I invalidated cashes and restarted android studio but the code was behaving incorrectly. I have heard that Gradle build system is fast because it considers and builds only changed code instead of building all the code again and again. I suspect some glitch in Gradle did not consider the changes made by me to bool variables. After some two days I rebuild the code and ran it again and it ran perfectly fine. This behavior is inconsistent and problematic. Can anyone predict what must have gone wrong? The Gradle version is gradle:4.2.2

Answer

If a team can be either “balling”/”bowling” or “batting” but not both then your design of theTeam class is error prone (because you can set both values to true, or both values to false which would mean that the team is neither “balling” nor “batting”).

Let’s assume that at the start team a is batting (a.batting is true, a.balling is false) and team b correspondingly balling (b.batting is false, b.balling is true).

If now void switchViewStubs() is called it will change the “batting” status for both teams and leave the “balling” status as it is. Therefore we will have the following situation:

  • a.batting is false, a.balling is false
  • b.batting is true, b.balling is true

Does that mean that team b is playing against itself?


If this “balling”/”bowling” or “batting” is truly exclusive you should change your Team class as follows:

public class Team {
    private boolean batting;

    public boolean isBatting() {
        return batting;
    }

    public void setBatting(boolean batting) {
        this.batting = batting;
    }

    public boolean isBalling() {
        return !batting;
    }