I have a custom gradle plugin which has uploaded into jcenter, I can use it in my android project like:
root project build.gradle:
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.myproject:projectname:1.0.1'
}
app's build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.myproject.projectname'
myconfig {
......
}
It works fine. But when I use it in my android library module, It will show error message "Gradle DSL method no found:'myconfig()'"
library module's build.gradle
apply plugin: 'com.android.library'
apply plugin: 'com.myproject.projectname'
myconfig {
......
}
anyone known why?
I found the reason, in my gradle plugin I checked the project like this:
if (project.getPlugins().hasPlugin(AppPlugin)) {....}
so when I call it in library module, it can't goto the right branch.
put your jar/dependencies in a folder (suppose "libs")
then show the directory
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile name: 'jarFile'
}
or something like:
dependencies {
compile files('libs/something_local.jar')
compile 'package_name'
}
Related
I'm currently working on creating my own Gradle custom distribution and I want to add some default settings to all my Android projects. I tried to do this with the following init script:
initscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath "com.android.tools.build:gradle:7.1.1"
}
}
apply plugin: 'com.android.application'
gradle.allprojects {
android {
...
}
dependencies {
implementation 'junit:junit:4.12'
}
}
but with this I get Plugin with id 'com.android.application' not found. What am I missing here?
For gradle init script, you must use the fully qualified class name of the plugin instead of the id. So you will have to apply the plugin this way:
apply plugin: com.android.build.gradle.AppPlugin
Change the initscript to buildscript.
And also, does this build.gradle in root project/directy? Because you Android Gradle Plugin is applied in the gradle module not in the root.
So apply:
apply plugin: "com.android.application"
inside your module's build.gradle.
Make sure your Gradle version is compatible with your Android Gradle Plugin.
For more information you can follow the link below, if it helps you
plugin with id com.android.application not found
My project has a two modules, an app module, and a lib module.
For my app I have the following in the app module's build.gradle file
apply plugin: 'com.android.application'
dependencies {
implementation project(':my-lib')
implementation 'com.android.support:appcompat-v7:27.1.1'
}
I would like to convert my-lib into a jar, and use the jar as the dependency rather than the module itself, so I added a libs folder in the parent folder of the src folder,
app
AndroidManifest.xml
build
src
res
libs
added the jar to libs and defined the libs folder in the build.gradle
apply plugin: 'com.android.application'
dependencies {
implementation project(':my-lib')
implementation 'com.android.support:appcompat-v7:27.1.1'
}
repositories {
flatDir {
dirs 'libs'
}
}
How can I instruct Gradle to use the jar? Is this the correct path?
Changing this
dependencies {
implementation project(':my-lib')
implementation 'com.android.support:appcompat-v7:27.1.1'
}
to this
dependencies {
api files('libs/my-lib.jar')
implementation 'com.android.support:appcompat-v7:27.1.1'
}
seems to have worked
I've a gradle project with subprojects. Every subproject produces a jar and a pom that it's published on a repository
1) In the main project gradle file there's a subprojects section that I used to define what and where to publish:
snippet from rootproject.gradle:
subprojects {
apply plugin: 'maven-publish'
publishing {
publications {
mavenJava(org.gradle.api.publish.maven.MavenPublication) {
from components.java
}
}
repositories {
maven {
url 'file://c:/temp/repo'
}
}
}
}
2) In the gradle file of one of my subprojects, I've added some dependencies:
snippet from subproject.gradle:
dependencies {
compile group: 'my-group', name: 'my-module', version:'1.1.0'
}
If I run "gradle publish" from the rootproject it will correctly publish every subproject. However, I noticed that the dependency defined in the subproject is missing from the pom publication related to the subproject.
If I cut and paste the content of the subprojects section in each subproject, the generated pom file contains the correct dependency.
It seems that “from components.java” is not a reference to something that should be used by the publish task to produce the pom, but the task will publish exactly what components.java contains when you call the “from” method.
As a workaround, I moved the subprojects code in a method defined in the root:
rootproject.gradle
def configurePublishing(project) {
project.apply plugin: 'maven-publish'
…
}
And I called it from each subproject:
subproject.gradle
dependencies {
compile group: 'my-group', name: 'my-module', version:'1.1.0'
}
configurePublishing(project)
Another solution could be adding a switch in the subprojects section and centralize everything in the gradle file of the root project:
subprojects { subProject ->
switch(subproject.name) {
case: ‘my-subproject-with-dependencies’ {
dependencies {
compile group: 'my-group', name: 'my-module', version:'1.1.0'
}
break;
}
}
apply plugin: 'maven-publish'
}
Is it an acceptable approach? Is there a best practice to follow? Is there an easier way to do it?
I have a project corehibernate and a project coregeneral. corehibernate is dependent on coregeneral. I need the jar file of coregeneral to be bundled along with the corehibernate jar. I tried various versions of the build.gradle thing, nothing worked.
I tried compile files("../coregeneral/build/libs/coregeneral.jar")
This version of fatJar too does not work.
apply plugin: 'java'
repositories {
jcenter()
}
dependencies {
compile (':coregeneral')
testCompile 'junit:junit:4.12'
}
jar {
baseName='corehibernate'
from ('bin')
}
task fatJar(type: Jar, dependsOn: jar) {
baseName = project.name + '-fat'
}
There are two basic ways how to bundle projects together. The first would be to use application plugin which creates a zip with scripts that will also execute your application and bundle all jars by default. Second way is to use distribution plugin and define the final archive yourself (zip or tar).
Here is a sample project using the application plugin:
settings.gradle
rootProject.name = 'root'
include 'partone', 'parttwo'
build.gradle
subprojects {
apply plugin: 'java'
}
partone/build.gradle - this one is empty
parttwo/build.gradle
apply plugin: 'application'
mainClassName = 'Hello'
dependencies {
compile project (':partone')
}
Give that both projects actually have some content (classes), when you run gradle :projecttwo:build it will generate a zip file with executable scripts and both jars bundled inside.
If you prefer to use distribution plugin, change the parttwo/build.gradle to:
apply plugin: 'distribution'
distributions {
main {
contents {
from jar
from (project.configurations.runtime)
}
}
}
dependencies {
compile project (':partone')
}
And again run gradle :parttwo:build. It will create a zip file that contains both jars.
I have two modules in Android Studio.
Main is the application and Sub is a library module. Sub is referred from Main with compile project(':Sub') in the gradle script. That works when run from Android Studio. But when run from command line, gradlew says:
Could not create plugin of type 'LibraryPlugin'.
Caused by: java.lang.NoClassDefFoundError: org/gradle/api/artifacts/result/ResolvedComponentResult
This is the important parts in Main's build.gradle file:
apply plugin: 'android'
buildscript {
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
task wrapper(type: org.gradle.api.tasks.wrapper.Wrapper) {
gradleVersion = '1.11'
}
android {
buildToolsVersion '19.0.3'
}
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile project (':Sub')
}
The Sub gradle file is more or less identical, but has
apply plugin: 'android-library'
instead of 'android'
I have tried with gradle 1.9 and 1.10, but same result.
Anyone knows how to solve this?
Verify that your dependencies contains classpath 'com.android.tools.build:gradle:0.9.+' in each gradle.build file (or just put it in the base one and not declare it in the others). Update gradle/wrapper/gradle-wrapper.properties to point to gradle 1.11:
distributionUrl=http\://services.gradle.org/distributions/gradle-1.11-all.zip
If you have any other instances of the gradle-wrapper (such as if you originally made the library project on its own and later added an example app), verify that all instances are updated to point to the same version (each gradle-wrapper.properties file).