Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redundant resource referances in multiple R.java causes DexIndexOverflowException

I have multiple library projects and they all have dependency to Support Library. My application has dependency to these multiple library projects. Every library project contains references to support library's resources in their R.java file. This inflates the field ID count because of redundancy.

My app gets

DexIndexOverflowException: field ID not in [0, 0xffff]: 65536

because of this redundant R.java references.

Because of this my app has 47k methods while 65k field ids.

Edit:

I won't use multi-dex, it is not a solution to my problem. I want to shave redundant field IDs.

The question is not about how to work around the problem, the question is about how to get rid of the redundant field IDs. Using multi-dex won't remove the redundant field IDs.

like image 931
Tony Avatar asked Oct 18 '17 07:10

Tony


1 Answers

Like @intellij-amiya's answer, using multidex or proguard will solve your problem and I personally recommend that.

If you do not want to follow that method, you can manually exclude duplicated dependencies.

Execute the following command in your terminal to find duplicated dependencies.

./gradlew :app:dependencies --configuration compile

or if you on windows,

gradlew.bat :app:dependencies --configuration compile

change :app as your project name.

Let's assume your gradle dependencies are like this:

compile 'com.android.support:support-compat:26.+'
compile 'com.android.support:support-fragment:26.+'

You will get output like below:

+--- com.android.support:support-compat:26.+ -> 26.0.0-alpha1
|    \--- com.android.support:support-annotations:26.0.0-alpha1
\--- com.android.support:support-fragment:26.+ -> 26.0.0-alpha1
     +--- com.android.support:support-compat:26.0.0-alpha1 (*)
     +--- com.android.support:support-core-ui:26.0.0-alpha1
     |    +--- com.android.support:support-annotations:26.0.0-alpha1
     |    \--- com.android.support:support-compat:26.0.0-alpha1 (*)
     \--- com.android.support:support-core-utils:26.0.0-alpha1
          +--- com.android.support:support-annotations:26.0.0-alpha1
          \--- com.android.support:support-compat:26.0.0-alpha1 (*)

And you can see dependencies marked with (*), and these dependencies can be excluded. You can see support-compat is duplicated, and exclude it is done by edit like this:

compile ('com.android.support:support-fragment:26.+') {
    exclude module: 'support-compat'
}

Repeating this until you can get the count below 64k

And now the hardest part remains.

In my experience, excluding some dependencies may cause build fail, runtime exceptions, and etc. So you need to check your application working well without problem.

Hope this help.

like image 179
changhwan Avatar answered Sep 28 '22 07:09

changhwan