nero
nero

Reputation: 41

Get classpath of Android project in gradle

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)?

Upvotes: 2

Views: 2632

Answers (2)

Fernando Jascovich
Fernando Jascovich

Reputation: 459

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

It does the trick.

Upvotes: 0

nero
nero

Reputation: 41

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();
    }
}

Upvotes: 2

Related Questions