【问题标题】:Android - Cause: java.lang.ArrayIndexOutOfBoundsException even with multidexEnable trueAndroid - 原因:java.lang.ArrayIndexOutOfBoundsException 即使 multidexEnable true
【发布时间】:2019-04-08 15:12:07
【问题描述】:

编辑:这是我的完整构建扫描:https://scans.gradle.com/s/fva2ntifaimgi

原文: 在我的应用程序的 gradle 配置中将 multidexEnabled true 添加到我的 defaultConfig 后,我有以下未解决的问题:

java.lang.RuntimeException: com.android.build.api.transform.TransformException: java.lang.ArrayIndexOutOfBoundsException
...
Caused by: com.android.build.api.transform.TransformException: java.lang.ArrayIndexOutOfBoundsException
...
Caused by: java.lang.ArrayIndexOutOfBoundsException: 65535

我必须检查什么才能真正启用多索引?我做错了什么?

我的配置是:

minSdkVersion 26
targetSdkVersion 28

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="come.example.project">
<uses-permission [...]/>
<supports-screens [...]/>
<application
    android:name="com.example.project.App"
    android:allowBackup="false"
    android:fullBackupContent="false"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <!-- activities -->
[...]

    <!-- providers -->
[...]
    <!-- broadcast receivers -->
[...]
  </application>

</manifest>

我的毕业配置: 应用插件:'com.android.application' 应用插件:'org.greenrobot.greendao'

android {
  compileSdkVersion 28
  buildToolsVersion '28.0.3'
  defaultConfig {
    applicationId "com.example.project.dev"
    minSdkVersion 26
    targetSdkVersion 28
    versionCode 1
    versionName "0.1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
  }
  buildTypes {
    release {
      [...]
    }
    debug {
      [...]
    }
    applicationVariants.all { variant ->
      variant.outputs.all { output ->
        final String chain = [...]
        outputFileName = [...]
      }
    }
  }
  configurations.all {
    resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.1'
  }
  dataBinding {
    enabled = true
  }
  packagingOptions {
    exclude 'META-INF/LICENSE*'
    exclude 'META-INF/NOTICE.txt'
    merge 'reference.conf'
  }
  externalNativeBuild {
    cmake {
      path 'CMakeLists.txt'
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

greendao {
  // increment when changing DB structure
  schemaVersion 8
}

configurations {
  compile.exclude group: 'com.google.guava', module: 'listenablefuture'
}

dependencies {
  implementation fileTree(include: ['*.jar'], dir: 'libs')
  androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-alpha4', {
    exclude group: 'com.android.support', module: 'support-annotations'
  })

  implementation 'com.android.support:multidex:1.0.3'
  //implementation 'com.android.support:design:28.0.0'
  implementation 'com.google.android.material:material:1.1.0-alpha05'

  //implementation 'com.android.support:preference-v7:28.0.0'
  implementation 'androidx.preference:preference:1.0.0'

  //implementation 'com.android.support:preference-v14:28.0.0'
  implementation 'androidx.legacy:legacy-preference-v14:1.0.0'

  //implementation 'com.android.support:support-media-compat:28.0.0'
  implementation 'androidx.media:media:1.0.1'

  //implementation 'com.android.support:support-v4:28.0.0'
  implementation 'androidx.legacy:legacy-support-v4:1.0.0'

  //implementation 'com.android.support.constraint:constraint-layout:1.1.3'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

  implementation 'com.google.code.findbugs:jsr305:3.0.2'
  implementation 'com.google.android.gms:play-services-drive:16.1.0'
  implementation 'com.google.android.gms:play-services-auth:16.0.1'
  implementation('android.arch.work:work-runtime:1.0.1-rc01') {
    exclude group: 'com.google.guava', module: 'listenablefuture'
  }
  // logging
  implementation 'org.slf4j:slf4j-api:1.7.25'
  implementation 'com.github.tony19:logback-android:1.1.1-12'
  implementation('com.papertrailapp:logback-syslog4j:1.0.0') {
    exclude group: 'ch.qos.logback'
  }

  // required for greendao encryption, disabled for now
  implementation 'net.zetetic:android-database-sqlcipher:3.5.6'
  // qr codes
  implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
  // events
  implementation 'org.greenrobot:eventbus:3.0.0'
  // http client
  testImplementation 'junit:junit:4.12'

}

我的申请文件:

public class App extends MultiDexApplication {
  @Override
  protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
  }
  [...]
}

【问题讨论】:

  • 你是否在你的应用类中重写了multi dex?
  • 我没有。我试着这样做看看它是否改变了一些东西,但它没有。也许我没有按照正确覆盖的步骤......

标签: android dex android-multidex


【解决方案1】:

你必须做一些事情。 https://developer.android.com/studio/build/multidex?authuser=1

添加依赖:

dependencies {
  compile 'com.android.support:multidex:1.0.3'
}

让 Gradle 知道它的存在

defaultConfig {
    ...
    multiDexEnabled true
 }

如果您有,请在您的应用程序类中覆盖它:

public class MyApplication extends MultiDexApplication {
@Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}

更新您的清单以指向您的应用程序类:

<application
      android:name="com.mypackage.MyApplication" >
      ...
</application>

或者,如果您没有自己的扩展 Application 类的自定义 Application 类,那么您可以简单地更改您的应用程序标签以使用默认支持 multidexapplication 类,而不必担心。

 <application
        android:name="android.support.multidex.MultiDexApplication" >
        ...
 </application>

希望对您有所帮助,如果您有更多问题,请告诉我。

【讨论】:

  • 感谢您的回答。我会尝试并回复你!
  • 为了清楚起见,我又编辑了一件事情,要么你做一个自定义类并指向它,要么你使用默认类。
  • 所以我以前扩展 Application 的应用程序现在必须扩展 MultiDexApplication ?还是我必须参考的新的?
  • 是的,如果你有一个自定义的应用程序类,那么它应该覆盖 MultiDexApplication
  • 您仍然遇到同样的错误?发布您的清单、您的应用程序文件和您的 gradle。并确认您在移植到多 dex 后执行了 File->Invalidate Cache and Restart
【解决方案2】:

尝试使缓存无效并重新启动android studio。转到文件->使缓存无效/重新启动

【讨论】:

    猜你喜欢
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    相关资源
    最近更新 更多