Skip to content

Instantly share code, notes, and snippets.

@creativepsyco
Created May 7, 2014 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save creativepsyco/57a232d3a2e2c90195f0 to your computer and use it in GitHub Desktop.
Save creativepsyco/57a232d3a2e2c90195f0 to your computer and use it in GitHub Desktop.
Gist for creating Dex file for class files in Android Library
/**
* The Task Below uses the Dex Tool to create classes.dex file for the archive
* This dex file can be then be loaded at runtime to prevent the 64K Dalvik Method Limit
*/
import org.apache.tools.ant.taskdefs.condition.Os
android.libraryVariants.all { variant ->
def name = variant.buildType.name
if (name.equals(com.android.builder.BuilderConstants.DEBUG)) {
return; // Skip debug builds.
}
log.info("========================================")
log.info("Starting Variant Build")
log.info("Variant Name: ${variant.name}")
log.info("Build Type Name: ${variant.buildType}")
log.info("Variant: ${variant}")
log.info("========================================")
String cmdExt = Os.isFamily(Os.FAMILY_WINDOWS) ? '.bat' : ''
def task = project.tasks.create ("dex${name.capitalize()}", Exec)
task.dependsOn variant.javaCompile
task.commandLine "${System.env.ANDROID_HOME}/build-tools/${BUILD_TOOLS_VERSION}/dx${cmdExt}", '--dex',
"--output=${buildDir}/classes/${variant.name}/classes.dex",
"${buildDir}/classes/${variant.name}"
// Make sure we run our Dexing task before the packageReleaseJar Task
Task packageJar = project.tasks.getByName("package${name.capitalize()}Jar")
packageJar.dependsOn(task)
}
@creativepsyco
Copy link
Author

You would need to declare the BUILD_TOOLS_VERSION string in your build file.

@CMingTseng
Copy link

Dear Sir

when new gradle version

we have new API -- Transform

i search. gradle system -- https://android.googlesource.com/platform/tools/base/+/refs/tags/gradle_3.4.0

when Android build default use Transform with DexArchiveBuilderTask. &. DexArchiveBuilder

those can create. dex file (. class file --> dex file. /. class folder --> dex file)

how use those or create the same to. trans myself class folder to dex file ?

THX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment