方法ID不在[0,0xffff]构建错误中

时间:2016-08-25 02:46:50

标签: java android

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

我尝试了这里提到的解决方案 DexIndexOverflowException issue after updating to latest appcompat and support library

但是我收到了这个错误

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

我也从这里尝试了解决方案 You need to use a Theme.AppCompat theme (or descendant) with this activity

但无效

旁边我不认为我的应用程序是那么大,包含65536方法 另一方面,当我用一些简单的方法创建新类时,我得到了这个错误

我试图追踪它

    Error:D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\DoctorMain.java:56: warning: [deprecation] setDrawerListener(DrawerListener) in DrawerLayout has been deprecated
    drawer.setDrawerListener(toggle);
          ^
D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\DoctorPersonalInformation.java:52: warning: [deprecation] getPreferenceScreen() in PreferenceActivity has been deprecated
    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    ^
D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\DoctorPersonalInformation.java:58: warning: [deprecation] getPreferenceScreen() in PreferenceActivity has been deprecated
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    ^
D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\DoctorPersonalInformation.java:124: warning: [deprecation] findPreference(CharSequence) in PreferenceActivity has been deprecated
            findPreference(s).setSummary(sharedPreferences.getString(s, ""));
            ^
D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\DoctorPersonalInformation.java:128: warning: [deprecation] findPreference(CharSequence) in PreferenceActivity has been deprecated
            findPreference(s).setSummary(sharedPreferences.getString(s, ""));
            ^
D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\DoctorPersonalInformation.java:132: warning: [deprecation] findPreference(CharSequence) in PreferenceActivity has been deprecated
            findPreference(s).setSummary(sharedPreferences.getString(s, ""));
            ^
D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\DoctorPersonalInformation.java:136: warning: [deprecation] findPreference(CharSequence) in PreferenceActivity has been deprecated
            findPreference(s).setSummary(sharedPreferences.getString(s, ""));
            ^
D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\DoctorPersonalInformation.java:140: warning: [deprecation] findPreference(CharSequence) in PreferenceActivity has been deprecated
            findPreference(s).setSummary(sharedPreferences.getString(s, ""));
            ^
D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\DoctorPersonalInformation.java:158: warning: [deprecation] findPreference(CharSequence) in PreferenceActivity has been deprecated
            findPreference(s).setSummary(name);
            ^
D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\DoctorPersonalInformation.java:162: warning: [deprecation] findPreference(CharSequence) in PreferenceActivity has been deprecated
            findPreference(s).setSummary(sharedPreferences.getString(s, ""));
            ^
  D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\DoctorPersonalInformation.java:166: warning: [deprecation] findPreference(CharSequence) in PreferenceActivity has been deprecated
            findPreference(s).setSummary(sharedPreferences.getBoolean(s, false) + "");
            ^
 D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\PatientMain.java:84: warning: [deprecation] setDrawerListener(DrawerListener) in DrawerLayout has been deprecated
    drawer.setDrawerListener(toggle);
          ^
  D:\games\games\ANDROID\HealMe\app\src\main\java\com\kareem\healme\Gui\PatientMain.java:325: warning: [deprecation] isLocationProviderEnabled(ContentResolver,String) in Secure has been deprecated
            .isLocationProviderEnabled(contentResolver,
            ^
13 warnings
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebug'.
  

com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException:java.util.concurrent.ExecutionException:com.android.dex.DexIndexOverflowException:方法ID不在[0, 0xffff]:65536

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

所有代码都正确运行,因此没有特定类或活动的错误

所以我该如何解决这个问题? 感谢

4 个答案:

答案 0 :(得分:4)

你的代码库有比65,536更多的方法,它不能捆绑在一个dex文件中。

在这种情况下,您必须添加multidex支持。

您需要做的就是添加:

android {
    ...

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
}

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

这是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

或者如果你有你自己的Application类实现,请覆盖attachBaseContext内的Application方法:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

* MultiDex仅适用于Android 5.0及更高版本。它没有向后兼容性。

答案 1 :(得分:3)

根据Android developer documentation

  

如果你已经构建了一个Android应用程序并收到此错误,那么恭喜你,你有很多代码!本文档介绍了如何超越此限制并继续构建应用程序。

有时,使用gradle依赖项导入库可能会导致此错误。您的应用中是否有Google Play服务库?如果您要使用谷歌地图,则可以导入您只想使用的库,而不是compile com.google.android.gms:play-services:9.4.0,而只需导入com.google.android.gms:play-services-maps:9.4.0而不是com.google.android.gms:play-services:9.4.0。它将有助于减少生成的dex数量。您还可以使用proguard来消除未使用的代码。

  

在6.5之前的Google Play服务版本中,您必须将整个API包编译到您的应用中。在某些情况下,这样做会使您的应用程序中的方法数量(包括框架API,库方法和您自己的代码)更难以保持65,536的限制。

     

从版本6.5开始,您可以选择性地将Google Play服务API编译到您的应用中。例如,要仅包含Google Fit和Android Wear API,请替换build.gradle文件中的以下行:

您可以参考Android Guide,了解如何有选择地将API编译为可执行文件

答案 2 :(得分:2)

如果您使用的是multiDex ......:

在你的清单中...如果你没有覆盖Application类,那么:

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

如果覆盖它,请确保您没有从Application扩展,而应该从MultiDexApplication扩展,并且在清单中应该是这样的:

<application android:name="yourOverridingClass" >

答案 3 :(得分:0)

检查您正在编译的Google Play Services。对我而言,修正了这个问题从

改变了
compile 'com.google.android.gms:play-services:10.2.0'

包括整个播放服务库,

compile 'com.google.android.gms:play-services-places:10.2.0'

仅包含Places API。