0

Android Studio: 1.3.1 - Gradle Build Plugin: 1.1.2 - Gradle: 1.3.0

On Android Studio, I have an app that runs perfectly fine on Android API22 (Lollipop, on both simulator API22 and Android phone API22, and also works on API 21 - but nothing below API 21).

In my Gradle build file, I have the following:

compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
    minSdkVersion 17
    targetSdkVersion 22
...
compile "commons-io:commons-io:2.4" //IO

So, as I understand it: my app is compiled with the latest API (22) to run on devices from API 17 to API 22 (and 22+ in compatibility mode).

However, when I run the Android app on a API 17 simulator, it crashes during some file copy operations. Before the crash, dalivkvm complains it cannot find methods.

I/dalvikvm﹕ Could not find method org.apache.commons.io.FileUtils.copyInputStreamToFile, 
referenced from method FileCopy.batchCreate
W/dalvikvm﹕ VFY: unable to resolve static method 58205: Lorg/apache/commons/io/FileUtils;
.copyInputStreamToFile (Ljava/io/InputStream;Ljava/io/File;)V
D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x0058

Then the fatal exception:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: org.apache.commons.io.FileUtils

Now obviously the apache commons libraries can be present and imported, at least on API 22 (which, I remind you, runs successfully on both the device and the simulator).

This also happens for other libraries aside from the Apache commons one (if I skip using the Apache Commons, then another third party library will cause a similar issue and so on).

I'm at loss as to why it won't run on API 17. Also have the same issues on API 18 and 19, API 20 doesn't exist.

It appears to work correctly on both API 21 and API 22.

I've looked for similar errors on here, but usually it is because people simply forgot to include their jar libs so it didn't help.


UPDATES

Community
  • 1
  • 1
Pelpotronic
  • 550
  • 6
  • 11

1 Answers1

0

I was able to fix my own issue.

So essentially, as I understand it (but I don't have time to investigate further), the issue is due to the fact that the way "Multidex" is supported has changed in Android API 21 (aka Android 5.0).

The link: https://developer.android.com/tools/building/multidex.html tells us:

Android 5.0 and higher uses a runtime called ART which natively supports loading multiple dex files from application APK files.

Now, it would seem that when multidexing and trying to support both "normal DEXing" (before 5.0/API21) and "ART oat files" (the new "DEX" files after 5.0/API21) in the same application you can encounter some issues similar to mine (unable to find some methods before API21 but app working fine on API21 and above).

My app hit the 65k methods limit and I had to support from API17+.

Anyway, the workaround was to disable multidex and use instead minifyEnabled with "Proguard", so that all unused methods are removed by Proguard and the total number of methods ends up below 65k.

The associated code (in Gradle build file):

defaultConfig {

    ...

    //Enabling multidex support (for more than 65k methods)
    multiDexEnabled false
}

buildTypes {
    debug {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

This isn't a real solution, of course, as someone who needs to effectively use 65k methods will HAVE to use multidex, but it did the job nicely for me and I can't spend more time on these issues.

Hopefully this will help someone.

Pelpotronic
  • 550
  • 6
  • 11
  • Note that enabling Proguard will then require you to do some tweaking to the proguard-android.txt file so it's not just a case of enabling minify (you then have to configure proguard to work with your specific app). – Pelpotronic Aug 19 '15 at 02:58
  • Note that proguard make the error trace in google play difficult to read, you need to take in account your mapping file – D4rWiNS Oct 05 '16 at 11:29