I have an annoying problem that forces me to Build -> Clean Project before every attempt to run it. But it's not obvious (at least for me) as to what's causing it.
Description
- I First run the app - it runs fine;
- Find something that needs to be changed;
- Code such change;
- Click run - (it doesn't matter if it's run or close and run);
- App is launched normally;
- Can fill in EditText just fine;
- Tap button to login: crash.
Important: (possible narrowing down the problem) As you'll se below, I have a code that calls another activity. When I remove the code that creates the intent and calls it, the app runs fine, and when I add it again, it sometimes runs fine for a few more attempts, going back to the problem later.
The error:
java.lang.ClassCastException: com.example.foobar.activity.FooActivity cannot be cast to com.example.foobar.activity.BarActivity at com.example.foobar.activity.-$Lambda$3.$m$0(Unknown Source) at com.example.foobar.activity.-$Lambda$3.onClick(Unknown Source) at android.view.View.performClick(View.java:4848)
Code
FooActivity.java:
Button btLogin;
void onCreate(..){ prepareComponents(); }
void prepareComponents(){
// findViewById
btLogin.setOnClickListener(v -> onClickLogin());
}
void onClickLogin(){
// check user info and do other apparently unimportant stuff
Intent intent = new Intent(FooActivity.this, BarActivity.class);
startActivity(intent);
finish();
}
Setup
- Android Studio 2.3.3
- Gradle 3.3
- buildToolsVersion "25.0.2"
- compileSdkVersion 25
- Using buildTypes
- Using some libs in gradlde
- Using
jackOptions enabled
in gradle
My Top-level gradle shows classpath 'com.android.tools.build:gradle:2.3.3'
.
Complementary info:
- Instant run is already disabled.
- I tried checking workd offline.
- Invalidate Caches / Restart just moves to problem to another part of code, but same behavior. Update: problem's gone gack to first screen.
- Tested on 2 different devices with different android versions.
- Uninstalling the app from the device doesn't help - I still need to clean project.
- Running without cleaning takes less time in process running "Gradle Build Running", but when I clean (which itself isn't very time consuming), the next run takes 1m30s to "Gradle Build Run".
- I've tried google, but the closest I could find was this question, which seems to be the same, but didn't really help me.
From my tests, commenting out the intent and activity call part solves the problem, by I need to call it, so not really. Besides, it works after cleaning, so it doesn't seem like an actual code problem. What could it be and how could I solve it?
I don't know what else info to include. Just let me know :)