calcazar/react-native-push-notification-CE

Android build fails after installing (constructor Builder in class Builder cannot be applied to given types)

Closed this issue · 34 comments

Hello,

After following the instructions for installing, my build breaks when I try to build on android.

Command: react-native run-android

Error in build output:

node_modules/react-native-push-notification-ce/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java:162: error: constructor Builder in class Builder cannot be applied to given types;
            NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
                                                      ^
  required: Context
  found: Context,String
  reason: actual and formal argument lists differ in length
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error
:react-native-push-notification-ce:compileReleaseJavaWithJavac FAILED

Additional details:

  • I am running a debug build on a physical device (Nexus 5X)
  • I have tried installing the package in my project, as well as creating a fresh RN project and installing it there. I encounter the same error both times.
  • targetSdkVersion for both projects is 26.

Any recommendations would be greatly appreciated, thanks!

I did a little digging in the Android Developer docs, and it looks like this may be due to an old version of NotificationCompat.Builder being used:

"This constructor was deprecated in API level 26.1.0. use NotificationCompat.Builder(Context, String) instead. All posted Notifications must specify a NotificationChannel Id."

As of API level 26, NotificationCompat.Builder needs both context and string arguments, but the previous version only required Context.

I am using 26 for my project, but I also tried bumping the version up to 27 and still encountered the same error.

Here's a snippet of my app/build.gradle file:

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        applicationId: $MY APPLICATION ID
        minSdkVersion 26
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }

Thanks for the info @lindseymenges

I'll take a look at it this weekend!

@lindseymenges

can you share your dependencies please?

Thanks, @calcazar !

Here are my project's dependencies:

dependencies {
    compile project(':react-native-push-notification-ce')
    compile(project(':react-native-maps')){
      exclude group: 'com.google.android.gms'
    }
    compile (project(':react-native-device-info')){
    }
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:23.0.1"
    compile "com.facebook.react:react-native:+"  // From node_modules
    compile ("com.google.android.gms:play-services-base:10.0.1") {
      force = true;
    }
    compile ("com.google.android.gms:play-services-maps:10.0.1") {
      force = true;
    }
    compile ("com.google.android.gms:play-services-gcm:10.0.1") {
      force = true;
    }
}

As a note, I'm using the force compilations for play services due to some collisions between react-native-device-info and react-native-maps. Solution from this Medium post.

Try updating your appcompat to 26 as well

Nice catch! I overlooked appcompat while I was updating the versions.

Update: I've change appcompat to 26.0.1, and now I'm running into a new error:

:app:processDebugManifest
.../android/app/src/main/AndroidManifest.xml:28:13-35 Error:
        Attribute meta-data#android.support.VERSION@value value=(26.0.2) from [com.android.support:appcompat-v7:26.0.2] AndroidManifest.xml:28:13-35
        is also present at [com.android.support:support-v4:26.1.0] AndroidManifest.xml:28:13-35 value=(26.1.0).
        Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:38 to override.

See http://g.co/androidstudio/manifest-merger for more information about the manifest merger.

:app:processDebugManifest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(26.0.2) from [com.android.support:appcompat-v7:26.0.2] AndroidManifest.xml:28:13-35
        is also present at [com.android.support:support-v4:26.1.0] AndroidManifest.xml:28:13-35 value=(26.1.0).
        Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:38 to override.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

I will look some more this evening, but from my initial investigation it looks like these version collisions are both coming from react-native-push-notification-ce.
Thanks!

I continued investigating last night and this morning, but still no luck. I also tried creating a fresh RN project and only installing react-native-push-notification-ce to make sure there wasn't something else in my project causing this issue. I still ran into the same error, however.

.../TestProject/android/app/src/main/AndroidManifest.xml:28:13-35 Error:
        Attribute meta-data#android.support.VERSION@value value=(26.0.2) from [com.android.support:appcompat-v7:26.0.2] AndroidManifest.xml:28:13-35
        is also present at [com.android.support:support-v4:26.1.0] AndroidManifest.xml:28:13-35 value=(26.1.0).
        Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:38 to override.

See http://g.co/androidstudio/manifest-merger for more information about the manifest merger.

:app:processDebugManifest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(26.0.2) from [com.android.support:appcompat-v7:26.0.2] AndroidManifest.xml:28:13-35
        is also present at [com.android.support:support-v4:26.1.0] AndroidManifest.xml:28:13-35 value=(26.1.0).
        Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:38 to override.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

If anyone has any insight I would be very grateful. Thanks!

Im still digging in to this (fixing a bug at the moment but once that's done I'll switch gears and take a look)

@lindseymenges can you share your android manifest with me por favor?

@calcazar Sure thing! Here it is for my main project:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="MY PACKAGE NAME"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <permission
        android:name="com.spotting.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.$PROJECT_NAME.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
      android:name=".MainApplication"
      android:allowBackup="true"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:theme="@style/AppTheme">
      <receiver
          android:name="com.google.android.gms.gcm.GcmReceiver"
          android:exported="true"
          android:permission="com.google.android.c2dm.permission.SEND" >
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.RECEIVE" />
              <category android:name="${applicationId}" />
          </intent-filter>
      </receiver>

      <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
      <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
          <intent-filter>
              <action android:name="android.intent.action.BOOT_COMPLETED" />
          </intent-filter>
      </receiver>
      <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
      <service
          android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
          android:exported="false" >
          <intent-filter>
              <action android:name="com.google.android.c2dm.intent.RECEIVE" />
          </intent-filter>
      </service>      
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
      <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="MY API KEY"/>
    </application>

</manifest>

And here it is for the test project (hasn't been touched beyond react-native init and still gets the same error):

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>

</manifest>

I started getting build errors after installing this plugin to replace the former one react-native-push-notification. I moved to this one to see if the grouped messages are available now; but alas, a bigger surprise met me.

Are you seeing the same build errors as Lindsey?

@calcazar I wasn't seeing those verbose errors but build failed as follows:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugApk'.
   > Configuration with name 'default' not found.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 1 mins 18.492 secs
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html

Thank you for the info. I'll look in to this more this weekend. I do have it running on my machine with no build errors on a project.

That said, I have some time this weekend and I'll be spinning up a clean RN project and try to tackle some of the issues you guys have been having.

If you want to contribute and help out, please feel free :) most of my time is based on my FTE stuff but I'm trying to get this package updated and up and running :)

any solution?

I changed my build configuration of android to sdk 27 and solved my problem

Excellent!! I'll try looking on to the sdk 26 issue when I can

any update for this? I can't get it to work :(

Have you tried updating the sdk version to 27?

@calcazar thanks for your answer,
yes I updated to Version 27. My Android Studio says that my SDK Tools is at Version 26.1.1 and Platform Version is at API 27 revision 1. So I changed my app/build.gradle to the following:

android {
compileSdkVersion 27
buildToolsVersion "27.0.1"
defaultConfig {
applicationId "..."
minSdkVersion 16
targetSdkVersion 27
versionCode 16
versionName "0.1.0"
ndk {
abiFilters "armeabi-v7a", "x86"
}
}

however, I updated SDK Tools Support Repository in the Android SDK Manager, but my newest appcompat-v7 is still 26.0.0.-alpha1, and I cant find any Solution to upgrade it to 27+.

dependencies {
...
compile "com.android.support:appcompat-v7:26.0.0-alpha1"
compile "com.facebook.react:react-native:+" // From node_modules
}

Maybe this could be the cause?

@lindseymenges possible,

Replace your appcompat compile to:

'com.android.support:appcompat-v7:27.0.2'

and clean the project and rebuild

Thanks, @calcazar - I was working on another feature so I'll pivot back to this and see if switching to 27 solves my issue.

bumping issue +1

We are currently using 27:

dependencies {
    compile project(':react-native-push-notification-ce')
    compile project(':react-native-exception-handler')
    compile project(':react-native-svg')
    compile project(':react-native-linear-gradient')
    compile project(':react-native-device-settings')
    compile project(':react-native-app-auth')
    compile project(':react-native-config')
    compile project(':react-native-device-info')
    compile project(':react-native-linear-gradient')
    compile project(':react-native-maps')
    compile project(':react-native-mixpanel')
    compile project(':lottie-react-native')
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:27.1.1"
    compile "com.facebook.react:react-native:+"  // From node_modules
    compile('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
        transitive = true;
    }
}

Unfortunately, build is not fixed after updating to sdk 27.

Here's one interesting thing I've noticed: When I try excluding either 'com.google.android.gms' OR 'com.android.support', I get different error messages when my build fails.

When I have the following in build.gradle:

dependencies {
    compile(project(':react-native-push-notification-ce')){
        exclude group: 'com.google.android.gms'
    }
    ...
    compile "com.android.support:appcompat-v7:27.0.2"
    ...
}
    

I get this error in my build:

:app:compileDebugSources UP-TO-DATE
:app:transformClassesWithDexForDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat;

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

And when, following the above error, I exclude 'com.android.support', the following happens:

build.gradle:

dependencies {
    compile(project(':react-native-push-notification-ce')){
        exclude group: 'com.android.support'
    }
...
    compile "com.android.support:appcompat-v7:27.0.2"
...
}

build error:

:app:compileDebugSources
:app:transformClassesWithDexForDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/common/internal/zze;

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

I also tried excluding both groups:

dependencies {
    compile(project(':react-native-push-notification-ce')){
        exclude group: 'com.android.support'
        exclude group: 'com.google.android.gms'
    }
...
}

but I would get the error for whichever group I listed first. So in the above ^ example I got the following error:

:app:compileDebugSources
:app:transformClassesWithDexForDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat;

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

(As a side question, is this the correct syntax for excluding more than one group?)

I'm still unable to get the library to work with my project, but hopefully these additional details are helpful in narrowing down the cause(s). I will also test in the fresh project with 27 and see if anything else happens.

@lindseymenges I was able to resolve my issue by defaulting to the latest googlePlayServicesVersion:

ext {
    compileSdkVersion   = 26
    targetSdkVersion    = 26
    buildToolsVersion   = "26.0.2"
    supportLibVersion   = "26.1.0"
    googlePlayServicesVersion = "+"
    androidMapsUtilsVersion = "0.5+"
    firebaseVersion = "15.0.2"
}

it was too old:

ext {
    compileSdkVersion   = 26
    targetSdkVersion    = 26
    buildToolsVersion   = "26.0.2"
    supportLibVersion   = "26.1.0"
    googlePlayServicesVersion = "11.8.0"
    androidMapsUtilsVersion = "0.5+"
    firebaseVersion = "15.0.2"
}

Hope that helps you chase it down!

As I see, this repo is BROKEN and not usable as the original one...
I hate android so much!!!!!!!!

@cidevant

What are you seeing.. I'm using this library in prod at the moment... And others are using the other library in prod as well..

I do get your frustration though :)

same issue with me :(

Good news, I’ve got notifications working on Android! Here are the changes I have made that resulted in success:

AndroidManifest.xml

<manifest …>
…
	<uses-permission android:name="android.permission.VIBRATE" />
    	<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
…
	<application…>
		…
		<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
		<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
	           	<intent-filter>
		            	<action android:name="android.intent.action.BOOT_COMPLETED" />
	            	</intent-filter>
       		</receiver>
       		<service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
	           <service
	            android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
          		 android:exported="false" >
		            <intent-filter>
		                <action android:name="com.google.firebase.MESSAGING_EVENT" />
		            </intent-filter>
	          </service>
		…
	</application>
</manifest>

build.gradle (root level)

allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven {
            url "https://maven.google.com"
        }
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}

app/build.gradle

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"

    defaultConfig {
        applicationId “<PROJECT NAME>“
        minSdkVersion 26
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
…
	dependencies {
	    compile project(':react-native-push-notification-ce')
	    compile project(':react-native-mauron85-background-geolocation')
	    compile project(':react-native-device-info')
	    compile project(':react-native-maps')
	    compile fileTree(dir: "libs", include: ["*.jar"])
	    compile "com.android.support:appcompat-v7:26.1.0"
	    compile "com.facebook.react:react-native:+"  // From node_modules
	    compile ("com.google.android.gms:play-services-gcm:15.0.0")
	    compile ("com.google.android.gms:play-services-base:15.0.0")
	    compile ("com.google.android.gms:play-services-maps:15.0.0")
	}
…
}

Also, when I configure PushNotification, I included the senderID - without it, the onRegister event was never called for android (which you need if you want the token for remote notifications!). The necessity of the ID for remote notifications is outlined in this project’s readme, but I was unaware that onRegister wouldn’t be called at all if it wasn’t included.

I think the key piece was using the correct version of play services, as firebase is connected to that group. The correct firebase messaging version is 15.0.2, so I needed to ensure my project was using play services version 15.0.0.

Thank you so much, everyone, (especially @calcazar and @theonetheycallneo ) for your help with this issue! And hopefully the pieces I’ve included above will help anyone else who runs into a similar problem.

Excellent! Glad you're up and running @lindseymenges

I had the same issue and was able to solve it from @lindseymenges comment/info. @calcazar, all I did was create via react-native init [projectName], install your package, and then react-native run-android to run into this error -- not by any means a cluttered/old project. Would this warrant a readme refresh?

emuen commented

The project has an DEFAULT_SUPPORT_LIB_VERSION = "26.0.+", defined in build.gradle, which is the fallback if the depending project has not set a supportLibVersion. However, in order to use Builder(Context, String) it needs to be at least 26.1.0.

I think that including a newer version as the fallback would solve this issue.

I had this issue and solved it by adding the following to my build.gradle in <project_name>/android

ext {
    supportLibVersion = "26.1.0"
}

allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven {
            url 'https://maven.google.com'
        }
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}

thank you so much that work for me @emuen