HMS NoClassDefFoundError Android

HMS NoClassDefFoundError Android

我正在为 Android Studio 使用 HMS 插件。

我有一个名为 MyFirebaseMessagingService 的 class,它接收来自 Firebase 的通知。

以前是这样的:

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

public class MyFirebaseMessagingService extends FirebaseMessagingService {
...
}

转换后:

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

public class MyFirebaseMessagingService extends ExtensionMessagingService {
...
}

转换代码后,我遇到了以下问题:

 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

我在华为支持中找到了建议的解决方案,但没有用!

(处理异常) link : https://developer.huawei.com/consumer/en/doc/development/Tools-Guides/map-conversion-0000001050062227

建议的解决方案:

将以下配置添加到app/build。gradle:

android {
    defaultConfig {
        multiDexEnabled false
    }
}

顺便说一句,我已经清理并重建了项目。另外,我试过重启无效。

app/build.gradle。转换前:

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),.转换后:

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'
}

堆栈跟踪:

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) 

可以参考这个Docs:

如果出现诸如“无法找到...”之类的错误消息“NoClassDefFoundError” "ClassNotFoundException", "NoSuchFieldException", or "NoSuchMethodException"minifyEnabled[=24=之后显示]设置为true,在app/proguard-rules.pro中配置如下XMS混淆规则:

-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.**{*;}

在尝试了几乎所有方法后,我解决了问题。为了帮助尽可能多的人,我将写下让 HMS 地图正常工作所需遵循的步骤:

  1. 创建华为开发者账号https://id5.cloud.huawei.com/CAS/portal/userRegister/regbyemail.html

  2. 在华为开发者控制台创建项目和应用,然后得到agconnect-service.json(下载后确保是.json)文件: https://developer.huawei.com/consumer/cn/service/josp/agc/index.html#/

您可以观看此视频来帮助您: https://www.youtube.com/watch?v=BQbFlP4gRN8&t=1s

  1. 从 Android 市场 下载并安装 HMS 转换器插件。该插件将要求您重新启动 Android Studio。

  2. 在转换项目之前确保在本地或在 Github.

    上创建备份
  3. 以下视频将帮助您了解如何转换 google API重要说明@5:50 in the video the instructor will choose "To HMS API", You need to select "Add HMS API (GMS API First)"。 另外,@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. 其实,自动转换是我遇到问题的原因https://www.youtube.com/watch?v=1b1Ap5xQHm4&t=357s

  4. 确保 HMS 转换器将以下行添加到您的 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. 确保 HMS 转换器将以下行添加到您的 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. 确保 HMS 转换器将以下行添加到您的 Settings.gradle:

    include ':app'
    ...
    // added by hms converter
    include ':xmsadapter', ':xmsadapter:xmsaux', ':xmsadapter:xmsaux:xg', ':xmsadapter:xmsaux:xh', ':xmsadapter:xmsaux:xapi'
    
  7. 如果 minifyEnabled 为真,则将以下规则添加到 proguard-rules.pro:

        #<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,你可能需要“restart and invalidate”Android studio.

  9. 你可以查看这个github项目作为参考https://github.com/AlineJo/AdapterMap