android greendao plugin config confusing - greendao

In latest androidstudio, the project build.gradle format changed as below:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.greenrobot-gradle-plugin' version '3.2.1' apply false
}
The 3rd line report error:
P
lugin [id: 'org.greenrobot-gradle-plugin', version: '3.2.1', apply: false] was not found in any of the following sources:
I know the old androidstudio use below format:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
}
}
How should I convert old build.gradle config to new format?

Related

How to call a scala method from build.gradle file

I have just started working on the scala and gradle. I want to know how we can call any scala method from the gradle build file. Can somebody please help me?
From comment: I want to run multiple files present in a directory. So in order to get all the files in the directory, I have written a method in scala. That method I am trying to call from build.gradle file
Gradle allows to specify dependencies of build script itself inside buildscript{ dependencies { ... } } (not to be confused with project dependencies inside ordinary dependencies { ... }).
For example here I added Shapeless dependency and used an HList in build.gradle
build.gradle
buildscript{
dependencies {
classpath group: 'com.chuusai', name: 'shapeless_2.13', version: '2.4.0-M1'
}
}
plugins {
id 'java'
id 'scala'
id 'application'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// compile group: ...
}
application {
mainClassName = 'App'
}
import shapeless.*
task hello {
doLast {
HList l = new $colon$colon(1, new $colon$colon("a", new HNil$()))
println l
}
}
Terminal
$ ./gradlew hello
Output
> Task :hello
1 :: a :: HNil
BUILD SUCCESSFUL in 457ms
1 actionable task: 1 executed
So you can compile your Scala sources, package classes into jar, publish it locally and specify your local dependency in
buildscript{
dependencies {
classpath group: 'yourOrganization', name: 'yourArtifact', version: 'yourVersion'
}
}
and then call methods from your Scala code in build.gradle.

Idea cannot recognize a symbol even it is present in external dependencies

Does somebody understands what is a problem? Why my IDE doesn't see classes from dependencies? Idea version: 17.2.2
The root build.gradle:
subprojects.each {
apply plugin: 'idea'
}
The root setting.gradle:
include 'client'
include 'api'
rootProject.name = 'app-1-akka-reactjs'
My build.gradle of api project:
apply plugin: 'play'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.iatoki:gradle-play-idea:0.4.1'
}
}
apply plugin: 'org.iatoki.play-idea'
repositories {
jcenter()
ivy {
url "https://repo.typesafe.com/typesafe/ivy-releases/"
layout "pattern", {
ivy "[organisation]/[module]/[revision]/ivys/ivy.xml"
artifact "[organisation]/[module]/[revision]/jars/[artifact].[ext]"
}
}
}
model {
components {
play {
platform play: '2.5.8', scala: '2.11', java: '1.8'
injectedRoutesGenerator = true
}
}
}
dependencies {
play 'com.typesafe.play:play-slick_2.11:2.1.0'
play 'com.typesafe.play:play-slick-evolutions_2.11:2.1.0'
play 'org.postgresql:postgresql:9.4-1200-jdbc41'
}
The build.gradle of client is empty for now.
To generate idea's files I've used:
gradle cleanIdea idea
I faced a similar issue. I could resolve it by moving/copying the settings.xml file from $MVN_HOME/conf to ~/.m2 folder. As MVN_HOME was not detected by Idea it could not resolve the settings.xml from MVN_HOME.

Writing custom gradle plugin with non-transitive dependency

I am trying to implement a gradle plugin which in turn depends on an another gradle plugin. Here is a snippet of my plugin's build.gradle
buildscript {
repositories {
mavenCentral()
mavenLocal()
}
}
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'maven'
dependencies {
repositories {
mavenCentral()
mavenLocal()
}
compile gradleApi()
compile localGroovy()
compile 'com.example:otherplugin:[0.1,)'
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: mavenLocal())
pom.groupId = 'com.example'
pom.version = '0.5'
pom.artifactId = 'myplugin'
}
}
}
As you can see, I am using range dependency and not particular about any version of the 'otherplugin'. When the user includes my plugin in his build.gradle:
buildscript {
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath 'com.example:otherplugin:1.0', 'com.example:myplugin:2.3'
}
}
Gradle always uses the latest version of 'com.example:otherplugin' to process his project. Ideally, I would like the version specified by the user to be downloaded, and used by gradle.
I believe this is a problem with my plugin's build.gradle. How can I modify my plugin's build.gradle so that, this transitive dependency is avoided ?

no gradle file in eclipse project Android studio

I am a complete noob with Gradle. I am writing an android application using Android Studio with a co-worker who is using Eclipse. we are sharing our files through git. It was created in eclipse without Gradle. My question is, is it possible to generate the gradle files for the project on my end without him having to export the project on his? I am trying to setup ActionBar Sherlock and its causing me all sorts of trouble I think the lack of a grable.build file might have something to do with it.
You can include abs in your project as a library.
Put a build.gradle in abs module:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+' // You can use also classpath 'com.android.tools.build:gradle:0.6.+' with gradle 1.8
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:18.0.0'
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1" //use your build version
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
Then in your project module add in build.gradle:
dependencies {
compile project(':libraries:actionbarsherlock') //Use your name
}
You have to setting also setting.gradle
include ':MyApplication'
include ':libraries:actionbarsherlock'
You can see this post:
http://gmariotti.blogspot.it/2013/06/quick-tips-convert-to-new-gradle-based.html
Otherwise, you can ignore the library folder and only use in your project module this script in build.gradle.
Gradle will download from maven the aar format.
dependencies {
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
}
All you need is a simple build.gradle file for your ActionBarSherlock project. Here's one I've created:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:13.0.0'
}
android {
buildToolsVersion "17.0"
compileSdkVersion 17
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}

how to copy the dependencies libraries JARs in gradle

I got a runnable jar with this build.gradle
apply plugin: 'java'
apply plugin: 'application'
manifest.mainAttributes("Main-Class" : "com.test.HelloWorld")
repositories {
mavenCentral()
}
dependencies {
compile (
'commons-codec:commons-codec:1.6',
'commons-logging:commons-logging:1.1.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpcore:4.2.1',
'org.apache.httpcomponents:httpmime:4.2.1',
'ch.qos.logback:logback-classic:1.0.6',
'ch.qos.logback:logback-core:1.0.6',
'org.slf4j:slf4j-api:1.6.0',
'junit:junit:4.+'
)
}
but it run failed, because the dependencies jars can't find.
and then I add this code:
task copyToLib(type: Copy) {
into "$buildDir/output/libs"
from configurations.runtime
}
but nothing change. I can't find the folder output/libs.
how can I copy the dependencies libs jars to a specified folder or path?
Add:
build.dependsOn(copyToLib)
When gradle build runs, Gradle builds tasks and whatever tasks depend on it (declared by dependsOn). Without setting build.dependsOn(copyToLib), Gradle will not associate the copy task with the build task.
So:
apply plugin: 'java'
apply plugin: 'application'
manifest.mainAttributes('Main-Class': 'com.test.HelloWorld')
repositories {
mavenCentral()
}
dependencies {
compile (
'commons-codec:commons-codec:1.6',
'commons-logging:commons-logging:1.1.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpclient:4.2.1',
'org.apache.httpcomponents:httpcore:4.2.1',
'org.apache.httpcomponents:httpmime:4.2.1',
'ch.qos.logback:logback-classic:1.0.6',
'ch.qos.logback:logback-core:1.0.6',
'org.slf4j:slf4j-api:1.6.0',
'junit:junit:4.+'
)
}
task copyToLib(type: Copy) {
into "${buildDir}/output/libs"
from configurations.runtime
}
build.dependsOn(copyToLib)
I find the application plugin way too cumbersome and too verbose in its output. Here's how I finally got a setup I was happy with, i.e., create a distribution zip file with dependency jars in subdirectory /lib and add all dependencies to Class-Path entry in the manifest file:
apply plugin: 'java'
apply plugin: 'java-library-distribution'
repositories {
mavenCentral()
}
dependencies {
compile 'org.apache.commons:commons-lang3:3.3.2'
}
// Task "distZip" added by plugin "java-library-distribution":
distZip.shouldRunAfter(build)
jar {
// Keep jar clean:
exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF'
manifest {
attributes 'Main-Class': 'com.somepackage.MainClass',
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')
}
// How-to add class path:
// http://stackoverflow.com/questions/22659463/add-classpath-in-manifest-using-gradle
// https://gist.github.com/simon04/6865179
}
Hosted as a gist here.
The result can be found in build/distributions and the unzipped contents look like this:
lib/commons-lang3-3.3.2.jar
MyJarFile.jar
Contents of MyJarFile.jar#META-INF/MANIFEST.mf:
Manifest-Version: 1.0
Main-Class: com.somepackage.MainClass
Class-Path: lib/commons-lang3-3.3.2.jar
Since Gradle 6.0 it is:
tasks {
val deps by registering(Copy::class) {
from(configurations.runtimeClasspath)
into("build/deps")
}
}
The problem with all the previous answers is that they only collect dependencies from one configuration. To get ALL of the dependencies, you should use this:
task saveDependencies(type: Copy){
configurations.each {
if (it.isCanBeResolved())
from it into "gradle_dependencies"
}
from buildscript.configurations.classpath into "gradle_dependencies"
}
The application plugin requires you to set the main class name like this:
mainClassName = "com.test.HelloWorld"
You will need to add that to your build script. Keep in mind that if you try to run your application with the java command you will also need to set the classpath with -cp.
The application plugin simplifies this process by providing the task distZip. If you run that task you a full distribution is created for you under build/distributions. The distribution contains start scripts and all dependencies. The generated start scripts already set the classpath for you so you don't have to deal with it anymore.
The java plugin can pack a jar with dependencies and there's no need for the application plugin. A task like the following would do:
task buildWithDeps(type: Jar) {
manifest {
attributes "Main-Class": "com.test.HelloWorld"
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
As of at least Gradle 5.6.4 you'll want to do something closer to this.
dependencies {
implementation 'my.group1:my-module1:0.0.1'
implementation 'my.group2:my-module2:0.0.1'
}
jar {
from {
configurations.compileClasspath.filter { it.exists() }.collect { it.isDirectory() ? it : zipTree(it) }
}
}
For Gradle 7.4 with Groovy:
configurations {
externalLib.extendsFrom(implementation)
}
task copyLibs(type: Copy){
from configurations.externalLib{
into '<dest-dir-name>'
exclude('<if any jars need to be excluded>')
}
}