14

I am having a wired issue I am using gradle 1.9

I cannot seem to import a class from outside build.gradle

The following works

build.gradle

buildscript {
    repositories {
        mavenLocal()
    }
    dependencies {
        classpath(group: 'com.foo', name: 'gradle-local-eureka', version: '1.0-SNAPSHOT')
    }
}
import com.foo.my.awesome.package.AwesomeService

The following errors out build.gradle

buildscript {
    repositories {
        mavenLocal()
    }
    dependencies {
        classpath(group: 'com.foo', name: 'gradle-local-eureka', version: '1.0-SNAPSHOT')
    }
}
apply from: file('gradle/foo.gradle')

foo.gradle

import com.foo.my.awesome.package.AwesomeService
// do stuff

I get an unable to resolve class error if I try to import a class outside of build.gradle, does anyone have any insight as to why that wouldn't work or what the proper way of doing this?

fieldju
  • 1,443
  • 1
  • 14
  • 20

1 Answers1

8

Try to move the buildscript block into gradle/foo.gradle.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • But very sadly, the class is not the same with the class of parent script file , becuase they have different classloader. So we cannot check instanceof operator. Very annoying for me. Anyone knows the solution ? https://github.com/gradle/gradle/issues/4007 – Victor Choy Apr 16 '21 at 10:34