Gradle - build jar which includes both .class and .java files? - android-studio

I'm using android studio + gradle. I have a module that is a plain standalone java app. Is there a way I can package that module into a .jar file that includes both its compiled .class files, and its source .java files? By default it looks like android studio is only including .class files for me.
I was originally doing this using Eclipse (export as a jar), but can't figure out how to do the equivalent with android studio.
---- EDIT -----
This is my current build.gradle file, but it still outputs a jar that only includes the .class files:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
task sourcesJar(type: Jar) {
from sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts {
archives sourcesJar
}
Thanks

It is most common to publish a separate sources JAR from compiled classes.
To do that in Gradle, add this to your Gradle build:
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts {
archives sourcesJar
}

Related

How to create a new test folder called Kotlin for android studio

Android Studio 3.1 Canary 4
Build #AI-171.4444016, built on November 10, 2017
JRE: 1.8.0_152-release-1012-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.13.16-302.fc27.x86_64
Hello,
I have the following project structure. However, as I have some kotlin files I want to create a new folder called kotlin under test and create a new package and store all my kotlin files there.
Currently I have one called Java, but I want to create the kotlin, but can't seem to be able to find out how to do it.
I would like to do the same for androidTest as well, so I can separate all my java and kotlin files.
Would like to give credit to denvercoder9 who posted the link to the proandoriddev.com article which solved my problem.
In my build.gradle file I have the following which worked for both the test and androidTest folders. However, I needed to create the folders first.
sourceSets {
main { java.srcDirs = ['src/main/java', 'src/main/kotlin'] }
test.java.srcDirs += 'src/test/kotlin'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
If anyone need any help on this you can reply back to this solution.
Android Testing with Kotlin
Basically, the idea is to showcase how we can test our android applications using Kotlin, so as a first step we need to setup and prepare our environment by adding Kotlin dependencies in our build.gradle file:
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.5-2'
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
...
dependencies {
...
compile "org.jetbrains.kotlin:kotlin-stdlib:1.0.6"
...
testCompile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.6'
testCompile 'org.jetbrains.kotlin:kotlin-test-junit:1.0.6'
testCompile "com.nhaarman:mockito-kotlin:1.1.0"
testCompile 'org.amshove.kluent:kluent:1.14'
}
Now we need to set the dedicated directories for tests written in Kotlin, this is done in our sourceSets section:
android {
...
sourceSets {
test.java.srcDirs += 'src/test/kotlin'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
...
}
This tutorials walks us through the process of using Java and Kotlin in a single IDEA project.

Download and commit gradle dependencies and plugins in Android Studio

This is an excerpt from a build.gradle file for one of my modules. I'm using android-studio-1.5.1 and gradle 2.10.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.google.android.gms:play-services-gcm:9.0.0'
compile 'com.squareup.picasso:picasso:2.5.2'
}
I also have classpath 'com.google.gms:google-services:3.0.0' in the project level build.gradle file.
I'm trying to gather all the associated jars into a directory which I can commit to my git repo. Something like:
task copyRuntimeLibs(type: Copy) {
into "${projectDir}/libs"
from configurations.compile
}
(This does not work)
Also, I'm not trying to download the sources or javadocs.
I need to be able to commit all dependencies so that the project can be shared on an intranet without internet access.
I've written a plugin which will download all jars and poms. See the code here to download all jars and all poms from a Gradle Configuration
Note: There's a failing test here which shows that the parent pom's are not being downloaded. I've raised an issue here on Gradle's github.
I will likely improve the plugin to invoke the Maven Model Builder APIs to get the parent poms.
FYI - I've already integrated the ModelBuilder APIs successfully with Gradle (see here and here) so shouldn't be too difficult.

Gradle - How to add war as dependency in simple java project

I have a war file which have classes which needs to be used in my Java project. How can I add war file as dependency in this Java project? Gradle pick jar file but no war file. Is there a way to add war as dependency.
build.gradle
group 'com.asklytics'
version 'unspecified'
apply plugin: 'java'
sourceCompatibility = 1.5
repositories {
mavenCentral()
maven { url "../$localMavenRepoRoot/local-maven-repo" }
}
dependencies {
compile group: 'com.asklytics', name: 'asklytics-mailer', version: '1.0-SNAPSHOT', changing: true
testCompile group: 'junit', name: 'junit', version: '4.11'
}
A war structure is different from a jar structure. You can make gradle pick up a file named something.war as a dependency using #war in the dependency identifier, but you'll likely not be able to use the classes that live in the war.
Probably the best way to do this is to make the project that produces the war, also publish a jar file, which you can then use as a dependency.

Where do i add my dependencies? In which build.gradle to put them?

I am requested to add a few dependencies. I know They should be added on build.gradle, but in the dependencies section is written:
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
I am new to android so from my small experience and googling there are supposed to be two build.gradle files, and only in one of them I should add dependencies, but I can not find an extra build.gradle file!?
I will be happy for help! Where should I add my dependencies and where did my second build.gradle disappear?
Gradle is a bit of an odd tool.
https://docs.gradle.org/current/userguide/artifact_dependencies_tutorial.html
They state here how it works. There is only one build.gradle per project that will pull and manage dependencies for you.
Android Studio extends this. There is one 'main' build.gradle for the entire project, and then for each submodule there is a build.gradle since they are run as separate programs. in the master project build.gradle, put dependencies that effect everything you are doing in the build process, and then for each module dependencies specific for those modules. That's what it's saying.
http://developer.android.com/tools/building/configuring-gradle.html
**EDIT: **
Android Studio docs:
Declare dependencies
The app module in this example declares three dependencies:
dependencies {
// Module dependency
compile project(":lib")
// Remote binary dependency
compile 'com.android.support:appcompat-v7:19.0.1'
// Local binary dependency
compile fileTree(dir: 'libs', include: ['*.jar']) }
Each of these dependencies is described below. The build system adds all the compile
dependencies to the compilation classpath and includes them in the
final package.
Gradle docs:
Example 7.1. Declaring dependencies
build.gradle
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
Dependencies can be listed in a bunch of different ways.

How to add Android Studio plugin as dependency in Gradle?

I installed the TestNG plugin through Android Studio. On my filesystem the following files were installed:
C:\Program Files\Android\Android Studio\plugins\testng\lib\testng.jar
C:\Program Files\Android\Android Studio\plugins\testng\lib\testng-plugin.jar
C:\Program Files\Android\Android Studio\plugins\testng\lib\resources_en.jar
C:\Program Files\Android\Android Studio\gradle\gradle-2.2.1\lib\plugins\testng-6.3.1.jar
I added the the TestNG dependency to the build.gradle of my Module--not "Project":
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
testCompile 'testng:testng:6.3.1' // Added this line
}
But Gradle couldn't find the library:
Error:Gradle: A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugUnitTestCompile'.
> Could not find testng:testng:6.3.1.
Searched in the following locations:
https://jcenter.bintray.com/testng/testng/6.3.1/testng-6.3.1.pom
https://jcenter.bintray.com/testng/testng/6.3.1/testng-6.3.1.jar
file:/C:/Users/UserFoo/AppData/Local/Android/sdk/extras/android/m2repository/testng/testng/6.3.1/testng-6.3.1.pom
file:/C:/Users/UserFoo/AppData/Local/Android/sdk/extras/android/m2repository/testng/testng/6.3.1/testng-6.3.1.jar
file:/C:/Users/UserFoo/AppData/Local/Android/sdk/extras/google/m2repository/testng/testng/6.3.1/testng-6.3.1.pom
file:/C:/Users/UserFoo/AppData/Local/Android/sdk/extras/google/m2repository/testng/testng/6.3.1/testng-6.3.1.jar
Required by:
MyBlackjack2:app:unspecified
How do I add TestNG to the search path? On the other hand, Android Studio installed the plugin in that location for a reason, but what's the best way to include it in my project?
Use TestNG 6.9.4, which is distributed on both JCenter and Maven (6.3.1 is on Maven Central only, which you don't seem to have in your repositories list).

Resources