How can I add a linked source folder in Android Studio? - java

In Eclipse I can add a source folder to my Android project as a "linked source folder". How do I achieve the same thing in Android Studio?
Or is it possible to add an external folder to build in Gradle?

In your build.gradle file, add the following to the end of the Android node:
android {
....
....
sourceSets {
main.java.srcDirs += 'src/main/<YOUR DIRECTORY>'
}
}

The right answer is:
android {
....
....
sourceSets {
main.java.srcDirs += 'src/main/<YOUR DIRECTORY>'
}
}
Furthermore, if your external source directory is not under src/main, you could use a relative path like this:
sourceSets {
main.java.srcDirs += 'src/main/../../../<YOUR DIRECTORY>'
}

You can add a source folder to the build script and then sync. Look for sourceSets in the documentation here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Basic-Project
I haven't found a good way of adding test source folders. I have manually added the source to the .iml file. Of course this means it will go away everytime the build script is synched.

While sourceSets allows you to include entire directory structures, there's no way to exclude parts of it in Android Studio (as of version 1.2), as described in Exclude a class from the build in Android Studio.
Until Android Studio gets updated to support include/exclude directives for Android sources, symbolic links work quite well. If you're using Windows, native tools such as junction or mklink can accomplish the equivalent of symbolic links on Unix-like systems. Cygwin can also create these with a little coercion. See: Git symbolic links in Windows and How to make a symbolic link with Cygwin in Windows 7.

Here’s a complete Java module Gradle file that correctly generates and references the built artefacts within an Android multi-module application:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "net.ltgt.gradle:gradle-apt-plugin:0.15"
}
}
apply plugin: "net.ltgt.apt"
apply plugin: "java-library"
apply plugin: "idea"
idea {
module {
sourceDirs += file("$buildDir/generated/source/apt/main")
testSourceDirs += file("$buildDir/generated/source/apt/test")
}
}
dependencies {
// Dagger 2 and Compiler
compile "com.google.dagger:dagger:2.15"
apt "com.google.dagger:dagger-compiler:2.15"
compile "com.google.guava:guava:24.1-jre"
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"

This is for Kotlin DSL (build.gradle.kts):
android {
sourceSets["main"].java.srcDirs("src/main/myDirectory/code/")
sourceSets["main"].resources.srcDirs("src/main/myDirectory/resources/")
// Another notation:
// sourceSets {
// getByName("main") {
// java.srcDirs("src/main/myDirectory/code/")
// resources.srcDirs("src/main/myDirectory/resources/")
// }
// }
}

If you're not using Gradle (creating a project from an APK, for instance), this can be done through the Android Studio UI (as of version 3.3.2):
Right-click the project root directory and pick Open Module Settings
Hit the + Add Content Root button (center right)
Add your path and hit OK
In my experience (with native code), as long as your .so files are built with debug symbols and from the same absolute paths, breakpoints added in source files will be automatically recognized.

Related

How would you add a library downloaded as a folder to gradle?

I'm new to Gradle and finding the documentation confusing.
I've downloaded the Jaunt webscraping API, however I can't seem to add it to my project. When downloaded, Jaunt is a folder that contains the jauntx.x.x.jar file inside it as a root for the library. In a normal IntelliJ project, I would simply add a reference to that .jar file.
However, I'm using Gradle as this project requires being built into an executable jar
I need to add the Jaunt library to the project but if I do it the normal way my project recognises the library while I'm developing it but as soon as I try to run it, it tells me that the com.jaunt package is not found.
I assume that it needs to be added to the build.gradle file, however I'm not sure how.
Can anyone advise?
Build.gradle as follows:
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
repositories {
mavenCentral()
}
dependencies {
implementation files("libs/jaunt1.6.0.jar")
}
javafx {
version = "14"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
jar {
manifest {
attributes 'Main-Class': 'org.openjfx.Main'
attributes 'Class-Path': 'org/openjfx/ '
}
}
application {
mainClassName = 'org.openjfx.Main'
}

gradle exclude external resource folder from all jars

Using gradle 5.4 with a project dependency from external folder in a netbeans project.
The external folder contains resources like images, xml and custom objects that can only be created by this netbeans project. These external assets are then used to create binary files that get packed into a separate jar by that netbeans project.
These same external resources are also used during runtime for development in the gradle project. While I need the resources for development in the gradle project, I do not need or want them to be included in any jars anywhere for any reason when using the task build command because only the binaries are needed for distribution.
How to exclude the external resources from any and all jar files in the gradle project but allow them to be used for the classPath so I can run the project?
Some code examples of failure.
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'idea'
sourceSets {
main {
resources {
srcDir '../ExternalResourceFolder/assets'
}
}
}
jar {
exclude('../ExternalResourceFolder/assets/**')
}
dependencies {
runtimeOnly files('../ExternalResourceFolder/assets')
}
jar {
exclude('../ExternalResourceFolder/assets/**')
}
distributions {
main {
contents {
exclude '../ExternalResourceFolder/assets/**'
}
}
}
Tried many more things like adding to classPath and exclude but it would just be clutter to add them. Changing from sourceSet to dependency only moves the problem around from "build/lib" folder to "build/distributions" folder.
Had to exclude per file type in the end.
sourceSets {
main {
resources {
srcDir '.'
exclude ('**/*.j3odata','**/*.mesh','**/*.skeleton',\
'**/*.mesh.xml','**/*.skeleton.xml','**/*.scene',\
'**/*.material','**/*.obj','**/*.mtl','**/*.3ds',\
'**/*.dae','**/*.blend','**/*.blend*[0-9]','**/*.bin',\
'**/*.gltf')
}
}
}

Java Import build.gradle file for minecraft mod

I have been following this tutorial - https://mcforge.readthedocs.io/en/latest/gettingstarted/
- and I am stuck on this section - Launch IDEA and choose to open/import the build.gradle file, using the default gradle wrapper choice. While you wait for this process to finish, you can open the gradle panel, which will get filled with the gradle tasks once importing is completed.
How do I import the build.gradle file? what is the build.gradle file? what does it do? I am new to coding, any help is appreciated. thx
Launch IDEA and select "File" → "New" → "Project from Existing Sources"
Select build.gradle file from the unpacked archive from the site you've provided
Check wrapper settings on the next screen. Leave the defaults.
Wait till IDEA builds the projects and makes indexes.
Happy hacking!
build.gradle is basically a build configuration file. It describes the way a piece of software is made. Like: where is the source code, what are the project's dependencies, where to get and how to link them, how to test and so on.
Speaking about particular build.gradle from forge-mdk:
buildscript {
repositories {
jcenter()
maven { url = "https://files.minecraftforge.net/maven" }
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT'
}
}
apply plugin: 'net.minecraftforge.gradle.forge'
This part applies net.minecraftforge.gradle.forge plugin that, I guess, is used to build Minecraft mods. As this is a third-party plugin buildscript block adds a repository (https://files.minecraftforge.net/maven) where it can be downloaded.
version = "1.0"
group = "com.yourname.modid" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "modid"
This part describes the result ("artifact") of the projects. It has version 1.0, name modid and will be published (if published) under com.yourname.modid group. This is a Maven related vocabulary. I guess, you'll need to replace this values with your own.
sourceCompatibility = targetCompatibility = '1.8' // Need this here so eclipse task generates correctly.
compileJava {
sourceCompatibility = targetCompatibility = '1.8'
}
Here you state that the projects is built with Java 8
minecraft {
version = "1.12.2-14.23.5.2775"
runDir = "run"
mappings = "snapshot_20171003"
}
Here you configure net.minecraftforge.gradle.forge plugin that you've added previously. Basically, any plugin can expose it's own configuration block and you'll need to read the docs to know what do the values mean.
dependencies {
…
}
The project has no dependencies yet, thus empty dependencies block
processResources {
// this will ensure that this task is redone when the versions change.
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
// replace stuff in mcmod.info, nothing else
from(sourceSets.main.resources.srcDirs) {
include 'mcmod.info'
// replace version and mcversion
expand 'version':project.version, 'mcversion':project.minecraft.version
}
// copy everything else except the mcmod.info
from(sourceSets.main.resources.srcDirs) {
exclude 'mcmod.info'
}
}
Here you configure built-int processResources task that… processes resources. As you see, things are self-descriptive in Gradle. Tasks are Java classes that has documentation. For example, here are the docs for ProcessResources. One more link for DSL reference
Hope this answer will get you some info to start with!

Gradle compile dependency outside of project

I have a frontend and a backend repository for my app. The backend is written in Go and serves the API over gRPC. The generated gRPC Java files end up in backend-repo/proto-gen/java/ (so backend-repo/proto-gen/java/com/myApp/Users.java).
In my frontend android repo I have submoduled the backend repo to a folder called server. I want to modify my build.gradle to compile the .java files from the backend.
android-repo/
app/
build.gradle
server/proto-gen/java/com/myApp/
Users.java
AnotherService.java
I'm very new to Android development and am struggling to figure out the right approach.
This is a snippet from my app/build.gradle but it fails because it can't find the package com.google.protobuf.
task compileGrpc (type: JavaCompile) {
source = fileTree(dir: '../server/proto-gen/java/', include: '**/*.java')
destinationDir = file('build/classes')
classpath = files('../server/proto-gen/java/')
options.compilerArgs = ["-sourcepath", "$projectDir/../server/proto-gen/java/"]
}
dependencies {
compile 'com.google.protobuf:protobuf-java:3.0.0-alpha-2'
compileGrpc.execute()
}
You can add your dependencies like
dependencies {
classpath files('server/proto-gen/java/com/myApp/')
}
This will accept relative path.
Simply including the path to the src sourceset for the project would include them within the compilation:
android {
...
sourceSets {
main {
java.srcDirs = [java.srcDirs, 'server/proto-gen/java']
}
}
}
You'll be able to see the src files/directories included within the Android View/Project Explorer on the right side and edit the files directly.

Android 3rd Party Library Integration

this is kinda crazy. I already worked often with 3rd party librarys. All I had to do was adding the compile command in the build gradle but in this case its something different. This is the github link of the lib i want to implement:
Link to github
I tried to integrate the files I need manually but i got a amount of errors.
In the README the developer says:
This library is provided as the AAR format. The source jar file won't be downloaded automatically (due to the current Gradle and Android Studio limitation), so javadoc comments are not displayed on IDE.
Can somebody help me how to integrate this the best way.
If you have a look below, they have already provided a workaround
buildscript {
repositories {
maven { url 'https://raw.github.com/xujiaao/mvn-repository/master/releases' }
}
dependencies {
classpath 'com.github.xujiaao:aarLinkSources:1.0.0'
}
}
apply plugin: 'com.android.application'
apply plugin: 'aar-link-sources'
android {
...
}
dependencies {
compile ('com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.8.0#aar'){
transitive=true
}
aarLinkSources 'com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.8.0:sources#jar'
}

Categories

Resources