aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

React Native - checkDebugDuplicateClasses error on Android #8389

Closed the-smart-home-maker closed 3 years ago

the-smart-home-maker commented 3 years ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Analytics, Push Notifications

Amplify Categories

analytics, notifications

Environment information

``` # Put output below this line System: OS: macOS 11.3.1 CPU: (6) x64 Intel(R) Core(TM) i5-8500B CPU @ 3.00GHz Memory: 50.54 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 15.0.1 - /usr/local/bin/node Yarn: 1.22.10 - /usr/local/bin/yarn npm: 7.0.3 - /usr/local/bin/npm Browsers: Chrome: 90.0.4430.212 Safari: 14.1 npmPackages: @aws-amplify/core: ^3.4.2 => 3.8.24 (4.0.3) @aws-amplify/pushnotification: ^4.0.3 => 4.0.3 @babel/core: ^7.9.6 => 7.14.3 @babel/preset-env: ^7.11.0 => 7.14.4 @babel/runtime: ^7.9.2 => 7.14.0 @nartc/react-native-barcode-mask: ^1.2.0 => 1.2.0 @react-native-async-storage/async-storage: ^1.15.5 => 1.15.5 @react-native-community/async-storage: ^1.12.1 => 1.12.1 @react-native-community/checkbox: ^0.5.5 => 0.5.8 @react-native-community/eslint-config: ^1.1.0 => 1.1.0 @react-native-community/masked-view: ^0.1.10 => 0.1.11 @react-native-community/netinfo: ^5.9.5 => 5.9.10 @react-native-community/push-notification-ios: ^1.5.0 => 1.8.0 (1.0.3) @react-native-community/toolbar-android: ^0.1.0-rc.2 => 0.1.0-rc.2 @react-navigation/bottom-tabs: ^5.5.2 => 5.11.11 @react-navigation/native: ^5.1.7 => 5.9.4 @react-navigation/stack: ^5.2.14 => 5.14.5 @sentry/react-native: ^2.3.0 => 2.4.3 HelloWorld: 0.0.1 amazon-cognito-identity-js: ^4.3.3 => 4.6.3 aws-amplify: ^3.3.2 => 3.4.3 aws-amplify-react-native: ^4.2.1 => 4.3.3 axios: ^0.20.0 => 0.20.0 (0.21.1) babel-eslint: ^10.1.0 => 10.1.0 babel-jest: ^25.5.1 => 25.5.1 babel-plugin-transform-remove-console: ^6.9.4 => 6.9.4 eslint: ^6.8.0 => 6.8.0 eslint-plugin-react: ^7.19.0 => 7.24.0 (7.19.0) eslint-plugin-react-native: ^3.8.1 => 3.11.0 (3.8.1) hermes-inspector-msggen: 1.0.0 husky: ^0.13.4 => 0.13.4 jest: ^25.5.4 => 25.5.4 lodash: ^4.17.20 => 4.17.21 metro-react-native-babel-preset: ^0.59.0 => 0.59.0 moment: ^2.29.1 => 2.29.1 native-base: ^2.13.12 => 2.15.2 react: 16.11.0 => 16.11.0 react-animated: 0.1.0 react-native: ^0.63.2 => 0.63.4 react-native-app-intro-slider: ^4.0.4 => 4.0.4 react-native-autocomplete-input: ^4.1.0 => 4.2.0 react-native-camera: ^3.23.1 => 3.44.0 react-native-circle-slider: ^2.7.0 => 2.9.0 react-native-collapsible: ^1.6.0 => 1.6.0 react-native-date-picker: ^3.2.3 => 3.3.1 react-native-dialog-input: ^1.0.8 => 1.0.8 react-native-dropdown-picker: ^3.0.4 => 3.8.3 react-native-elements: ^2.0.2 => 2.3.2 react-native-geolocation-service: ^5.0.0 => 5.2.0 react-native-gesture-handler: ^1.9.0 => 1.10.3 react-native-image-base64: ^0.1.4 => 0.1.4 react-native-image-resizer: ^1.4.2 => 1.4.4 react-native-interactable: ^2.0.1 => 2.0.1 react-native-keychain: ^6.0.0 => 6.2.0 react-native-languages: ^3.0.2 => 3.0.2 react-native-localization: ^2.1.6 => 2.1.7 react-native-localize: ^1.4.0 => 1.4.3 react-native-modal: ^11.5.6 => 11.10.0 react-native-popup-menu: ^0.15.9 => 0.15.10 react-native-reanimated: ^1.8.0 => 1.13.3 react-native-safe-area-context: ^3.1.9 => 3.2.0 react-native-screens: ^2.7.0 => 2.18.1 react-native-side-menu-updated: ^1.3.2 => 1.3.2 react-native-snap-carousel: ^3.9.1 => 3.9.1 react-native-splash-screen: ^3.2.0 => 3.2.0 react-native-svg: ^12.1.0 => 12.1.1 react-native-text-ticker: ^1.12.0 => 1.12.0 react-native-user-avatar: ^1.0.7 => 1.0.7 react-native-vector-icons: ^6.6.0 => 6.7.0 (7.1.0) react-native-version-info: ^1.1.0 => 1.1.0 react-native-webview: ^10.7.0 => 10.10.2 react-redux: ^7.2.0 => 7.2.4 react-test-renderer: 16.11.0 => 16.11.0 redux: ^4.0.5 => 4.1.0 redux-actions: ^2.6.5 => 2.6.5 redux-devtools-extension: ^2.13.8 => 2.13.9 redux-persist: ^6.0.0 => 6.0.0 redux-persist/integration/react: undefined () redux-promise: ^0.6.0 => 0.6.0 redux-thunk: ^2.3.0 => 2.3.0 victory-native: ^35.0.0 => 35.3.3 npmGlobalPackages: @aws-amplify/cli: 4.32.0 npm: 7.0.3 react-native-cli: 2.0.1 serverless: 2.10.0 ```

Describe the bug

I am trying to get amplify push notifications running on Android with React Native. Since I have integrated the package @aws-amplify/pushnotification, I can not get my Android app to build and run any more. The error message is:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:checkDebugDuplicateClasses'.
> 1 exception was raised by workers:
  java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class com.google.android.gms.gcm.PendingCallback found in modules jetified-firebase-jobdispatcher-0.6.0-runtime.jar (com.firebase:firebase-jobdispatcher:0.6.0) and jetified-play-services-gcm-16.1.0-runtime.jar (com.google.android.gms:play-services-gcm:16.1.0)
  Duplicate class com.google.android.gms.internal.measurement.zzu found in modules jetified-firebase-analytics-impl-16.2.4-runtime.jar (com.google.firebase:firebase-analytics-impl:16.2.4) and jetified-play-services-measurement-19.0.0-runtime.jar (com.google.android.gms:play-services-measurement:19.0.0)
  Duplicate class com.google.android.gms.internal.measurement.zzv found in modules jetified-firebase-analytics-impl-16.2.4-runtime.jar (com.google.firebase:firebase-analytics-impl:16.2.4) and jetified-play-services-measurement-19.0.0-runtime.jar (com.google.android.gms:play-services-measurement:19.0.0)
  Duplicate class com.google.android.gms.internal.measurement.zzw found in modules jetified-firebase-analytics-impl-16.2.4-runtime.jar (com.google.firebase:firebase-analytics-impl:16.2.4) and jetified-play-services-measurement-19.0.0-runtime.jar (com.google.android.gms:play-services-measurement:19.0.0)
  Duplicate class com.google.android.gms.measurement.AppMeasurement found in modules jetified-firebase-analytics-impl-16.2.4-runtime.jar (com.google.firebase:firebase-analytics-impl:16.2.4) and jetified-play-services-measurement-impl-19.0.0-runtime.jar (com.google.android.gms:play-services-measurement-impl:19.0.0)
...

There had been already a bug filed for this bug it had been closed without a resolution to the problem just because of inactivity: https://github.com/aws-amplify/amplify-js/issues/4593

Expected behavior

I can build and run my Android app as usual

Reproduction steps

  1. Follow the instructions https://docs.amplify.aws/lib/push-notifications/getting-started/q/platform/js for setting push notifications up on Android (alternatively also follow this guide for instructions about integrating it for React Native > 0.60 https://medium.com/@dantasfiles/setting-up-android-push-notifications-with-aws-amplify-e6334c6356d8)

Code Snippet

Just follow the guide https://medium.com/@dantasfiles/setting-up-android-push-notifications-with-aws-amplify-e6334c6356d8

Log output

I don't even get the app to run, so not debug output possible

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

Android

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

chrisbonifacio commented 3 years ago

@the-smart-home-maker It looks like you updated pushnotifications to v4 but not the aws-amplify package. There are breaking changes for React Native in v4 but nothing too crazy it seems.

https://github.com/aws-amplify/amplify-js#amplify-4xx-has-breaking-changes-for-react-native-please-see-the-breaking-changes-below

Could you try upgrading your other amplify packages and let me know if that helps?

chrisbonifacio commented 3 years ago

Oh, one other thing to note is that you probably don't need the separate @aws-amplify/core package if you already have aws-amplify installed.

the-smart-home-maker commented 3 years ago

Hi @chrisbonifacio,

thanks a lot for your support! I have upgraded all aws-amplify packages and removed the @aws-amplify/core package but unfortunately still receive the exact same error message.

Here my upgraded packages versions:

 npmPackages:
    @aws-amplify/pushnotification: ^4.0.3 => 4.0.3 
    @babel/core: ^7.9.6 => 7.14.3 
    @babel/preset-env: ^7.11.0 => 7.14.4 
    @babel/runtime: ^7.9.2 => 7.14.0 
    @nartc/react-native-barcode-mask: ^1.2.0 => 1.2.0 
    @react-native-async-storage/async-storage: ^1.15.5 => 1.15.5 
    @react-native-community/async-storage: ^1.12.1 => 1.12.1 
    @react-native-community/checkbox: ^0.5.5 => 0.5.8 
    @react-native-community/eslint-config: ^1.1.0 => 1.1.0 
    @react-native-community/masked-view: ^0.1.10 => 0.1.11 
    @react-native-community/netinfo: ^6.0.0 => 6.0.0 
    @react-native-community/push-notification-ios: ^1.5.0 => 1.8.0 (1.0.3)
    @react-native-community/toolbar-android: ^0.1.0-rc.2 => 0.1.0-rc.2 
    @react-navigation/bottom-tabs: ^5.5.2 => 5.11.11 
    @react-navigation/native: ^5.1.7 => 5.9.4 
    @react-navigation/stack: ^5.2.14 => 5.14.5 
    @sentry/react-native: ^2.3.0 => 2.4.3 
    HelloWorld:  0.0.1 
    amazon-cognito-identity-js: ^5.0.2 => 5.0.2 
    aws-amplify: 4.0.3 => 4.0.3 
    aws-amplify-react-native: 5.0.1 => 5.0.1 
    axios: ^0.20.0 => 0.20.0 (0.21.1)
    babel-eslint: ^10.1.0 => 10.1.0 
    babel-jest: ^25.5.1 => 25.5.1 
    babel-plugin-transform-remove-console: ^6.9.4 => 6.9.4 
    eslint: ^6.8.0 => 6.8.0 
    eslint-plugin-react: ^7.19.0 => 7.24.0 (7.19.0)
    eslint-plugin-react-native: ^3.8.1 => 3.11.0 (3.8.1)
    hermes-inspector-msggen:  1.0.0 
    husky: ^0.13.4 => 0.13.4 
    jest: ^25.5.4 => 25.5.4 
    lodash: ^4.17.20 => 4.17.21 
    metro-react-native-babel-preset: ^0.59.0 => 0.59.0 
    moment: ^2.29.1 => 2.29.1 
    native-base: ^2.13.12 => 2.15.2 
    react: 16.11.0 => 16.11.0 
    react-animated:  0.1.0 
    react-native: ^0.63.2 => 0.63.4 
    react-native-app-intro-slider: ^4.0.4 => 4.0.4 
    react-native-autocomplete-input: ^4.1.0 => 4.2.0 
    react-native-camera: ^3.23.1 => 3.44.0 
    react-native-circle-slider: ^2.7.0 => 2.9.0 
    react-native-collapsible: ^1.6.0 => 1.6.0 
    react-native-date-picker: ^3.2.3 => 3.3.1 
    react-native-dialog-input: ^1.0.8 => 1.0.8 
    react-native-dropdown-picker: ^3.0.4 => 3.8.3 
    react-native-elements: ^2.0.2 => 2.3.2 
    react-native-geolocation-service: ^5.0.0 => 5.2.0 
    react-native-gesture-handler: ^1.9.0 => 1.10.3 
    react-native-image-base64: ^0.1.4 => 0.1.4 
    react-native-image-resizer: ^1.4.2 => 1.4.4 
    react-native-interactable: ^2.0.1 => 2.0.1 
    react-native-keychain: ^6.0.0 => 6.2.0 
    react-native-languages: ^3.0.2 => 3.0.2 
    react-native-localization: ^2.1.6 => 2.1.7 
    react-native-localize: ^1.4.0 => 1.4.3 
    react-native-modal: ^11.5.6 => 11.10.0 
    react-native-popup-menu: ^0.15.9 => 0.15.10 
    react-native-reanimated: ^1.8.0 => 1.13.3 
    react-native-safe-area-context: ^3.1.9 => 3.2.0 
    react-native-screens: ^2.7.0 => 2.18.1 
    react-native-side-menu-updated: ^1.3.2 => 1.3.2 
    react-native-snap-carousel: ^3.9.1 => 3.9.1 
    react-native-splash-screen: ^3.2.0 => 3.2.0 
    react-native-svg: ^12.1.0 => 12.1.1 
    react-native-text-ticker: ^1.12.0 => 1.12.0 
    react-native-user-avatar: ^1.0.7 => 1.0.7 
    react-native-vector-icons: ^6.6.0 => 6.7.0 (7.1.0)
    react-native-version-info: ^1.1.0 => 1.1.0 
    react-native-webview: ^10.7.0 => 10.10.2 
    react-redux: ^7.2.0 => 7.2.4 
    react-test-renderer: 16.11.0 => 16.11.0 
    redux: ^4.0.5 => 4.1.0 
    redux-actions: ^2.6.5 => 2.6.5 
    redux-devtools-extension: ^2.13.8 => 2.13.9 
    redux-persist: ^6.0.0 => 6.0.0 
    redux-persist/integration/react:  undefined ()
    redux-promise: ^0.6.0 => 0.6.0 
    redux-thunk: ^2.3.0 => 2.3.0 
    victory-native: ^35.0.0 => 35.3.3 
  npmGlobalPackages:
    @aws-amplify/cli: 4.32.0
    npm: 7.0.3
    react-native-cli: 2.0.1
    serverless: 2.10.0
chrisbonifacio commented 3 years ago

Hmm, okay I will try to reproduce this on the latest versions as well so we can try to figure out the cause and find a fix for it. Thank you for trying!

the-smart-home-maker commented 3 years ago

Hey @chrisbonifacio,

Thanks for investigating! One more important information- on iOS, everything runs smoothly and I can get push notifications delivered as expected. It's just on Android that I experience problems.

Thank you and best regards The Smart Home Maker

the-smart-home-maker commented 3 years ago

Hi @chrisbonifacio,

Even further information - here's my android/app/build.gradle:

apply plugin: "com.android.application"

import com.android.build.OutputFile

project.ext.react = [
    enableHermes: true,  // clean and rebuild if changing
]

apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/@sentry/react-native/sentry.gradle"

/**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
def enableSeparateBuildPerCPUArchitecture = false

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = false

/**
 * The preferred build flavor of JavaScriptCore.
 *
 * For example, to use the international variant, you can use:
 * `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
 *
 * The international variant includes ICU i18n library and necessary data
 * allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
 * give correct results when using with locales other than en-US.  Note that
 * this variant is about 6MiB larger per architecture than default.
 */
def jscFlavor = 'org.webkit:android-jsc-intl:+'

/**
 * Whether to enable the Hermes VM.
 *
 * This should be set on project.ext.react and mirrored here.  If it is not set
 * on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
 * and the benefits of using Hermes will therefore be sharply reduced.
 */
def enableHermes = project.ext.react.get("enableHermes", false);

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId ...
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode ...
        versionName ...
    missingDimensionStrategy 'react-native-camera', 'general'
    manifestPlaceholders = [
            appAuthRedirectScheme: ...
    ]
        multiDexEnabled true
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro", "devsupport.pro"
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://facebook.github.io/react-native/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

    packagingOptions {
        pickFirst "lib/armeabi-v7a/libc++_shared.so"
        pickFirst "lib/arm64-v8a/libc++_shared.so"
        pickFirst "lib/x86/libc++_shared.so"
        pickFirst "lib/x86_64/libc++_shared.so"
    }

    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // https://developer.android.com/studio/build/configure-apk-splits.html
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }

        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules

    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"

    // Import the BoM for the Firebase platform
    implementation platform('com.google.firebase:firebase-bom:28.0.1')

    // Declare the dependencies for the Firebase Cloud Messaging and Analytics libraries
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation 'com.google.firebase:firebase-messaging' 
    implementation 'com.google.firebase:firebase-analytics'

    implementation "com.google.android.gms:play-services-gcm:17.0.0"

    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
      exclude group:'com.facebook.fbjni'
    }

    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }

    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }

     implementation(project(':aws-amplify_pushnotification'), {}) 

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }

    // Multidex
    implementation 'com.android.support:multidex:1.0.3'

    configurations.all {
        resolutionStrategy {
            force 'com.google.android.gms:play-services-gcm:16.1.0'
            force 'com.google.android.gms:play-services-base:16.1.0'
            force 'com.google.firebase:firebase-core:16.0.6'
            force 'com.google.firebase:firebase-messaging:17.6.0'
        }
    }

}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"

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

where ... are anonymizations.

Also I have created a dependency tree for maybe more information about the dependencies for com.firebase:firebase-jobdispatcher and com.google.android.gms:play-services-gcm (just picked the compilation variant for debug exemplarily):

...
debugCompileClasspath - Resolved configuration for compilation for variant: debug
...
+--- com.google.android.gms:play-services-gcm:17.0.0 -> 16.1.0
...
+--- project :aws-amplify_pushnotification
...
|    \--- com.firebase:firebase-jobdispatcher:0.6.0
...
+--- com.google.android.gms:play-services-gcm:{strictly 16.1.0} -> 16.1.0 (c)
...
+--- com.firebase:firebase-jobdispatcher:{strictly 0.6.0} -> 0.6.0 (c)

Maybe that helps as well?

chrisbonifacio commented 3 years ago

I tried creating a React Native project with this package.json and it was able to build. Could you try deleting your node_modules folder, reinstalling your dependencies and see if it's able to build then?

This is my package.json, using latest versions of amplify packages as well

{
  "name": "android8389",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@aws-amplify/pushnotification": "^4.0.3",
    "@react-native-async-storage/async-storage": "^1.15.5",
    "@react-native-community/netinfo": "^6.0.0",
    "amazon-cognito-identity-js": "^5.0.2",
    "aws-amplify": "^4.0.3",
    "aws-amplify-react-native": "^5.0.1",
    "react": "17.0.1",
    "react-native": "0.64.2"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@babel/runtime": "^7.12.5",
    "@react-native-community/eslint-config": "^2.0.0",
    "babel-jest": "^26.6.3",
    "eslint": "7.14.0",
    "jest": "^26.6.3",
    "metro-react-native-babel-preset": "^0.64.0",
    "react-test-renderer": "17.0.1"
  },
  "jest": {
    "preset": "react-native"
  }
}

my build.gradle file

apply plugin: "com.android.application"

import com.android.build.OutputFile

project.ext.react = [
    enableHermes: false,  // clean and rebuild if changing
]

apply from: "../../node_modules/react-native/react.gradle"

/**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
def enableSeparateBuildPerCPUArchitecture = false

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = false

/**
 * The preferred build flavor of JavaScriptCore.
 *
 * For example, to use the international variant, you can use:
 * `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
 *
 * The international variant includes ICU i18n library and necessary data
 * allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
 * give correct results when using with locales other than en-US.  Note that
 * this variant is about 6MiB larger per architecture than default.
 */
def jscFlavor = 'org.webkit:android-jsc:+'

/**
 * Whether to enable the Hermes VM.
 *
 * This should be set on project.ext.react and mirrored here.  If it is not set
 * on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
 * and the benefits of using Hermes will therefore be sharply reduced.
 */
def enableHermes = project.ext.react.get("enableHermes", false);

android {
    ndkVersion rootProject.ext.ndkVersion

    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "com.android8389"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://reactnative.dev/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // https://developer.android.com/studio/build/configure-apk-splits.html
            // Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        defaultConfig.versionCode * 1000 + versionCodes.get(abi)
            }

        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules

    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"

    implementation "com.google.firebase:firebase-core:15.0.2"
    implementation "com.google.firebase:firebase-messaging:15.0.2"

    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
      exclude group:'com.facebook.fbjni'
    }

    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
        exclude group:'com.squareup.okhttp3', module:'okhttp'
    }

    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

apply plugin: "com.google.gms.google-services"
chrisbonifacio commented 3 years ago

Here's a link to my project on github in case it helps

https://github.com/chrisbonifacio/amplify-issues/tree/dev/android8389

the-smart-home-maker commented 3 years ago

Dear @chrisbonifacio ,

thank you so much - given your app/build.gradle I was able to solve the problem!

I am pretty convinced that the following change did the trick.

In my app/build.gradle I had the following definitions related to firebase and play-services:

// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:28.0.1')

// Declare the dependencies for the Firebase Cloud Messaging and Analytics libraries
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-analytics'

implementation "com.google.android.gms:play-services-gcm:17.0.0"
...
configurations.all {
        resolutionStrategy {
            force 'com.google.android.gms:play-services-gcm:16.1.0'
            force 'com.google.android.gms:play-services-base:16.1.0'
            force 'com.google.firebase:firebase-core:16.0.6'
            force 'com.google.firebase:firebase-messaging:17.6.0'
        }
    }

I had taken those definitions from different resources explaining how to integrate aws amplify push notifications and analytics.

In your app/build.gradle there were only the following definitions related to firebase:

implementation "com.google.firebase:firebase-core:15.0.2"
implementation "com.google.firebase:firebase-messaging:15.0.2"

After replacing all of my definitions shown above with just the two lines from your definition, I was able to build again. Still, I am not able to receive push notifications on Android devices but this is a different issue.

This ticket can be closed.

Thanks a lot and best regards The Smart Home Maker

chrisbonifacio commented 3 years ago

Awesome! Glad I could help. Have a great day 😄

Mavennix commented 3 years ago

I still have this issue. Here is my build.graddle


apply from: '../../node_modules/react-native-unimodules/gradle.groovy'
import com.android.build.OutputFile

/**
 * The react.gradle file registers a task `for` each build variant (e.g. bundleDebugJsAndAssets
 * and bundleReleaseJsAndAssets).
 * These basically call `react-native bundle` with the correct arguments during the Android build
 * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
 * bundle directly from the development server. Below you can see all the possible configurations
 * and their defaults. If you decide to add a configuration block, make sure to add it before the
 * `apply from: "../../node_modules/react-native/react.gradle"` line.
 *
 * project.ext.react = [
 *   // the name of the generated asset file containing your JS bundle
 *   bundleAssetName: "index.android.bundle",
 *
 *   // the entry file for bundle generation. If none specified and
 *   // "index.android.js" exists, it will be used. Otherwise "index.js" is
 *   // default. Can be overridden with ENTRY_FILE environment variable.
 *   entryFile: "index.android.js",
 *
 *   // https://reactnative.dev/docs/performance#enable-the-ram-format
 *   bundleCommand: "ram-bundle",
 *
 *   // whether to bundle JS and assets in debug mode
 *   bundleInDebug: false,
 *
 *   // whether to bundle JS and assets in release mode
 *   bundleInRelease: true,
 *
 *   // whether to bundle JS and assets in another build variant (if configured).
 *   // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
 *   // The configuration property can be in the following formats
 *   //         'bundleIn${productFlavor}${buildType}'
 *   //         'bundleIn${buildType}'
 *   // bundleInFreeDebug: true,
 *   // bundleInPaidRelease: true,
 *   // bundleInBeta: true,
 *
 *   // whether to disable dev mode in custom build variants (by default only disabled in release)
 *   // for example: to disable dev mode in the staging build type (if configured)
 *   devDisabledInStaging: true,
 *   // The configuration property can be in the following formats
 *   //         'devDisabledIn${productFlavor}${buildType}'
 *   //         'devDisabledIn${buildType}'
 *
 *   // the root of your project, i.e. where "package.json" lives
 *   root: "../../",
 *
 *   // where to put the JS bundle asset in debug mode
 *   jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
 *
 *   // where to put the JS bundle asset in release mode
 *   jsBundleDirRelease: "$buildDir/intermediates/assets/release",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in debug mode
 *   resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in release mode
 *   resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
 *
 *   // by default the gradle tasks are skipped if none of the JS files or assets change; this means
 *   // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
 *   // date; if you have any other folders that you want to ignore for performance reasons (gradle
 *   // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
 *   // for example, you might want to remove it from here.
 *   inputExcludes: ["android/**", "ios/**"],
 *
 *   // override which node gets called and with what additional arguments
 *   nodeExecutableAndArgs: ["node"],
 *
 *   // supply additional arguments to the packager
 *   extraPackagerArgs: []
 * ]
 */

project.ext.react = [
    enableHermes: false,  // clean and rebuild if changing
]

apply from: "../../node_modules/react-native/react.gradle"

/**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
def enableSeparateBuildPerCPUArchitecture = false

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = false

/**
 * The preferred build flavor of JavaScriptCore.
 *
 * For example, to use the international variant, you can use:
 * `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
 *
 * The international variant includes ICU i18n library and necessary data
 * allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
 * give correct results when using with locales other than en-US.  Note that
 * this variant is about 6MiB larger per architecture than default.
 */
// def jscFlavor = 'org.webkit:android-jsc:+'
def jscFlavor = 'org.webkit:android-jsc-intl:+'

/**
 * Whether to enable the Hermes VM.
 *
 * This should be set on project.ext.react and mirrored here.  If it is not set
 * on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
 * and the benefits of using Hermes will therefore be sharply reduced.
 */
def enableHermes = project.ext.react.get("enableHermes", false);

android {
    ndkVersion rootProject.ext.ndkVersion

    compileSdkVersion rootProject.ext.compileSdkVersion
    aaptOptions {
    cruncherEnabled = false
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId ""
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 16
        versionName "1.1.2"
        missingDimensionStrategy 'react-native-camera', 'general'

    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://reactnative.dev/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // https://developer.android.com/studio/build/configure-apk-splits.html
            // Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        defaultConfig.versionCode * 1000 + versionCodes.get(abi)
            }

        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation "com.google.firebase:firebase-core:15.0.2"
    implementation "com.google.firebase:firebase-messaging:15.0.2"

    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"

    addUnimodulesDependencies()

    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
      exclude group:'com.facebook.fbjni'
    }

    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
        exclude group:'com.squareup.okhttp3', module:'okhttp'
    }

    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
apply plugin: "com.google.gms.google-services"
github-actions[bot] commented 2 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.