skip to Main Content

I’ve been working on a project for a very long time.
Recently I started to have a ANR problem right on application startup.
In fact, since the App.onCreate() method is never called the application is not even starting. As far as I know the only thing that is called before that is the onCreate() method of any ContentProvider. So i removed them from the manifest file but the problem stays the same. App.onCreate still not called.

Here are some additional details:

Manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.somepackagename">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature
        android:name="android.hardware.camera"
        android:required="true" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />
        <meta-data
            android:name="io.fabric.ApiKey"
            android:value="a_very_long_secret_key" />
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="another_secret_key" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@mipmap/ic_launcher" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorPrimary" />

        <activity
            android:name=".activities.SplashActivity"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/app_name"
            android:screenOrientation="portrait">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="@string/deep_link_host"
                    android:scheme="https" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.MainActivity"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name=".activities.MessageCreationEditActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.SignInSignUpActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize"/>
        <activity
            android:name=".activities.SettingsActivity"
            android:screenOrientation="portrait"/>
        <activity
            android:name=".activities.MediaViewer"
            android:configChanges="orientation|screenSize"/>
        <activity
            android:name=".activities.CreateGroupActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan" />
        <activity
            android:name=".activities.CategoriesActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.ExploreActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.AllMembersActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.InviteMembersActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.QrCodeActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.GroupSettingsActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.SliceCreationEditActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.FavoritesMessagesActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.QrCodeScannerActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.UserDetailsActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.MembershipActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.GroupDetailsActivity"
            android:screenOrientation="portrait"/>
        <activity
            android:name=".activities.MemberManagementActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".activities.ReportActivity"
            android:screenOrientation="portrait" />
        <activity android:name=".activities.ShareMessageActivity"
            android:screenOrientation="portrait"/>
        <activity
            android:name=".activities.WebViewActivity"
            android:label="@string/title_activity_web_view"
            android:theme="@style/AppTheme"/>
        <activity android:name=".activities.ContentRatingActivity"
            android:screenOrientation="portrait"/>
        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />
        <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="@string/fb_login_protocol_scheme" />
            </intent-filter>
        </activity>

        <!--
        <provider
            android:name=".AssetContentProvider"
            android:authorities="${packageName}.AssetContentProvider"
            android:exported="false"
            android:multiprocess="false"/>
        <provider
            android:name="com.facebook.FacebookContentProvider"
            android:authorities="com.facebook.app.FacebookContentProvider547123654789621"
            android:exported="true" />
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${packageName}.provider"
            android:exported="false"
            android:multiprocess="true"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider> -->

        <service android:name=".services.FCMIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".services.FCMService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>

</manifest>

app gradle file:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

android {
   compileSdkVersion 27
   buildToolsVersion "27.0.3"

defaultConfig {
    applicationId "com.somepackagename"
    minSdkVersion 17
    targetSdkVersion 27
    renderscriptTargetApi 22
    renderscriptSupportModeEnabled true
    versionCode 34
    versionName "1.0.34"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    api fileTree(dir: 'libs', include: ['*.jar'])

    api group: 'commons-lang', name: 'commons-lang', version: '2.6'
    api 'com.android.support:multidex:1.0.1'
    api 'com.android.support:cardview-v7:27.1.1'
    api 'com.android.support:percent:27.1.1'
    api 'com.android.support.constraint:constraint-layout:1.0.2'
    api 'com.android.support:appcompat-v7:27.1.1'
    api 'com.mikhaellopez:circularimageview:3.0.2'
    api 'com.android.support:design:27.1.1'
    api 'fr.tvbarthel.blurdialogfragment:lib:2.2.0'
    api 'com.github.channguyen:rsv:1.0.1'
    api 'com.squareup.retrofit2:retrofit:2.1.0'
    api 'com.squareup.retrofit2:converter-gson:2.1.0'
    api 'net.danlew:android.joda:2.9.4.3'
    //implementation 'com.facebook.android:facebook-android-sdk:4.17.0'
    implementation 'com.facebook.android:facebook-android-sdk:[4,5)'
    api 'com.google.zxing:core:3.3.0'
    api 'com.google.android.gms:play-services-maps:11.0.2'
    api 'com.google.android.gms:play-services-ads:11.0.2'
    api 'com.google.android.gms:play-services-auth:11.0.2'
    api 'com.google.firebase:firebase-ads:11.0.2'
    api 'com.google.firebase:firebase-messaging:11.0.2'
    api 'com.google.firebase:firebase-core:11.0.2'
    api 'com.google.firebase:firebase-invites:11.0.2'
    api 'com.nvanbenschoten.motion:library:1.1.2'
    api 'com.google.ads.interactivemedia.v3:interactivemedia:3.7.2'
    api('com.crashlytics.sdk.android:crashlytics:2.7.1@aar') {
        transitive = true;
    }
    api('com.twitter.sdk.android:twitter-core:2.3.0@aar') {
        transitive = true;
    }
    api('com.twitter.sdk.android:tweet-composer:2.2.0@aar') {
        transitive = true;
    }
    api 'com.drewnoakes:metadata-extractor:2.11.0'
}

apply plugin: 'com.google.gms.google-services'

I would appreciate any suggestions or ideas.
Thanks

EDIT
Here is some additional info, Logs of ANR:

09-04 10:10:31.360 772-791/? E/ActivityManager: ANR in com.somepackagename
PID: 18073
Reason: executing service com.somepackagename/.services.FCMIDService
Load: 8.05 / 8.37 / 8.36
CPU usage from 26127ms to 0ms ago (2018-09-04 10:10:01.774 to 2018-09-04 10:10:27.901):
  3.4% 772/system_server: 2.2% user + 1.2% kernel / faults: 658 minor
  0.5% 599/media.codec: 0.3% user + 0.1% kernel / faults: 407 minor
  0.8% 434/surfaceflinger: 0.4% user + 0.3% kernel / faults: 10 minor
  0.6% 3942/com.google.android.gms.persistent: 0.4% user + 0.1% kernel / faults: 239 minor
  0.5% 2323/com.android.phone: 0.2% user + 0.3% kernel / faults: 5 minor
  0.4% 21381/adbd: 0.1% user + 0.3% kernel / faults: 421 minor
  0.1% 593/media.extractor: 0% user + 0% kernel / faults: 86 minor
  0.4% 3899/kworker/u16:3: 0% user + 0.4% kernel
  0.4% 12837/kworker/0:2: 0% user + 0.4% kernel
  0.3% 10/rcu_preempt: 0% user + 0.3% kernel
  0.3% 1969/com.android.systemui: 0.2% user + 0.1% kernel / faults: 10 minor
  0.3% 367/logd: 0.1% user + 0.1% kernel / faults: 13 minor
  0.3% 18068/kworker/1:1: 0% user + 0.3% kernel
  0.2% 301/msm-core:sampli: 0% user + 0.2% kernel
  0.2% 16559/kworker/0:1: 0% user + 0.2% kernel
  0.2% 432/msm_irqbalance: 0% user + 0.1% kernel
  0.2% 485/irq/215-fc38800: 0% user + 0.2% kernel
  0.2% 4096/com.google.android.gms: 0.1% user + 0.1% kernel / faults: 66 minor
  0.2% 6227/com.google.android.ims: 0.1% user + 0% kernel / faults: 110 minor
  0.1% 250/cfinteractive: 0% user + 0.1% kernel
  0.1% 258/mmcqd/0: 0% user + 0.1% kernel
  0.1% 17837/com.vkontakte.android: 0.1% user + 0% kernel / faults: 6 minor
  0.1% 15/ksoftirqd/1: 0% user + 0.1% kernel
  0.1% 428/[email protected]: 0% user + 0.1% kernel
  0.1% 17656/kworker/u16:2: 0% user + 0.1% kernel
  0.1% 18023/com.Project100Pi.themusicplayer: 0% user + 0% kernel / faults: 34 minor
  0.1% 3/ksoftirqd/0: 0% user + 0.1% kernel
  0.1% 40/kworker/u17:0: 0% user + 0.1% kernel
  0% 57/system: 0% user + 0% kernel
  0.1% 368/servicemanager: 0% user + 0% kernel
  0.1% 575/thermal-engine: 0% user + 0.1% kernel
  0.1% 15700/mdss_fb0: 0% user + 0.1% kernel
  0% 8/rcuc/0: 0% user + 0% kernel
  0% 170/hwrng: 0% user + 0% kernel
  0% 433/lmkd: 0% user + 0% kernel
  0% 586/zygote: 0% user + 0% kernel / faults: 158 minor
  0% 7655/ru.gismeteo.gismeteo: 0% user + 0% kernel / faults: 19 minor
  0% 10682/com.android.vending: 0% user + 0% kernel / faults: 23 minor
  0% 14340/com.android.chrome:sandboxed: 0% user + 0% kernel / faults: 4 minor
  0% 17489/com.google.android.gm: 0% user + 0% kernel / faults: 39 minor
  0% 18073/com.somepackagename: 0% user + 0% kernel / faults: 10 minor
  0% 18605/kworker/u16:4: 0% user + 0% kernel
  0% 28197/kworker/3:2: 0% user + 0% kernel
  0% 5/kworker/0:0H: 0% user + 0% kernel
  0% 13/rcuc/1: 0% user + 0% kernel
  0% 20/ksoftirqd/2: 0% user + 0% kernel
  0% 25/ksoftirqd/3: 0% user + 0% kernel
  0% 44/irq/51-cpr: 0% user + 0% kernel
  0% 47/ksoftirqd/4: 0% user + 0% kernel
  0% 160/mdss_dsi_event: 0% user + 0% kernel
  0% 262/irq/224-spdm_bw: 0% user + 0% kernel
  0% 467/ksoftirqd/5: 0% user + 0% kernel
  0% 545/dmcrypt_write: 0% user + 0% kernel
  0% 564/jbd2/dm-2-8: 0% user + 0% kernel
  0% 576/qmuxd: 0% user + 0% kernel / faults: 9 minor
  0% 578/perfd: 0% user + 0% kernel / faults: 12 minor
  0% 587/audioserver: 0% user + 0% kernel / faults: 187 minor
  0% 594/media.metrics: 0% user + 0% kernel / faults: 53 minor
  0% 596/netd: 0% user + 0% kernel / faults: 63 minor
  0% 598/wificond: 0% user + 0% kernel / faults: 1 minor
  0% 600/rild: 0% user + 0% kernel
  0% 610/cnd: 0% user + 0% kernel
  0% 1835/wpa_supplicant: 0% user + 0% kernel / faults: 1 minor
  0% 3984/com.quicinc.cne.CNEService: 0% user + 0% kernel / faults: 7 minor
  0% 4118/com.google.android.googlequicksearchbox:search: 0% user + 0% kernel / faults: 15 minor
  0% 5010/com.facebook.orca: 0% user + 0% kernel / faults: 5 minor
  0% 5082/com.facebook.katana:notification: 0% user + 0% kernel / faults: 150 minor
  0% 7216/kworker/u17:6: 0% user + 0% kernel
  0% 15818/irq/504-synapti: 0% user + 0% 

FCMIDService class:

public class FCMIDService extends FirebaseInstanceIdService{

    private static final String TAG = "FCMIDService";

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    @Override
    public void onTokenRefresh() {
        CLog.d(TAG, "FCM id received");
        // Get updated InstanceID token.
        String token = FirebaseInstanceId.getInstance().getToken();
        //Do something with the token
        //...
    }

    @Override
    public void onCreate() {
        Log.d("atf", "on create here called");
        super.onCreate();
    }
}

According to Logs the ANR falls into FCMIDService class but that doesn’t make sense. When I debug the application the onCreate() method is never called. So how can the ANR fall in there if it never gets executed?

Another important notice:
When I rolled back the code to a previous working version (via SVN) the same issue persisted which makes me think that the repository of one of the libraries I use got changed.

EDIT2:
After a little bit of experimenting I found out that the problem is caused by Facebook integration.
When I removed the line

"implementation 'com.facebook.android:facebook-android-sdk:[4,5)'" 

and all of Java code related to it, the ANR disappeared.
I can’t resign on Facebook implementation so I replaced

"implementation 'com.facebook.android:facebook-android-sdk:[4,5)'" 

with

"api 'com.facebook.android:facebook-android-sdk:4.36.0'" 

acording to https://mvnrepository.com/artifact/com.facebook.android/facebook-android-sdk/3.19.0 . But that didn’t work.

Any ideas of a work around?

3

Answers


  1. Chosen as BEST ANSWER

    The problem laid in Facebook repository. I just changed one line of code in the app.gradle file: From

    implementation 'com.facebook.android:facebook-android-sdk:[4,5)'
    

    to

    api 'com.facebook.android:facebook-share:4.36.0'
    

    and that's it, no other changes.

    So here is what was happening: Since I was using a version range in dependency declaration [4,5) gradle would automatically update the Facebook library to it's latest version (currently 4.36.0). And Facebook did update their repository recently and that's when I started to have problems.

    The solution was to implement not their full sdk (com.facebook.android:facebook-android-sdk:[4,5) ) but just a part of it (com.facebook.android:facebook-share:4.36.0) and since I don't have a version range now, I shouldn't have any problems if Facebook decides to make an updates again.

    So apparently something in the latest Facebook library started to conflict with something, or they simply did a crappy job.


  2. It basically happens when you are blocking UI thread for too long. So please check your recent code where you have done something which blocks UI thread.For more info on ANRs you can visit:
    https://developer.android.com/topic/performance/vitals/anr

    Login or Signup to reply.
  3. do not put statements before calling to super.onCreate(); this is illegal and with high probability the reason why you get the ANR from a background Service, which barely might run on the UiThread – also, that FirebaseInstanceIdService is deprecated and therefore should be replaced altogether.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search