I'm trying to setup my project so i can run unit tests and automation tests thru Android Studio. After many attempts i ended up with this structure on my project.
Project
-> Android Module (With the real app where i run the automation tests)
-> Java Library Module (With business logic classes and all things that i need to run unit tests)
I'm also referencing some Jar Lib on the Java Module in order to use some common code that i've wrote.
The Android Module references the Library Module as can be seen on the following sample gradle script:
dependencies {
compile project(':Core')
compile files(':Core/libs/fake_lib.jar')
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
}
The second line on the dependencies section was need for the project can get assembled and run, but the problem is that on the first call to any class that have some reference to the library, the app crashes with a NoClassDefFoundError
Does anyone knows why and how to fix?
Since this JAR is a common library that i will use to many projects, i ended up placing it on my local maven repository and referenced it like other maven artifacts.
This way i don't have to reference it on both projects (Android and Java Library) just in the library, which i think is better approach.
Related
I am very new to both the Android and JVM platforms. Using Android Studio, I would like to create an Android app and put most of my business logic in a library. I would also like to just use Kotlin, no Java.
When I go to File > New Module, the options listed are
Phone & Tablet module
Android Library
Instant App
Feature Module
Android Wear Module
Android TV Module
Android Things Module
Import Gradle Project
Import Eclipse ADT Project
Import .JAR/.AAR Package
Java Library
I can create a Kotlin-based library with the Android Library option, but this also includes a lot of files for dealing with resources and controls.
I just want a pure Kotlin library. What is the easiest way to create one?
Can I delete a portion of an Android Library?
Can I change some settings in a Java Library?
Can I download a plugin that will just give me the option to create a Kotlin library?
I am still a bit confused the file organization in Java/Kotlin/Android projects.
You need a module with no Android dependencies and resources - this is what a Java library module does, so you start by creating one of those. From there, you just need to enable Kotlin inside this module.
The build.gradle file you get when you create your module is something like this:
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
You need to add the Kotlin plugin and standard library. If you don't have the mavenCentral repository added for all modules in your project yet, you need to add that as well:
apply plugin: 'java-library'
apply plugin: 'kotlin'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}
This is assuming that you have a kotlin_version declared in your project level build.gradle file, which is the usual way for Android projects, and I believe that's how Android Studio configures a project if you tick the checkbox to use Kotlin.
With those changes to your library's build.gradle, you can remove the automatically generated MyClass.java file from your library's source folder (/src/main/java/your/package/name), and start adding Kotlin files instead. If you'd like, you can also rename the /src/main/java folder to /src/main/kotlin. Or you can use both folders, this is entirely up to you.
Create Android studio project
After that create as many as module which are basically Android library
You can select the language of module (java/kotlin)
Extra
Upload your module to github and use jitpcak tool to make your module a library like this
Implementation 'com. My. Module:1.0'
And use your module any future project
These are the dependencies of a library module called "mylibrary1" inside project 1
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api 'com.android.volley:volley:1.0.0'
...
}
The keyword api is here to transitively expose this dependency to the consumers of my own library (I'm using Gradle 3). Everything goes fine and the .aar file is made.
Then let's move to another project 2 which is the consumer of the library. I click on file > new module > import .jar/.aar and select the aar file that I made before.
I checked if settings.gradle includes the new module, then a dependency is added to the consumer in this way
api project(':mylibrary1')
Again, the keyword api is necessary to reuse the code from another project that will use the aar of project 2. Anyway, it doesn't change at all even if I put the keyword implementation.
From the classes of project 2 I can see all of my classes in mylibrary1, except those in Volley.
What am I doing wrong? How should I do to solve this problem?
When you package your AAR, it won't include the transitive dependencies, only your own code. So you'll need to add a Volley dependency in your project 2.
Upgrade to Android Studio 3.0.0 mentions this, and doesn't elaborate on how to handle it:
Library modules no longer process local JARs. This is to speed up incremental builds that are caused by changes to a library module's code.
So I have a project with a library project in it. In my library project's build.gradle file I have this:
compile files('libs/com.somelib.somepackage.jar')
I changed compile to implementation and when I tried to run my app, all my classes that tried to access the import com.somelib.somepackage.SomeClass import statement threw an error that this package didnt exist.
I changed back to compile and I was able to build and run my app.
I want to comply to the new rules since compile is deprecated and will be removed with the next Gradle release, so how do I go about doing that?
If you are trying to access classes from the .jar that is included in the library project from the app project, you will have to use api instead of implementation otherwise the classes will only be accessible in the library project:
implementation files('libs/com.somelib.somepackage.jar')
should be
api files('libs/com.somelib/somepackage.jar')
As said by the documentation:
... When a module includes an api dependency, it's letting Gradle know
that the module wants to transitively export that dependency to other
modules, so that it's available to them at both runtime and compile
time ...
Reference:
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations
Apologies for what may seem an idiotic post.
How do you run Mockito on the newest version of Android Studio SDK?
and can you run multiple tests using Mockito using the Android Studio platform?
I've used Mockito on Eclipse and ran as much as 6 tests in the same window. But I'm trying to figure out how to do this on the Android Studio platform and I cannot find any website or tutorial with an answer.
Android Studio 1.1 now has built-in support for unit testing. From Unit testing support - Android Tools Project Site:
Unit tests run on a local JVM on your development machine. Our gradle plugin will compile source code found in src/test/java and execute it using the usual Gradle testing mechanisms. At runtime, tests will be executed against a modified version of android.jar where all final modifiers have been stripped off. This lets you use popular mocking libraries, like Mockito.
You will have to specify your testing dependencies in the build.gradle file of your android module. For example:
dependencies {
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}
The page also contains a step-by-step guide for setting up Android Studio for unit testing, including creating a separate directory for unit tests:
Create a directory for your testing source code, i.e. src/test/java. You can do this from the command line or using the Project view in the Project tool window. The new directory should be highlighted in green at this point. Note: names of the test source directories are determined by the gradle plugin based on a convention.
I'm currently working on a project using junit 4.12 and Mockito 2.0.5 beta for unit testing in Android Studio 1.1, and haven't had any issues:
dependencies {
// ...
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:2.0.5-beta"
}
As far as running multiple tests at the same time, do you mean test cases? Test classes? Test suites? Please clarify, and I'll update my answer, if needed.
Open your app/build.gradle file in your application and add mockito to the dependencies, if dependencies isn't there you can go ahead and create it.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile 'org.mockito:mockito-core:1.10.8'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.1'
}
Then in your unit tests, just create a mock object as you normally would:
http://site.mockito.org/#how
Unit tests should be under the app/src/androidTest/ folder.
I can verify that the accepted answer is correct however just to further to the answer, there will be an androidTest folder alongside your main folder. Normally you would use the androidTest folder for instrumentation tests. Just make sure that under the build variants panel, the Test Artifact: is selected to be "Unit Tests" otherwise the testCompile in build.gradle will not work. It took me a while to figure this part of out.
Hope it helps.
I'm using Square's Wire library for my Android app, using Android Studio with Gradle.
I originally added the wire-runtime-1.2.0.jar into a libs folder in my module, and added the dependency to Gradle like this in my build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
That worked fine.
I'm new to Gradle and Android Studio, but based on the way I'm depending on the Google Support and Play Services libraries, I thought I might be able to remove the wire-runtime-1.2.0.jar library from my repository and just declare a dependency like this (the line is from the Maven repository):
dependencies {
compile 'com.squareup.wire:wire:1.0.0'
}
But if I do that then I hit this error:
Gradle: package com.squareup.wire does not exist
Is there a way to set up this dependency without importing the JAR file directly? Or does that only work for libraries that you can install through the SDK Manager?
Some packages, like com.squareup.wire, have multiple artifacts in Maven Central. You need to choose the right one for your needs. In this case, the equivalent of wire-runtime-1.2.0.jar is the wire-runtime artifact, not the wire artifact.
Here's what your dependencies section should look like:
dependencies {
compile 'com.squareup.wire:wire-runtime:1.2.0'
}