How do I import a class in gradle outside of build

2019-04-07 17:42发布

问题:

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?

回答1:

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



回答2:

buildscript {
repositories {
    jcenter()
    maven {
        url RELEASE_REPOSITORY_URL
        credentials {
            username NEXUS_USERNAME
            password NEXUS_PASSWORD
        }
    }
    maven { url uri(new File(rootDir, 'local_repo')) }
}
dependencies { classpath "android:cm.android.util:1.2" }
}

I have the same problem to you, I want to import the class of cm.android.util.If I do not addd the buildscript, it gives me the error:can not import the class.you can add buildscript in your build files, try it!