Android 安卓工作室-Can';t从libs文件夹(或mipmap文件夹)中的某个_lib.aar文件加载资源

Android 安卓工作室-Can';t从libs文件夹(或mipmap文件夹)中的某个_lib.aar文件加载资源,android,android-studio,gradle,Android,Android Studio,Gradle,我搜索了这个问题,没有找到任何与我的问题相关的答案。我希望有人处理这个问题之前,可以帮助我(-:) 我有一个android studio项目,在“libs”文件夹中有一个 aar库(例如:some_lib.aar) 在aar库中,我有“mipmap”和“raw”文件夹 内部资源(“mipmap”有PNG,“raw”有声音 文件名为:sound.wav) 在我的项目中,我试图从mipmap和 “原始”文件夹中的声音文件,如下所示: 我有这样一个例外(也适用于“mipmap”文件夹): 例外

我搜索了这个问题,没有找到任何与我的问题相关的答案。我希望有人处理这个问题之前,可以帮助我(-:)

  • 我有一个android studio项目,在“libs”文件夹中有一个
    aar库(例如:some_lib.aar)
  • 在aar库中,我有“mipmap”和“raw”文件夹 内部资源(“mipmap”有PNG,“raw”有声音 文件名为:sound.wav)
  • 在我的项目中,我试图从mipmap和 “原始”文件夹中的声音文件,如下所示:




  • 我有这样一个例外(也适用于“mipmap”文件夹):
  • 例外情况

    Process: com.growmobile.engagement.sample, PID: 20883
            java.lang.NoClassDefFoundError: Failed resolution of: Lcom/growmobile/engagement/R$raw;
            at com.growmobile.engagement.ModelPushMessage.loadSoundFromLocal(ModelPushMessage.java:451)
            at com.growmobile.engagement.ModelPushMessage.parseMessageData(ModelPushMessage.java:375)
            at com.growmobile.engagement.ModelPushMessage.<init>(ModelPushMessage.java:174)
            at com.growmobile.engagement.ManagerNotification.buildPushMessageObjectAndSaveItToDB(ManagerNotification.java:139)
            at com.growmobile.engagement.ManagerNotification.buildGMENotificationObject(ManagerNotification.java:108)
            at com.growmobile.engagement.ManagerNotification.initializeNotification(ManagerNotification.java:77)
            at com.growmobile.engagement.GMEGcmListenerService.executePushAction(GMEGcmListenerService.java:152)
            at com.growmobile.engagement.GMEGcmListenerService.handleMessage(GMEGcmListenerService.java:118)
            at com.growmobile.engagement.GMEGcmListenerService.onMessageReceived(GMEGcmListenerService.java:79)
            at com.google.android.gms.gcm.GcmListenerService.zzq(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zzp(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)
            Caused by: java.lang.ClassNotFoundException: Didn't find class "com.growmobile.engagement.R$raw" on path: DexPathList[[zip file "/data/app/com.growmobile.engagement.sample-1/base.apk"],nativeLibraryDirectories=[/data/app/com.growmobile.engagement.sample-1/lib/arm64, /vendor/lib64, /system/lib64]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at com.growmobile.engagement.ModelPushMessage.loadSoundFromLocal(ModelPushMessage.java:451)
            at com.growmobile.engagement.ModelPushMessage.parseMessageData(ModelPushMessage.java:375)
            at com.growmobile.engagement.ModelPushMessage.<init>(ModelPushMessage.java:174)
            at com.growmobile.engagement.ManagerNotification.buildPushMessageObjectAndSaveItToDB(ManagerNotification.java:139)
            at com.growmobile.engagement.ManagerNotification.buildGMENotificationObject(ManagerNotification.java:108)
            at com.growmobile.engagement.ManagerNotification.initializeNotification(ManagerNotification.java:77)
            at com.growmobile.engagement.GMEGcmListenerService.executePushAction(GMEGcmListenerService.java:152)
            at com.growmobile.engagement.GMEGcmListenerService.handleMessage(GMEGcmListenerService.java:118)
            at com.growmobile.engagement.GMEGcmListenerService.onMessageReceived(GMEGcmListenerService.java:79)
            at com.google.android.gms.gcm.GcmListenerService.zzq(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zzp(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)
            Suppressed: java.lang.ClassNotFoundException: com.growmobile.engagement.R$raw
            at java.lang.Class.classForName(Native Method)
            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 18 more
            Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
    
    Process:com.growmobile.engagement.sample,PID:20883
    java.lang.NoClassDefFoundError:解析失败:Lcom/growmobile/engagement/R$raw;
    在com.growmobile.engagement.ModelPushMessage.loadSoundFromLocal(ModelPushMessage.java:451)上
    位于com.growmobile.engagement.ModelPushMessage.parseMessageData(ModelPushMessage.java:375)
    访问com.growmobile.engagement.ModelPushMessage.(ModelPushMessage.java:174)
    在com.growmobile.engagement.ManagerNotification.buildPushMessageObjectAndSaveItToDB上(ManagerNotification.java:139)
    位于com.growmobile.engagement.ManagerNotification.buildGMENotificationObject(ManagerNotification.java:108)
    位于com.growmobile.engagement.ManagerNotification.initializeNotification(ManagerNotification.java:77)
    在com.growmobile.engagement.GMEGcmListenerService.executePushAction(GMEGcmListenerService.java:152)
    在com.growmobile.engagement.GMEGcmListenerService.handleMessage(GMEGcmListenerService.java:118)上
    在com.growmobile.engagement.GMEGcmListenerService.onMessageReceived上(GMEGcmListenerService.java:79)
    在com.google.android.gms.gcm.GcmListenerService.zzq(未知来源)
    在com.google.android.gms.gcm.GcmListenerService.zzp(未知来源)
    在com.google.android.gms.gcm.GcmListenerService.zzo(未知来源)
    在com.google.android.gms.gcm.GcmListenerService.zza(未知来源)
    在com.google.android.gms.gcm.GcmListenerService$1.run(未知来源)
    位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    运行(Thread.java:818)
    原因:java.lang.ClassNotFoundException:在路径:DexPathList[[zip文件”//data/app/com.growmobile.engagement.sample-1/base.apk“],NativeLibraryDirectory=[/data/app/com.growmobile.engagement.sample-1/lib/arm64,/vendor/lib64,/system/lib64]]上未找到类“com.growmobile.engagement.R$ray”
    位于dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    位于java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    位于java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    在com.growmobile.engagement.ModelPushMessage.loadSoundFromLocal(ModelPushMessage.java:451)上
    位于com.growmobile.engagement.ModelPushMessage.parseMessageData(ModelPushMessage.java:375)
    访问com.growmobile.engagement.ModelPushMessage.(ModelPushMessage.java:174)
    在com.growmobile.engagement.ManagerNotification.buildPushMessageObjectAndSaveItToDB上(ManagerNotification.java:139)
    位于com.growmobile.engagement.ManagerNotification.buildGMENotificationObject(ManagerNotification.java:108)
    位于com.growmobile.engagement.ManagerNotification.initializeNotification(ManagerNotification.java:77)
    在com.growmobile.engagement.GMEGcmListenerService.executePushAction(GMEGcmListenerService.java:152)
    在com.growmobile.engagement.GMEGcmListenerService.handleMessage(GMEGcmListenerService.java:118)上
    在com.growmobile.engagement.GMEGcmListenerService.onMessageReceived上(GMEGcmListenerService.java:79)
    在com.google.android.gms.gcm.GcmListenerService.zzq(未知来源)
    在com.google.android.gms.gcm.GcmListenerService.zzp(未知来源)
    在com.google.android.gms.gcm.GcmListenerService.zzo(未知来源)
    在com.google.android.gms.gcm.GcmListenerService.zza(未知来源)
    在com.google.android.gms.gcm.GcmListenerService$1.run(未知来源)
    位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    运行(Thread.java:818)
    抑制:java.lang.ClassNotFoundException:com.growmobile.engagement.R$raw
    位于java.lang.Class.classForName(本机方法)
    位于java.lang.BootClassLoader.findClass(ClassLoader.java:781)
    位于java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
    位于java.lang.ClassLoader.loadClass(ClassLoader.java:504)
    …还有18个
    原因:java.lang.NoClassDefFoundError:使用引导类加载程序找不到类;没有可用的堆栈跟踪
    

    • 我必须指出,当库作为模块导入而不是作为 *.aar文件一切工作都很好
    • 我需要将库作为*.aar文件导入,因此我被卡住了,需要一个 尽快解决
    • 我的代码行吗?如果不行,这里怎么了
    感谢任何人的帮助 希望我能尽快得到答案(-:

    private Uri loadSoundFromLocal(Context context, String sound) {             
    
        try {
    
            // Get resource id.
            boolean isGmeResource = true;
            int soundResource = UtilsResources.getResId(sound, R.raw.class);
            if (soundResource == -1 || soundResource == 0) {
    
                soundResource = UtilsResources.getIdentifier(context, sound, "raw");
                if (soundResource != -1 && soundResource != 0) {
                    isGmeResource = false;
                }
            }
    
            // Create the uri string for the sound file.
            if (soundResource == -1 || soundResource == 0) {
    
                return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
            } else {                                                    
    
                return UtilsResources.getUriFromResId(context, soundResource, isGmeResource);
            }
    
        } catch (Exception e) { 
            return null;
        }
    }
    
    public static int getResId(String resourceName, Class<?> c) {
    
        if (TextUtils.isEmpty(resourceName)) { return -1; }
        if (c == null) { return -1; }
    
        try {
    
            Field field = c.getField(resourceName);
            if (field == null) {
                return -1;
            }
    
            return field.getInt(null);                      
    
        } catch (Exception e) {
            return -1;
        }
    }   
    
    public static int getIdentifier(Context context, String name, String defType) {
    
        String appPackageName = UtilsData.getAppPackageName(context);
        if (TextUtils.isEmpty(appPackageName)) {            
            return -1;
        }
    
        if (context.getPackageManager() == null) { return -1; }     
        if (TextUtils.isEmpty(name)) { return -1; }     
        if (TextUtils.isEmpty(defType)) { return -1; }
    
        try {
    
            Resources resources = context.getPackageManager().getResourcesForApplication(appPackageName);
            if (resources == null) {
                return -1;
            }
    
            return resources.getIdentifier(name, defType, appPackageName);      
    
        } catch (NameNotFoundException e) {
    
            // Should never happen
            UtilsLogger.e(LOG_TAG, (e != null)? e.getMessage() : "Could not get package name");
            return -1;
    
        } catch (Exception e) {
    
            UtilsLogger.e(LOG_TAG, (e != null)? e.getMessage() : "Could not get identifier version code");
            return -1;
        }
    }
    
    Process: com.growmobile.engagement.sample, PID: 20883
            java.lang.NoClassDefFoundError: Failed resolution of: Lcom/growmobile/engagement/R$raw;
            at com.growmobile.engagement.ModelPushMessage.loadSoundFromLocal(ModelPushMessage.java:451)
            at com.growmobile.engagement.ModelPushMessage.parseMessageData(ModelPushMessage.java:375)
            at com.growmobile.engagement.ModelPushMessage.<init>(ModelPushMessage.java:174)
            at com.growmobile.engagement.ManagerNotification.buildPushMessageObjectAndSaveItToDB(ManagerNotification.java:139)
            at com.growmobile.engagement.ManagerNotification.buildGMENotificationObject(ManagerNotification.java:108)
            at com.growmobile.engagement.ManagerNotification.initializeNotification(ManagerNotification.java:77)
            at com.growmobile.engagement.GMEGcmListenerService.executePushAction(GMEGcmListenerService.java:152)
            at com.growmobile.engagement.GMEGcmListenerService.handleMessage(GMEGcmListenerService.java:118)
            at com.growmobile.engagement.GMEGcmListenerService.onMessageReceived(GMEGcmListenerService.java:79)
            at com.google.android.gms.gcm.GcmListenerService.zzq(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zzp(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)
            Caused by: java.lang.ClassNotFoundException: Didn't find class "com.growmobile.engagement.R$raw" on path: DexPathList[[zip file "/data/app/com.growmobile.engagement.sample-1/base.apk"],nativeLibraryDirectories=[/data/app/com.growmobile.engagement.sample-1/lib/arm64, /vendor/lib64, /system/lib64]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at com.growmobile.engagement.ModelPushMessage.loadSoundFromLocal(ModelPushMessage.java:451)
            at com.growmobile.engagement.ModelPushMessage.parseMessageData(ModelPushMessage.java:375)
            at com.growmobile.engagement.ModelPushMessage.<init>(ModelPushMessage.java:174)
            at com.growmobile.engagement.ManagerNotification.buildPushMessageObjectAndSaveItToDB(ManagerNotification.java:139)
            at com.growmobile.engagement.ManagerNotification.buildGMENotificationObject(ManagerNotification.java:108)
            at com.growmobile.engagement.ManagerNotification.initializeNotification(ManagerNotification.java:77)
            at com.growmobile.engagement.GMEGcmListenerService.executePushAction(GMEGcmListenerService.java:152)
            at com.growmobile.engagement.GMEGcmListenerService.handleMessage(GMEGcmListenerService.java:118)
            at com.growmobile.engagement.GMEGcmListenerService.onMessageReceived(GMEGcmListenerService.java:79)
            at com.google.android.gms.gcm.GcmListenerService.zzq(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zzp(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zzo(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
            at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)
            Suppressed: java.lang.ClassNotFoundException: com.growmobile.engagement.R$raw
            at java.lang.Class.classForName(Native Method)
            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 18 more
            Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available