Skip to content
Advertisement

HMS NoClassDefFoundError Android

I’m using HMS plugin for Android Studio.

I have a class called MyFirebaseMessagingService which receives notifications from Firebase.

it used to be like :

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
...
}

after conversion:

import org.xms.f.messaging.ExtensionMessagingService;
import org.xms.f.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends ExtensionMessagingService {
...
}

After converting the code, I had the following problem :

 Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/xms/f/messaging/ExtensionMessagingService


 Caused by: java.lang.ClassNotFoundException: Didn't find class "org.xms.f.messaging.ExtensionMessagingService" on path: DexPathList

I found a suggested solution in Huawei support, but it didn’t work!

(Handle the exception) link : https://developer.huawei.com/consumer/en/doc/development/Tools-Guides/map-conversion-0000001050062227

Suggested solution :

Add the following configuration to app/build.gradle:

android {
    defaultConfig {
        multiDexEnabled false
    }
}

btw I already clean and rebuild the project. Also, I tried to restart and invalidate.

app/build.gradle. Before conversion:

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.marsa.store"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 24
        versionName "2.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        signingConfig signingConfigs.signing_configs
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.firebase:firebase-messaging:21.1.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    //ext lib
    // Retrofit & OkHttp
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    //side menu
    implementation 'androidx.navigation:navigation-fragment:2.3.4'
    implementation 'androidx.navigation:navigation-ui:2.3.4'
    //glide
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    //circle-img
    implementation 'de.hdodenhof:circleimageview:3.1.0'


    implementation 'com.google.maps.android:android-maps-utils:0.5'
    implementation 'com.google.android.gms:play-services-location:18.0.0'

    implementation 'com.github.zcweng:switch-button:0.0.3@aar'


    // implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    //volley + gson
//    implementation 'com.android.volley:volley:1.2.0'
    implementation 'com.google.code.gson:gson:2.8.6'


    //finger + face recognition
    def biometric_version = '1.0.0'
    implementation "androidx.biometric:biometric:$biometric_version"


    //fcm
    implementation 'com.google.firebase:firebase-messaging-directboot:22.0.0'

    //country picker
    implementation 'com.hbb20:ccp:2.5.4'

}

    

build.gradle (:xmsadapter),. After conversion:

apply plugin: 'com.android.library'
apply plugin: 'com.huawei.agconnect'
apply from: 'scripts/productFlavor.gradle'

android {
    compileSdkVersion 30
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 30
    }

    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }

}

dependencies {
    //Basement
    compileOnly 'com.google.android.gms:play-services-base:17.1.0'
    compileOnly 'com.huawei.hms:base:6.0.1.302'
    compileOnly 'com.google.android.gms:play-services-basement:17.1.0'
    compileOnly 'com.google.android.gms:play-services-tasks:17.0.0'
    //Push
    compileOnly 'com.google.android.gms:play-services-gcm:17.0.0'
    compileOnly 'com.huawei.hms:push:5.3.0.304'
    compileOnly 'com.google.firebase:firebase-messaging:20.2.0'
    compileOnly 'com.huawei.hms:hianalytics:6.1.0.300'
    compileOnly 'com.google.firebase:firebase-core:17.0.0'
    //Map
    compileOnly 'com.google.android.gms:play-services-maps:17.0.0'
    compileOnly 'com.huawei.hms:maps:5.3.0.300'
    compileOnly 'com.huawei.hms:location:6.0.0.302'
    compileOnly 'com.google.android.gms:play-services-location:17.0.0'
    compileOnly 'com.google.android.material:material:1.0.0-rc01'
}

stack trace :

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/xms/f/messaging/ExtensionMessagingService;
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "org.xms.f.messaging.ExtensionMessagingService" on path: DexPathList[[zip file "/data/app/~~A2nKXMdxonSKg_WgJRMT_Q==/com.marsa.store-vyoa6CNMY3Bwny01O9S41w==/base.apk"],nativeLibraryDirectories=[/data/app/~~A2nKXMdxonSKg_WgJRMT_Q==/com.marsa.store-vyoa6CNMY3Bwny01O9S41w==/lib/x86, /data/app/~~A2nKXMdxonSKg_WgJRMT_Q==/com.marsa.store-vyoa6CNMY3Bwny01O9S41w==/base.apk!/lib/x86, /system/lib, /system_ext/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at java.lang.Class.newInstance(Native Method) 
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95) 
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45) 
        at android.app.Instrumentation.newActivity(Instrumentation.java:1253) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

Advertisement

Answer

After trying almost everything I solved the problem. In order to help as many people as possible, I will write the steps you need to follow to get HMS map working :

  1. Create Huawei developer account: https://id5.cloud.huawei.com/CAS/portal/userRegister/regbyemail.html

  2. Create a project and an app in Huawei Developer console, then get agconnect-service.json (After downloading make sure it is .json) file: https://developer.huawei.com/consumer/cn/service/josp/agc/index.html#/

you can watch this video to help you: https://www.youtube.com/watch?v=BQbFlP4gRN8&t=1s

  1. Download and install HMS converter plugin from Android Market place. The plugin will ask you to restart Android Studio.

  2. Before converting the project make sure to create a backup locally or on Github.

  3. The following video will help you understand how to convert google APIs. Important note: @5:50 in the video the instructor will choose "To HMS API", You need to select "Add HMS API (GMS API First)". Also, @6:53 in the video the instructor will click on automatic convert for all files. From a Personal Experience, it is better to convert files one by one manually. Actually, auto-convert is the cause of the issue I encountered! https://www.youtube.com/watch?v=1b1Ap5xQHm4&t=357s

  4. Make sure HMS converter added the following lines to your build:gradle/Project:

     buildscript {
     repositories {
     ...
    
    // added by hms converter
    maven {url 'https://developer.huawei.com/repo/'}
    }
    
    dependencies {
     ...
    
     // added by hms converter
     classpath 'com.huawei.agconnect:agconnect-apms-plugin:1.4.1.305'
     classpath 'com.huawei.agconnect:agcp:1.2.1.301'
      }
    }
    
    allprojects {
     repositories {
     ...
    
     // added by hms converter
     maven {url 'https://developer.huawei.com/repo/'}
      }
     }
    
  5. Make sure HMS converter added the following lines to your build:gradle/App:

     android {
     ...
     compileOptions {
         sourceCompatibility JavaVersion.VERSION_1_8
         targetCompatibility JavaVersion.VERSION_1_8
      }
    }
    
    
    
     dependencies {
     // added by hms converter
     implementation project(':xmsadapter')
     implementation project(':xmsadapter:xmsaux')
     compileOnly project(':xmsadapter:xmsaux:xapi')
    
     ...
    
     //added by hms converter
     implementation 'com.huawei.hms:maps:5.3.0.300'
     implementation 'com.huawei.hms:location:6.0.0.302'
    
    
    }
    
    //added by hms converter
    apply plugin: 'com.huawei.agconnect'
    apply from: '../xmsadapter/scripts/productFlavor.gradle'
    
  6. Make sure HMS converter added the following lines to your Settings.gradle:

    include ':app'
    ...
    // added by hms converter
    include ':xmsadapter', ':xmsadapter:xmsaux', ':xmsadapter:xmsaux:xg', ':xmsadapter:xmsaux:xh', ':xmsadapter:xmsaux:xapi'
    
  7. Add the following rules to proguard-rules.pro in case minifyEnabled is true:

        #<HMS Rules>
        -ignorewarnings
        -keepattributes *Annotation*
        -keepattributes Exceptions
        -keepattributes InnerClasses
        -keepattributes Signature
        -keepattributes SourceFile,LineNumberTable
        -keep class com.hianalytics.android.**{*;}
        -keep class com.huawei.updatesdk.**{*;}
        -keep class com.huawei.hms.**{*;}
        -keep class com.huawei.agconnect.**{*;}
        -keep interface com.huawei.agconnect.**{*;}
        -keep class com.huawei.hmf.**{*;}
    
        -keep class com.google.android.gms.**{*;}
        -keep interface com.google.android.gms.**{*;}
    
        -keep class com.google.firebase.**{*;}
        -keep interface com.google.firebase.**{*;}
    
        -keep class org.xms.**{*;}
        -keep interface org.xms.**{*;}
    
        #for map
        -keep class * extends androidx.fragment.app.Fragment{*;}
        -keep class org.xms.g.maps.ExtensionMapOptions{*;}
        -keep class com.huawei.hms.maps.HuaweiMapOptions{*;}
        -keep class com.google.android.gms.maps.GoogleMapOptions{*;}
        -keep interface org.xms.g.maps.OnMapReadyCallback{*;}
        -keep interface com.huawei.hms.maps.OnMapReadyCallback{*;}
        -keep interface com.google.android.gms.maps.OnMapReadyCallback{*;}
        -keep class org.xms.g.maps.StreetViewPanoramaOptions{*;}
        -keep class com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback{*;}
        -keep class com.huawei.hms.maps.OnStreetViewPanoramaReadyCallback{*;}
        -keep interface org.xms.g.maps.OnStreetViewPanoramaReadyCallback{*;}
        -keep interface com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback{*;}
        -keep interface com.huawei.hms.maps.OnStreetViewPanoramaReadyCallback{*;}
        -keep class com.huawei.hms.dynamicloader.** { *; }
        -keep class com.huawei.hms.feature.dynamic.** {*;}
        -keep class com.huawei.hms.feature.** { *;}
    
    
    
        #</HMS Rules>
    
  8. Clean then Rebuild the project, you may need to “restart and invalidate” Android studio.

  9. you can check this github project as a reference: https://github.com/AlineJo/AdapterMap

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement