10

This is a long story and I'm going try to provide as much details as I can.

I have been using android studio 3.x.x to built my apps over several months, until recently it started to offer me an update to the newest stable 4.0.1.

I updated and it was hassle free as it was supposed to be, all plugins updated without error and the code compiled at first in the new android studio, the project worked fine in tests then i uploaded to the play store's internal test track (here the problem starts).

the app started showing a some sort of lock or infinite loop error in some devices... it was weird because the very same devices when running the debug build were fine, at this moment i thought that some proguard rules are the problem. So I adjusted the build.grade file in order to the debug build have the same settings as the release build (shrink and minify TRUE).

after compiled the app runs smoothly in the emulator and in the problematic devices...
once more i uploaded it to google play, and downloaded from there the app doesn't work...

I made a very exhaustive and annoying code debug in order to get what method was being locked somehow and i found this suspect:

OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
    httpClientBuilder.readTimeout(15000, TimeUnit.MILLISECONDS);
    httpClientBuilder.writeTimeout(15000, TimeUnit.MILLISECONDS);
    httpClientBuilder.connectTimeout(15000, TimeUnit.MILLISECONDS);
    httpClientBuilder.retryOnConnectionFailure(true);
    httpClientBuilder.followRedirects(true);
    httpClientBuilder.followSslRedirects(true);
    httpClientBuilder.connectionPool(DEFAULT_CONNECTION_POOL);
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.setMaxRequests(10);
    dispatcher.setMaxRequestsPerHost(10);
    httpClientBuilder.dispatcher(dispatcher);
    okHttpClient = httpClientBuilder.build();

private boolean isInternetOK(){
    boolean ret = false;
    try {
        okHttpClient.newCall(new Request.Builder().get().url("https://www.google.com").build()).execute();
        ret true;
    } catch (Exception e) {                
    }
    return ret;
}

In my launcher activity, before start login I verify if users internet connection is ok that is a simple code and the httpclient has a timeout of 15 seconds so would connect or fail fast.

it made no sense to me why this specific method is working in dev and not working when served from play store (in the very same device and very same build config)

I spend a few hours trying to solve the problem... until i decided to downgrade android studio to the previous version i was using. AND THE PROBLEM SOLVED BY ITSELF!

So i'm 100% sure the problem is related to android studio 4.0.1 and its gradle plugin and how they interact with "com.squareup.okhttp3:okhttp:3.12.12"

here comes the observations i made:

1- the apk generated by android studio 4.0.1 is 4.18MB while 3.6.3 generates a 4.33MB apk (same build.config and same proguard.rules)

2- after update android studio to 4.0.1 proguard.rules started showing lots of erros, like is said here: Unresolved class name proguard-rules Android Studio 4.0 but still compiles fine (and worked fine in debug with shrink and minify on)

3- the only difference in the project is android studio version and com.android.tools.build:gradle dependency, because it must follow android studio version.

Well this is as far as I got, i'm available to provide any more info and even the apks compiled from the different versions if anyone can help me... now i need to sleep because is 23h30 today is Saturday and i lost 7 hours of my life in this junk update

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Rafael Lima
  • 3,079
  • 3
  • 41
  • 105
  • create an empty project with the same name and package, delete all files from app/main dir and copy from old project, sync gradle – Style-7 Aug 16 '20 at 05:24
  • 1
    What change you had to make exactly in the `com.android.tools.build:gradle`? Can you please post that? – Reaz Murshed Aug 19 '20 at 20:17
  • @ReazMurshed, NONE... this plugin must be updated to be compatbile with android studio version... so always you change android studio version this import must be edited... – Rafael Lima Aug 19 '20 at 21:49

2 Answers2

4

it may be helpful for you to check internet, if your method not working on some devices

you can also get confirmation about your error it is due to check internet or any other

boolean hasActiveInternetConnection() {
    try {
        InetAddress ipAddr = InetAddress.getByName("google.com");
        return !ipAddr.equals("");
    } catch (Exception e) {
        return false;
    }
}
Abdur Rehman
  • 1,247
  • 10
  • 13
  • this might be an alternative way to check internet connection, but since i use okhttp in many other points of my code, simple replace the internet connection test wont make the project work – Rafael Lima Aug 26 '20 at 21:51
  • @RafaelLima you can use Retrofit instead of okhttp for other points of your code – Abdur Rehman Aug 27 '20 at 06:48
  • that is not a suggestion that you should do here. first retrofit uses okhttp internally, so if this doesnt work, retrofit wont work either. second suggesting such a big change like switch the whole api in for a inconstant problem is like saying "start from scratch" – Rafael Lima Aug 27 '20 at 15:48
0

Add this line in your AndroidManifest.xml file,

<application
    android:usesCleartextTraffic="true"

still if you are facing same problem,

Try this configuration, update project level build.gradle file,

    classpath 'com.android.tools.build:gradle:3.6.4'

update in gradle/wrapper/gradle-wrapper.properties file

distributionUrl=https://services.gradle.org/distributions/gradle-5.6.4-all.zip

then clean project

try File-> Invalid Cache / Restarts...

Karthik Kompelli
  • 2,104
  • 1
  • 19
  • 22