How can I run test before generate signed apk? - android-studio

I consider that Android Studio will run test before generate signed apk.
But AS didn't do that for me.It's not nice before package my apk, I need run the tests myself.
I'm not sure if dependsOn or other way can help me.I'm not sure weather my build.gradle has mistakes.
Some relevant code in gradle maybe like this:
defaultConfig {
applicationId "com.xx.xx"
versionCode getDefaultVersionCode()
minSdkVersion 19
targetSdkVersion 19
}
dependencies {
testCompile 'org.robolectric:robolectric:3.0'
testCompile 'junit:junit:4.12'
}
I didn't write testOption.
My directory is like this(content before them is package name):

To run all available tests, when building a release, make the task, that builds the release (e.g. assembleRelease) depend on the test tasks:
android {
// ...
}
afterEvaluate {
assembleRelease.dependsOn testReleaseUnitTest, connectedAndroidTest
}
The afterEvaluate closure is executed after evaluation (when the android tasks have been created). At this time the android tasks can be referenced as variable.
Instead of testReleaseUnitTest you can just use test, which runs unit tests for all variants.
Keep in mind that there are by default no instrumentation tests for the release version of your app (build with assembleRelease). So in the above example, connectedAndroidTest runs the instrumentation tests for the debug version only.

I'm not familiar with Android development, but I think you could achieve your intent with adding this somewhere in your build.gradle:
sign.dependsOn test
Where sign is the signing of apk task (same name as from gradle tasks).

Related

Configuring maven deploy script with Android Experimental Gradle Plugin

I am working on adding NDK builds to a module of a library I already have deployed in maven. In the past, I used the stable Android Gradle plugin v1.5, but do to needs with the NDK build I am trying to use the experimental plugin (I need to specify a shared library to link against). The NDK build is fine, my issue is with my Maven deploy script.
At the end of my module's build script I call
apply from: '../maven_push.gradle'
After all the maven configuration stuff I have the following tasks:
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.sourceFiles
}
task androidJavadocsJar(type: Jar) {
classifier = 'javadoc'
//basename = artifact_id
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
//basename = artifact_id
from android.sourceSets.main.java.sourceFiles
}
artifacts {
//archives packageReleaseJar
archives androidSourcesJar
archives androidJavadocsJar
}
When I try to compile the build script I receive the following error:
Error:Could not find property 'android' on task ':moduleName:androidJavadocs'.
What is odd is that in Android Studio, autocomplete does find android.sourceSets.main.java.sourceFiles if I start typing. I know the experimental plugin is, well, experimental and I am also aware its poorly documented. Google is unfortunately not giving us any good options when it comes to automated building in this matter but I am hoping maybe someone understands enough about gradle to help me with this one.

Make Robolectric run test for the current build type only

I'm using Robolectric alongside Android Studio (v 1.2.2)
classpath 'com.android.tools.build:gradle:1.2.3'
buildToolsVersion "21.1.2"
testCompile 'org.robolectric:robolectric:3.0-rc3'
My project contains 4 build types. In spite of having debug build type selected in the "Build Variants" window, Robolectric tests every declared build type (4 times).
Is it an expected behavior? How can I narrow down execution to just debug build type?
Thanks.
It is quite simple. Let assume you have next:
productFlavours {
one
two
}
So to run tests for on flavour you simply run gradle:
gradle testOneDebug
To run all tests for all variants:
gradle test

Duplicate test execution when run gradle test

When I run >gradle test all the BDD test steps are executed twice. I created the project using intelliJ as my IDE and following are the task that could see under Gradle tasks window.
I choose command line to run the tests with above command
In my build.gradle I don't have test task defined
apply plugin: "groovy"
apply plugin: "idea"
repositories {
mavenCentral()
}
sourceCompatibility = 1.6
targetCompatibility = 1.6
def version = [
'groovy' : '1.8.6',
'junit' : '4.10',
'geb' : '0.7.2',
'selenium' : '2.25.0',
'cucumber' : '1.0.8'
]
ext.drivers = ["htmlunit", "firefox", "chrome"]
dependencies {
groovy "org.codehaus.groovy:groovy-all:$version.groovy"
testCompile "junit:junit:$version.junit"
testCompile "org.codehaus.geb:geb-junit4:$version.geb"
testCompile "info.cukes:cucumber-groovy:$version.cucumber"
testCompile "info.cukes:cucumber-junit:$version.cucumber"
// Drivers
drivers.each { driver ->
testCompile "org.seleniumhq.selenium:selenium-$driver-driver:$version.selenium"
}
}
How can I fix this
I managed to resolve this issue by deleting .idea and build folders and then by re-import the project.
But still curious what the root cause is.
I believe this happened when re-build the project via intelliJ
This happened to me also.
There is some confusion between test run inside Intellij and using Gradle.
When I run a test inside IntelliJ then run test in gradle, they are launched in double. I must run a gradle clean to get back to normal.
To prevent the issue from happening you can change in Intellij the gradle configuration to specify that test should be run using gradle.
see: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000191604-Unit-Tests-run-from-Gradle-and-IDEA-differ

Android Studio 0.8.1 - how to use Facebook SDK?

I tried via import module but I'm always getting
Error:(13, 0) Could not find property 'ANDROID_BUILD_MIN_SDK_VERSION' on project ':facebook'.
I didn't find a tutorial how to use the Facebook SDK with the latest Android Studio...
Using Facebook SDK 3.23 in Android Studio 1.1 preview 2
Two choices :
*Maven Repository for the Facebook SDK.
dependencies {
compile 'com.facebook.android:facebook-android-sdk:4.7.0'
}
check for the latest version facebook sdk
OR
*Follow the steps below:
1.Download facebook sdk
https://developers.facebook.com/docs/android/
2.Unzip the archive facebook-android-sdk-3.23.0
3.File menu->Import Module
4.Chose “facebook” folder inside the unzipped archive.
5.Go to directory facebook/build.gradle change this part
This:
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
defaultConfig {
minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
}
To proper sdk version
example:
compileSdkVersion 21
buildToolsVersion '21.1.1'
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
}
6.Click your project and press F4 ,go to 'dependencies' tab, click '+', 'module dependency' and select your imported facebook module.
7.Resync and rebuild
For anybody still wondering, there is finally an official Maven repo for the Facebook SDK:
dependencies {
compile 'com.facebook.android:facebook-android-sdk:3.20.0'
}
Write all those variables values in "gradle.properties" file.
Like
ANDROID_BUILD_TARGET_SDK_VERSION=19
ANDROID_BUILD_TOOLS_VERSION=19.1.0
ANDROID_BUILD_SDK_VERSION=19
ANDROID_BUILD_MIN_SDK_VERSION=14
You can follow this post https://stackoverflow.com/a/20221453/1232520
add these line in your gradle.properties it work for me!
ANDROID_BUILD_TARGET_SDK_VERSION=19
ANDROID_BUILD_TOOLS_VERSION=19.1.0
ANDROID_BUILD_SDK_VERSION=19
ANDROID_BUILD_MIN_SDK_VERSION=14
After do this plese follow step describe in this post https://stackoverflow.com/a/24457464/3296641
Now you can use version 3.21.1 from maven
https://developers.facebook.com/docs/android/
dependencies {
compile 'com.facebook.android:facebook-android-sdk:3.21.1'
}
You can now get Facebook SDK with gradle:
compile 'com.facebook.android:facebook-android-sdk:3.20.0'
Simply just go to project structure and select dependency
then click + on the right top corner and select library dependency (the first option ) and just type facebook or if you need any other lib and click a search icon (MAKE SURE YOU HAVE INTERNET CONNECTION) and select com.facebook.android:facebook-android-sdk:.... and you are good to go
EXPLINATION:
The following constants can be defined in your project gradle.properties:
ANDROID_BUILD_MIN_SDK_VERSION=15
ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=21.1.2
ANDROID_BUILD_SDK_VERSION=21
Those are used in the Facebook sdk as build gradle settings. To specify your use of the sdk. (it's encouraged that those values match your project values!).
"Code Reusability and Dependancy!" you can use those same values as gradle build settings in all build.gradle files for your project and all other modules inside your project. use them they same way they are used in the Facebook build.gradle file.
Example Benefit: Changing the minimum sdk version on gradle.properties will automatically change it for all your modules build.gradle.
You can get rid of the
"Error:(13, 0) Could not find property 'ANDROID_BUILD_MIN_SDK_VERSION'"
by replacing all the values like below
see your build.gradle . you can find all the properties and the versions your application is using. replace the values of 'buld.gradle file' from facebook sdk with the same versions.
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
}
and for BOLT.JAR Error, there is a dependacy in facebook sdk build.gradle file like compile "files('../libs/bolts.jar')" this is actually for the whole sdk folder which we download, but as we use only facebook folder so we have one less directory path to follow.
Hence, you can get rean of one period before /libs so just remove it and keep it as compile files ('./libs/bolts.jar')
you are good to go !!
Add the compile dependency with the latest version of the Facebook SDK in the build.gradle file:
dependencies {
compile 'com.facebook.android:facebook-android-sdk:4.14.0'
}
For more info https://developers.facebook.com/docs/android/getting-started
For those who do not know where to put gradle.properties, it should go to the root of the project. This is how it looks in one of my projects.
I recently came across an error that was not resolved here yet. I tried to use the latest Facebook SDK from maven (by declaring compile 'com.facebook.android:facebook-android-sdk:4.6.0'). After gradle build I got no error and Android studio's static code analysis was also OK when declaring and writing FacebookSdk related code. However when I tried to run the code in the emulator, I got an error that facebook sdk is not included in the project. Then I tried some other hacks and then I finally removed the facebook entry from the gradle build file and tried to import it as a module, but that also did't work.
After a while when I was reading Facebook gradle file I realized that they are compiling the code with "sourceCompatibility JavaVersion.VERSION_1_7" flag. I used VERSION_1_8 flag because I wanted to use lambda's using retrolambda library. After I switched to 1.7 (and also adjusted the settings in File -> Project structure -> Source Compatibility) it started to work and there were no problems.
I quite don't understand why (and if) is this related together, but it probably is so be aware of that.

Android Studio runtime error Stub! at com.amazon.device.messaging.ADMMessageReceiver.<init>()

When I build the Amazon (Kindle) flavor of my Android app I run into this Runtime error:
Caused by: java.lang.RuntimeException: Stub!
at com.amazon.device.messaging.ADMMessageReceiver.<init>()
I need the local amazon-device-messaging.jar file to compile my app, however I do not need to include it during runtime as the amazon device will have the necessary classes and methods.
How do I update my Android Studio build.gradle file to do this?
I also ran into this issue. When adding the Amazon Device Messaging jar as a library, Android Studio automatically generated
dependencies {
compile files('libs/amazon-device-messaging-1.0.1.jar')
}
I just needed to switch that to
dependencies {
provided files('libs/amazon-device-messaging-1.0.1.jar')
}
That did the trick for me. I'd up-vote your answer, #Clu, but I don't have a high enough reputation.
To solve this I used the provided type of dependency.
Inside my project modules build.gradle file, right before my dependencies closure I included the following:
configurations {
provided
}
sourceSets {
main {
compileClasspath += configurations.provided
}
}
And then, within my dependencies closure I included the following:
dependencies {
provided files('libs/amazon-device-messaging-1.0.1.jar')
}
This ensured that the .jar was only used for compile time and not runtime. I'm quite new to Android Studio, and this took me a while to figure out; hopefully this will help you make the switch to Android Studio as well.
Add the ADM jar in the Maven local repository.
Command :
mvn install:install-file "-Dfile=amazon-device-messaging-1.0.1.jar" "-DgroupId=com.amazon.device.messaging" "-DartifactId=amazondevicemessaging" "-Dversion=1.0.1" "-Dpackaging=jar"
Include local maven repository as project dependency :
Add “mavenLocal()” in main Gradle build script:
allprojects {
repositories {
mavenCentral()
mavenLocal()
}
Link the Maven artifact in ADM project.
Add below line ADMWrapperLib Gradle script (::).
provided 'com.amazon.device.messaging:amazondevicemessaging:1.0.1'

Resources