0
votes

I am having trouble building my ionic 4 apps. Every time I try to build the android app it gives the following error.

FAILURE: Build failed with an exception.

* What went wrong: Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:   Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes. Program type already present: android.support.v4.os.ResultReceiver$MyResultReceiver

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

* Get more help at https://help.gradle.org

BUILD FAILED in 19s
> Task :app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED 36 actionable tasks: 2 executed, 34 up-to-date F:\WorkSpace\Ionic Programs\Project\sujagSindhiSamiti\platforms\android\gradlew: Command failed with exit code 1 Error output: D8: Program type already present: android.support.v4.os.ResultReceiver$MyResultReceiver

FAILURE: Build failed with an exception.

* What went wrong: Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:   Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes. Program type already present: android.support.v4.os.ResultReceiver$MyResultReceiver

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

* Get more help at https://help.gradle.org

BUILD FAILED in 19s [ERROR] An error occurred while running subprocess cordova.

        cordova.cmd build android exited with exit code 1.

        Re-running this command with the --verbose flag may provide more information. 

I have followed instructions for push notification: https://ionicframework.com/docs/native/fcm

I have followed instructions for verifying phone number: https://ionicframework.com/docs/native/firebase-authentication

After adding a plugin for firebase-authentication while running app error occurs.

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { HttpClientModule } from '@angular/common/http';
import { Device } from '@ionic-native/device/ngx';
import { CallNumber } from '@ionic-native/call-number/ngx';
import { Contacts } from '@ionic-native/contacts/ngx';
import { SocialSharing } from '@ionic-native/social-sharing/ngx';
import { File } from '@ionic-native/file/ngx';
import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer/ngx';
import { FilePath } from '@ionic-native/file-path/ngx';
import { FileChooser } from '@ionic-native/file-chooser/ngx';
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';
import { FCM } from '@ionic-native/fcm/ngx';
import { FirebaseAuthentication } from '@ionic-native/firebase-authentication/ngx';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, HttpClientModule],
  providers: [
    StatusBar,
    SplashScreen,
    Device,
    CallNumber,
    Contacts,
    SocialSharing,
    File,
    FileTransfer,
    FileTransferObject,
    FileChooser,
    FilePath,
    InAppBrowser,
    FCM,
    FirebaseAuthentication,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

login.ts

import { Device } from '@ionic-native/device/ngx';
import { FCM } from '@ionic-native/fcm/ngx';
import { FirebaseAuthentication } from '@ionic-native/firebase-authentication/ngx';

constructor(private router: Router, public platform: Platform, private device: Device,private fcm: FCM, private firebaseAuthentication: FirebaseAuthentication) { 
this.platform.ready()
    .then(() => {
      this.fcm.onNotification().subscribe(data => {
        if (data.wasTapped) {
          console.log("Received in background");
        } else {
          console.log("Received in foreground");
        };
      });


      this.fcm.onTokenRefresh().subscribe(token => {
        alert(token);
      });
    })
  }

subscribeToTopic() {
    this.fcm.subscribeToTopic('enappd');
  }
  getToken() {
    this.fcm.getToken().then(token => {
      alert(token);
      this.userData.fcm_id = token;
    });
  }
  unsubscribeFromTopic() {
    this.fcm.unsubscribeFromTopic('enappd');
  }

login(){
    this.userData.imei_no = this.device.uuid;

    this.fcm.getToken().then(token => {
      alert('FCM Token: ' + token);
      this.userData.fcm_id = token;
    });

    this.firebaseAuthentication.verifyPhoneNumber("+919824592145", 30000).then((verificationID) => {
      console.log(verificationID);
      this.verificationID = verificationID;
      alert(verificationID);
    }).catch((error) => {
        console.log(error);
        alert(error);
    });
}
4

4 Answers

11
votes
ionic cordova plugin add cordova-plugin-androidx
ionic cordova plugin add cordova-plugin-androidx-adapter

Simply adding these plugins removed any build error.

0
votes

I had the same problem. This solved it for me.

  1. clean [ from terminal cordova clean ]
  2. rebuild [ from terminal ionic cordova build android]

Or

Remove platform and add it again

0
votes

I was facing the same issue in my ionic app and after trying everything suggested by the best developer in StackOverflow nothing worked. Then I tried this and it works like a charm

Ionic repair

0
votes

The solution proposed here works for me.

  • Remove android platform

    ionic cordova platform rm android

  • Remove plugins related to support, gradle and/or firebase versions control. In my case it was a little bit weird, I removed the local-notifications plugin.

  • Add androidX plugins

    cordova plugin add cordova-plugin-androidx

    cordova plugin add cordova-plugin-androidx-adapter