Android Studio - 无法从位于libs文件夹(或mipmap文件夹)中的some_lib.aar文件加载资源

时间:2016-05-16 07:55:46

标签: android android-studio gradle

我搜索了这个问题,但没有找到任何与我的问题相关的答案。我希望以前有人能解决这个问题并且可以帮助我( - :。

  1. 我有一个android工作室项目,里面有'libs'文件夹 aar库(例如:some_lib.aar)。
  2. 在aar库中,我有'mipmap'和'raw'文件夹 内部资源('mipmap'有PNG,'raw'有声音 文件名为:sound.wav)。
  3. 在我的项目中,我正在尝试从mipmap中加载一些* .png文件 来自'raw'文件夹的声音文件如下:
  4. Uri soundUri = loadSoundFromLocal(context, "sound.wav");    
    
    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;
        }
    }
    
    1. 我有这样的例外(也适用于'mipmap'文件夹):
    2. EXCEPTION

      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
      
      • 我必须说当图书馆作为模块而不是作为模块导入时 * .aar文件一切正常!
      • 我需要将库导入为* .aar文件,所以我卡住了,需要一个 解决方案尽快解决!
      • 我的代码好吗?如果没有,这里有什么问题?

      感谢您提前获得任何人的帮助 希望我能尽快得到答案( - :

0 个答案:

没有答案