2

I'm writing a gradle task, which should execute a java application, but I can't get the classpath of my Android project.

My gradle task:

task helloWorld(type: JavaExec) {
  classpath = buildscript.configurations.classpath;
  main = "com.test.HelloWorld";
}

This doesn't work.

How can I get the classpath (which includes all classes and dependencies)?

nero
  • 41
  • 7
  • Possible duplicate of [Get classpath for gradle project using Android plugin](http://stackoverflow.com/questions/28222747/get-classpath-for-gradle-project-using-android-plugin) – sschuberth Sep 07 '16 at 07:38

2 Answers2

2

I've found a solution the source folders and the libraries. But I still miss the android.jar. There Is another question for this problem: Finding android.jar for a own written Java task (in gradle)

class JavaTask extends JavaExec {    
    @Override
    void exec() {
        List<String> classpaths = new ArrayList<>(Arrays.asList(getClasspath().getAsPath().split(";")));

        project.android.libraryVariants.all  { variant ->
            classpaths.add(variant.javaCompile.destinationDir.getPath());
        }

        org.gradle.api.artifacts.Configuration config = project.configurations.getByName("compile");
        for (String path : config.getAsPath().split(";")) {
            classpaths.add(path);
        }

        setClasspath(project.files(classpaths.toArray(new String[classpaths.size()])));
        super.exec();
    }
}
Community
  • 1
  • 1
nero
  • 41
  • 7
0

Please check the second answer of this question: https://stackoverflow.com/a/40515402/3130610

It does the trick.

Community
  • 1
  • 1