Search code examples
androidgradledalvikdynamic-class-loaders

Custom Class Loading in Dalvik with Gradle (Android New Build System)


As per the introduction of Custom Class Loading in Dalvik by Fred Chung on the Android Developers Blog:

The Dalvik VM provides facilities for developers to perform custom class loading. Instead of loading Dalvik executable (“dex”) files from the default location, an application can load them from alternative locations such as internal storage or over the network.

However, not many developers have the need to do custom class loading. But those who do and follow the instructions on that blog post, might have some problems mimicking the same behavior with Gradle, the new build system for Android introduced in Google I/O 2013.

How exactly one can adapt the new build system to perform the same intermediary steps as in the old (Ant based) build system?


Solution

  • My team and I recently reached the 64K method references in our app, which is the maximum number of supported in a dex file. To get around this limitation, we need to partition part of the program into multiple secondary dex files, and load them at runtime.

    We followed the blog post mentioned in the question for the old, Ant based, build system and everything was working just fine. But we recently felt the need to move to the new build system, based on Gradle.

    This answer does not intend to replace the full blog post with a complete example. Instead, it will simply explain how to use Gradle to tweak the build process and achieve the same thing. Please note that this is probably just one way of doing it and how we are currently doing it in our team. It doesn't necessarily mean it's the only way.

    Our project is structured a little different and this example works as an individual Java project that will compile all the source code into .class files, assemble them into a single .dex file and to finish, package that single .dex file into a .jar file.

    Let's start...

    In the root build.gradle we have the following piece of code to define some defaults:

    ext.androidSdkDir = System.env.ANDROID_HOME
    
    if(androidSdkDir == null) {
        Properties localProps = new Properties()
        localProps.load(new FileInputStream(file('local.properties')))
    
        ext.androidSdkDir = localProps['sdk.dir']
    }
    
    ext.buildToolsVersion = '18.0.1'
    ext.compileSdkVersion = 18
    

    We need the code above because although the example is an individual Java project, we still need to use components from the Android SDK. And we will also be needing some of the other properties later on... So, on the build.gradle of the main project, we have this dependency:

    dependencies {
        compile files("${androidSdkDir}/platforms/android-${compileSdkVersion}/android.jar")
    }
    

    We are also simplifying the source sets of this project, which might not be necessary for your project:

    sourceSets {
        main {
            java.srcDirs = ['src']
        }
    }
    

    Next, we change the default configuration of the build-in jar task to simply include the classes.dex file instead of all .class files:

    configure(jar) {
        include 'classes.dex'
    }
    

    Now we need to have new task that will actually assemble all .class files into a single .dex file. In our case, we also need to include the Protobuf library JAR into the .dex file. So I'm including that in the example here:

    task dexClasses << {
        String protobufJarPath = ''
    
        String cmdExt = Os.isFamily(Os.FAMILY_WINDOWS) ? '.bat' : ''
    
        configurations.compile.files.find {
            if(it.name.startsWith('protobuf-java')) {
                protobufJarPath = it.path
            }
        }
    
        exec {
            commandLine "${androidSdkDir}/build-tools/${buildToolsVersion}/dx${cmdExt}", '--dex',
                        "--output=${buildDir}/classes/main/classes.dex",
                        "${buildDir}/classes/main", "${protobufJarPath}"
        }
    }
    

    Also, make sure you have the following import somewhere (usually at the top, of course) on your build.gradle file:

    import org.apache.tools.ant.taskdefs.condition.Os
    

    Now we must make the jar task depend on our dexClasses task, to make sure that our task is executed before the final .jar file is assembled. We do that with a simple line of code:

    jar.dependsOn(dexClasses)
    

    And we're done... Simply invoke Gradle with the usual assemble task and your final .jar file, ${buildDir}/libs/${archivesBaseName}.jar will contain a single classes.dex file (besides the MANIFEST.MF file). Just copy that into your app assets folder (you can always automate that with Gradle as we've done but that is out of scope of this question) and follow the rest of the blog post.

    If you have any questions, just shout in the comments. I'll try to help to the best of my abilities.