In my android application I need to generate source code and use it in the app.
For that I created task genSources (using tutorials) for source generation. It works correctly if run it separately.
In my case I need to run source code generation automatically.
From tutorial I found out the following command:
compileJava.dependsOn(genSources)
but that is unknown command for the apply plugin: 'com.android.library'
gradle throws following exception:
Error:(35, 0) Could not find property 'compileJava' on project ':data'.
Looks like it can be used with apply plugin: 'Java'
but I cannot use these 2 plugins together
How can I solve this issue and generate needed source code before compilation?
build.gradle
apply plugin: 'com.android.library'
configurations {pmd}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
buildscript {
repositories {
maven {
url "http://repo1.maven.org/maven2/"
}
}
dependencies {
classpath group: 'net.sourceforge.fmpp', name: 'fmpp', version: '0.9.14'
}
ant.taskdef(name: 'fmpp', classname:'fmpp.tools.AntTask', classpath: buildscript.configurations.classpath.asPath)
}
task genSources << {
println "Generating sources...."
ant.fmpp configuration:"src/main/resources/codegen/config.fmpp",
sourceRoot:"src/main/resources/codegen/templates",
outputRoot:"target/generated-sources/main/java";
}
compileJava.dependsOn(genSources)
sourceSets {
main {
java {
srcDir 'target/generated-sources/main/java'
}
}
}
dependencies {
...
}
UPDATED
I was found some solution which at least not throw exception
gradle.projectsEvaluated {
compileJava.dependsOn(genSources)
}
Then I execute gradle build but nothing happens
With gradle 2.2+ this should work:
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn genSources
}
If you also want it to happen when you evaluate (e.g. when syncing your project with gradle in android studio) you can do it like this:
gradle.projectsEvaluated {
preBuild.dependsOn genSources
}
Related
In my android application I need to generate source code and use it in the app.
For that I created task genSources (using tutorials) for source generation. It works correctly if run it separately.
In my case I need to run source code generation automatically.
From tutorial I found out the following command:
compileJava.dependsOn(genSources)
but that is unknown command for the apply plugin: 'com.android.library'
gradle throws following exception:
Error:(35, 0) Could not find property 'compileJava' on project ':data'.
Looks like it can be used with apply plugin: 'Java'
but I cannot use these 2 plugins together
How can I solve this issue and generate needed source code before compilation?
build.gradle
apply plugin: 'com.android.library'
configurations {pmd}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
buildscript {
repositories {
maven {
url "http://repo1.maven.org/maven2/"
}
}
dependencies {
classpath group: 'net.sourceforge.fmpp', name: 'fmpp', version: '0.9.14'
}
ant.taskdef(name: 'fmpp', classname:'fmpp.tools.AntTask', classpath: buildscript.configurations.classpath.asPath)
}
task genSources << {
println "Generating sources...."
ant.fmpp configuration:"src/main/resources/codegen/config.fmpp",
sourceRoot:"src/main/resources/codegen/templates",
outputRoot:"target/generated-sources/main/java";
}
compileJava.dependsOn(genSources)
sourceSets {
main {
java {
srcDir 'target/generated-sources/main/java'
}
}
}
dependencies {
...
}
UPDATED
I was found some solution which at least not throw exception
gradle.projectsEvaluated {
compileJava.dependsOn(genSources)
}
Then I execute gradle build but nothing happens
With gradle 2.2+ this should work:
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn genSources
}
If you also want it to happen when you evaluate (e.g. when syncing your project with gradle in android studio) you can do it like this:
gradle.projectsEvaluated {
preBuild.dependsOn genSources
}
I am learning Gradle for Android recently. For learning impressively, i create all android application files manually.
my project dirs as below
package name : com.wonbin.gradledemo
project build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
}
}
allprojects {
repositories {
jcenter()
}
}
app module build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.wonbin.gradledemo"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "adroid.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
lintOptions {
htmlReport true
htmlOutput file("lint-results.html")
}
}
dependencies {
compile fileTree(dir:'libs',include:['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{
exclude group: 'com.android.support',module: 'support-annotations'})
compile 'com.android.support:appcompat-v7:23.4.0'
testCompile 'junit:junit:4.12'
}
./gradlew build is successful and there are 'intermediates' and 'generated'
dirs , but no outputs dir, i don't know what to do!
Thanks a lot!
You need not only to build, but to assemble it as well.
To do this you can run a gradle task
assembleDebug
Or use Build -> Build APK main menu item
I'm trying to export a signed apk of my exported project from eclispe ADT to Android Studio.
I have 2 issues:
Error:(16, 0) Gradle DSL method not found:
'lintOptions()'
Possible causes:The project 'gigCheck2' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper fileThe build file may be missing a Gradle plugin.
Apply Gradle plugin
And when I'm trying to generate the apk:
Missing Gradle Project Information. Please check if the IDE
successfully synchronized its state with the Gradle Project
Model.
I've two build.gradle
1-Inside root project:
<code>// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
}
}
allprojects {
repositories {
jcenter()
}
}
lintOptions {
abortOnError false
checkReleaseBuilds false
}</code>
2-In the app
<code>
apply plugin: 'com.android.application'
android {
compileSdkVersion 15
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.project.client.android"
minSdkVersion 7
targetSdkVersion 10
}
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard.cfg'
}
}
sourceSets {
main {
manifest.srcFile 'app/src/main/AndroidManifest.xml'
}
}
}
dependencies {
compile files('lib/commons-codec-1.7.jar')
compile files('lib/commons-io-2.4.jar')
compile files('lib/commons-lang-2.6.jar')
compile files('lib/core.jar')
compile files('lib/javase.jar')
compile files('lib/ksoap2-android-assembly-3.0.0.jar')
}
</code>
Remove lintOptions in root level gradle file and add it in app level gradle file.
Your app>build.gradle look like this.
apply plugin: 'com.android.application'
android {
compileSdkVersion 15
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.project.client.android"
minSdkVersion 7
targetSdkVersion 10
}
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard.cfg'
}
}
sourceSets {
main {
manifest.srcFile 'app/src/main/AndroidManifest.xml'
}
}
lintOptions {
abortOnError false
checkReleaseBuilds false
}
}
dependencies {
compile files('lib/commons-codec-1.7.jar')
compile files('lib/commons-io-2.4.jar')
compile files('lib/commons-lang-2.6.jar')
compile files('lib/core.jar')
compile files('lib/javase.jar')
compile files('lib/ksoap2-android-assembly-3.0.0.jar')
}
lint options should be included in your module gradle not project:
android{
defaultConfig {
lintOptions {
abortOnError false
checkReleaseBuilds false
}
}
}
Is is possible to setup robolectric in its own module and add it as a dependency to my project? I can add it to my project module fine but id prefer if it was in its own module. Ive created a javaLibrary module and added the following code to the gradle build script
buildscript {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies {
//Prior to AS 0.5.9: classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.+'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.0'
//previous plugin >> classpath 'com.novoda.gradle:robolectric-plugin:0.0.1-SNAPSHOT'
}
}
allprojects {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
}
apply plugin: 'android'
apply plugin: 'android-test' //previously >> apply plugin: 'robolectric'
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "com.irishtimes.newsapp.tests"
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
androidTest.setRoot('src/tests') //note that this is androidTest instead of instrumentTest
}
}
dependencies {
androidTestCompile 'junit:junit:4.10'
//include the stable robolectric 2.3 library
androidTestCompile 'org.robolectric:robolectric:2.3'
androidTestCompile 'com.squareup:fest-android:1.0.+'
}
//only include files that are suffixed with Test.class and set the max heap size
androidTest {
include '**/*Test.class'
maxHeapSize = "2048m"
}
Ive also added a blank manifest file and a dummy test in a src/tests/java. In my previous setup when i run ./gradlew test, my tests would run and id get feedback. Now when i run the same command my build script runs but it does not pick up my tests at all. If anyone has any reference to tutorials or advice or a quick solution that would be great. Thanks!
Yes its possible with a different plugin. Plugin supported by robolectric do not support this.
Plugin can be found here https://github.com/novoda/gradle-android-test-plugin
A ready to use example can be found here https://github.com/nenick/android-gradle-template
If you want to run tests from a separate folder, but not necessarily a separate module, I have had some success with the Android Studio Unit Test Plugin along with this gradle plugin.
I would like to run ProGuard on release build of my Android library. Below is my build.gradle file that is within my project.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
resources {
srcDir 'src/../lib'
}
}
}
buildTypes {
release {
runProguard true
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}
}
Running gradlew clean or gradlew build returns the following error:
> Could not find method buildTypes() for arguments [build_25r1m0a3etn5cudtt5odlegprd$_run_closure2_closure10#5f3306ad] on project
It seems that plugin android-library is missing the buildTypes method. If I change apply plugin to android, gradlew runs successfully. However, this is a library project and I would like to keep it so. Other parts my my gradle.build (not mentioned here) rely on the project being a library.
Is there any way to make the android-library plugin run ProGuard on build?
Solved my issue by taking release out of buildTypes like so:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
resources {
srcDir 'src/../lib'
}
}
}
release {
runProguard true
proguardFile getDefaultProguardFile('proguard-android.txt')
proguardFile 'proguard-android.txt'
}
}
Also note that I added another proguardFile under release. This fixed another issue I was having after solving the initial buildTypes problem.