How do I add debug/release specific generated source to Android Gradle? - gradle

I have the following sourceSet entry. Each directory contains the autogenerated java source files, which should be compiled for each respective debug/release build.
sourceSets {
debug {
java.srcDirs = [generatedDebugCodeDir]
}
release {
java.srcDirs = [generatedReleaseCodeDir]
}
}
However, I do not see the directories added to the classpath (by using the --debug Gradle flag). How should I address this? I tried java.srcDir (no S), which is not even recognized.
One workaround I'm considering is just generating the files in the appropriate generated buildConfig dirs. Hopefully there's no crazy side effect.

Turns out, it will be better for me to use the flavor specific java.srcDirs. This setup worked for me.
flavor1Debug{
java.srcDirs = ["${generatedJavaDirRoot}/flavor1/debug/"]
}
flavor1Release{
java.srcDirs = ["${generatedJavaDirRoot}/flavor1/release/"]
}
flavor2Debug{
java.srcDirs = ["${generatedJavaDirRoot}/flavor2/debug/"]
}
flavor2Release{
java.srcDirs = ["${generatedJavaDirRoot}/flavor2/release/"]
}

Related

How to change java.srcDirs in gradle sourceSets dynamically?

By default, thejava.srcDirs in sourceSets is declare like the following:
android {
sourceSets {
gdt{
java.srcDirs = ['src/gdt/java']
}
open {
java.srcDirs = ['src/open/java']
}
}
}
How to change it dynamically.
...What i wanna do is to select different files to build an aar according to the input from command line.
Any better idea for achieving this?

Can Gradle produce multiple Kotlin Native binaries (for one OS)?

Can I convince Gradle to produce multiple binaries? I have several Kotlin packages with files that have a proper "fun main(...)" but the default IntelliJ build.gradle file only allows me to specifiy one "compilations.main.entryPoint".
I could put the main functions into Kotlin classes or objects if that would help.
Changing the entryPoint argument to an array did not work :)
If it's not currently possible, is it a general limitation of Gradle or only of the "kotlin-multiplatform" plugin?
plugins {
id 'kotlin-multiplatform' version '1.3.11'
}
repositories {
mavenCentral()
}
kotlin {
targets {
// For ARM, preset should be changed to presets.iosArm32 or presets.iosArm64
// For Linux, preset should be changed to e.g. presets.linuxX64
// For MacOS, preset should be changed to e.g. presets.macosX64
fromPreset(presets.mingwX64, 'mingw')
configure([mingw]) {
// Comment to generate Kotlin/Native library (KLIB) instead of executable file:
compilations.main.outputKinds('executable')
// Change to specify fully qualified name of your application's entry point:
compilations.main.entryPoint = 'hello.main'
}
}
sourceSets {
// Note: To enable common source sets please comment out 'kotlin.import.noCommonSourceSets' property
// in gradle.properties file and re-import your project in IDE.
mingwMain {
}
mingwTest {
}
}
}
task runProgram {
def buildType = 'debug' // 'release' - Change to 'debug' to run application with debug symbols.
dependsOn "link${buildType.capitalize()}ExecutableMingw"
doLast {
def programFile = kotlin.targets.mingw.compilations.main.getBinary('EXECUTABLE', buildType)
exec {
executable programFile
args ''
}
}
}
In https://github.com/JetBrains/kotlin-native/issues/2505 I've just got the answer that this will be possible with Kotlin Native 1.3.20!

How to add gradle generated source folder to Eclipse project?

My gradle project generates some java code inside gen/main/java using annotation processor. When I import this project into Eclipse, Eclipse will not automatically add gen/main/java as source folder to buildpath. I can do it manually. But is there a way to automate this?
Thanks.
You can easily add the generated folder manually to the classpath by
eclipse {
classpath {
file.whenMerged { cp ->
cp.entries.add( new org.gradle.plugins.ide.eclipse.model.SourceFolder('gen/main/java', null) )
}
}
}
whereby null as a second constructor arg means that Eclipse should put the compiled "class" files within the default output folder. If you want to change this, just provide a String instead, e.g. 'bin-gen'.
I think it's a little bit cleaner just to add a second source directory to the main source set.
Add this to your build.gradle:
sourceSets {
main {
java {
srcDirs += ["src/gen/java"]
}
}
}
This results in the following line generated in your .classpath:
<classpathentry kind="src" path="src/gen/java"/>
I've tested this with Gradle 4.1, but I suspect it'd work with older versions as well.
Andreas' answer works if you generate Eclipse project from command line using gradle cleanEclipse eclipse. If you use STS Eclipse Gradle plugin, then you have to implement afterEclipseImport task. Below is my full working snippet:
project.ext {
genSrcDir = projectDir.absolutePath + '/gen/main/java'
}
compileJava {
options.compilerArgs += ['-s', project.genSrcDir]
}
compileJava.doFirst {
task createGenDir << {
ant.mkdir(dir: project.genSrcDir)
}
createGenDir.execute()
println 'createGenDir DONE'
}
eclipse.classpath.file.whenMerged {
classpath - >
def genSrc = new org.gradle.plugins.ide.eclipse.model.SourceFolder('gen/main/java', null)
classpath.entries.add(genSrc)
}
task afterEclipseImport(description: "Post processing after project generation", group: "IDE") {
doLast {
compileJava.execute()
def classpath = new XmlParser().parse(file(".classpath"))
new Node(classpath, "classpathentry", [kind: 'src', path: 'gen/main/java']);
def writer = new FileWriter(file(".classpath"))
def printer = new XmlNodePrinter(new PrintWriter(writer))
printer.setPreserveWhitespace(true)
printer.print(classpath)
}
}

How do I replace the default source folders for gradle?

I am new to Gradle and I have a source code location different than what Gradle expects.
Gradle expects to find the production source code under src/main/java and your test source code under src/main/resources. How do I configure Gradle to a different source code?
You have to add few lines to build.gradle:
To replace the default source folders, you will want to use srcDirs instead, which takes an array of the path.
sourceSets {
main.java.srcDirs = ['src/java']
main.resources.srcDirs = ['src/resources']
}
Another way of doing it is:
sourceSets {
main {
java {
srcDir 'src/java'
}
resources {
srcDir 'src/resources'
}
}
}
The same thing is applicable to test folder too.

"Neither path nor baseDir may be null or empty string." error when importing gradle project in Android Studio 0.2.9

NOTE: i can't post links, so i guess you'll need to go here to follow the references. sorry, not my rule.
i'm getting the following error when attempting to import a project into Android Studio 0.2.9:
Could not execute build using Gradle distribution
'http://services.gradle.org/distributions-snapshots/
gradle-1.8-20130830160653+0000-bin.zip'.
A problem occurred configuring project ':library'.
A problem occurred configuring project ':library'.
Failed to notify project evaluation listener.
Neither path nor baseDir may be null or empty
string. path='' basedir='<projects folder>/
drag-sort-listview/library'
Consult IDE log for more details (Help | Show Log)
the project was originally a Maven project (1). i opened it in Eclipse ADT, generated a /librabry/build.gradle file per the instructions at (2).
the Eclipse ADT generated build.gradle looked like:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android-library'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 7
buildToolsVersion "17.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['']
resources.srcDirs = ['']
aidl.srcDirs = ['']
renderscript.srcDirs = ['']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
i had to change line 6 from
classpath 'com.android.tools.build:gradle:0.4'
to
classpath 'com.android.tools.build:gradle:0.5+'
to get Android Studio to stop saying the versions were miss-matched. i also added a /settings.gradle file containing
include ':library'
and a /local.properties file with the contents
# This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/Applications/Android Studio.app/sdk
i then attempted to import the /settings.gradle file by selecting it in the 'File | Import Project...' dialog. i have 'Use Auto-import' checked and 'Use gradle wrapper with verification' option selected in the dialog (3). the full idea.log entry can be viewed at (4).
any help would be greatly appreciated, thanks.
This will happen if you are using Environment Variables like so:
android {
signingConfigs {
release {
storeFile file(RELEASE_STORE_FILE)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
}
And you have not defined those variables in your gradle.properties file located at the root of your project.
To fix this, make sure your variables are defined, here is an example from my gradle.properties file:
RELEASE_STORE_FILE=app_keystore.jks
RELEASE_STORE_PASSWORD=password
RELEASE_KEY_ALIAS=MyAppKey
RELEASE_KEY_PASSWORD=password
The error may be referring to your keystore's path. If the keystore's path doesn't work, it will think it's null. If you just want to use your keystore's file name (instead of the full path), make sure the keystore is in the root directory of your project.
android {
signingConfigs {
release {
storeFile file(**DoubleCheckPath**)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
}
(Answered by the OP in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
ok, solved it. the issue is with the empty lists under android.sourceSets in build.gradle, I commented them out to resolve the error. Here's my current build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android-library'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 7
buildToolsVersion "17.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
// java.srcDirs = ['']
// resources.srcDirs = ['']
// aidl.srcDirs = ['']
// renderscript.srcDirs = ['']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
hope that helps y'all.
you may miss a file in project, for "gradle.properties" file, because this file is include some varies, for example , our custom var, such key.storefile key.keypassword, you should check this file is exists or not.
Modify file(String.valueOf(RELEASE_STORE_FILE)) in
android {
signingConfigs {
release {
storeFile file(RELEASE_STORE_FILE)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
}

Resources