DexGrape the Maven dependency manager
Since Groovy Shell 3.0.0, you can now load dependencies dynamically from Maven. It is the adaptation of Groovy Grape.
Note that jars loaded by DexGrape
will be added to your classpath only for the current session. When
the app is killed, all loaded dependencies are discarded. If you want to keep dependencies on your classpath
everytime you start a Shell, you can achieve that by managing your additional class path
How it works
Grape will fetch dependency from Maven (it will also resolve and fetch transitive dependencies) and then save them into the local repository of the Groovy Shell.
Since Android uses Dalvik byte code, it will then convert jars into dex jars (jar of Dalvik byte code classes) using Dexter. Only converted jars will be kept (since Android can’t load original jars it is useless to keep them).
If you want to take a look on how dependencies are resolved, you can consult the Maven Dependency Resolver.
How to use it
Start by using the import alias dex
for simplicity
import dex
Load Maven dependency
Just grab the dependency with DexGrape
import dex
addedJarsToClassPath = DexGrape.grab("com.google.code.gson", "gson", "2.8.6")
println "List of jars added to classpath: $addedJarsToClassPath"
import com.google.gson.Gson
g = new Gson()
It also works with plain Groovy Grape.grab()
groovy.grape.Grape.grab(group:'com.google.code.gson', module: 'gson', version: '2.8.6')
Resolve Maven dependency
Resolving a Maven will just fetch the dependency (and its transitive), and return them, but it won’t add them to your classpath.
resolvedArtifacts = DexGrape.resolve("com.google.code.gson", "gson", "2.8.6")
List fetched artifacts
You can list fetched artifacts, jars from your local repository as follows: It returns a map String -> (String -> (Artifact)). It is because it maps groupId -> artifactId -> list of all versions of this artifact
artifacts = DexGrape.listFetchedArtifacts()
println('All fetched gson artifacts ' + artifacts['com.google.code.gson']['gson'])
Handle fetched artifacts
You can handle artifacts from your local repository through the Preferences
screen, at the item
sdf Dex jars repository
. There, you can delete fetched jars or add some to your
additional class path