In Android is there a way to generate signed APKs for all modules in a project.
E.g. I have following project
Project
-- Library Module
-- Module 1
-- Module 2
-- Module 3
I want to generate APKs for all 3 modules in one command. Currently I have to separately use Generate Dialog for all 3 which takes a lot of time.
Yes you can generate multiple apk files with gradlew.
Open Terminal Window in Android Studio and run following commands:
1- Navigate to root folder of the project, where gradlew file is located
cd ..
2- Give executable permissions to gradlew (this needs to be done only once, no need to repeat again)
chmod a+x gradlew
3- Generate debuggable apks of all underlying modules.
./gradlew assembleDebug
You can also generate release apk files for all modules, by using this command instead
./gradlew assembleRelease
for more details, run the following command to see list of all tasks that can be run on gradlew
./gradlew tasks
Note: Running ./gradlew first time might result in terminal downloading the gradle files from server, wait for the downloading to complete before moving forward!
Hope that helps!
Update:
For providing signing information in grade file, Open your module specific build.grade file and update it to contain this code:
signingConfigs {
playstore {
keyAlias 'KEY_ALIS_NAME_HERE'
storeFile file('/PATH_TO_KEYSTORE_FILE_HERE/app.keystore')
keyPassword 'KEY_PASSWORD_HERE'
storePassword 'STORE_PASSWORD_HERE'
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard-file.txt'
proguardFile 'proguard-file.txt'
debuggable false
signingConfig signingConfigs.playstore
}
}
After that you can simply run ./gradlew assembleRelease to do the work :)
Related
I have some android instrumented tests under AndroidTests directory in Android Studio. Of course I can manually execute the tests but I need to execute all the tests in the suite after each build ("release" type of build, not during normal debug build). I need to do that because I'd like to validate the new code against the tests before releasing the new app's apk. How to do that? I google it but I didn't find a proper solution yet.
Any idea?
Finally I managed to do that. I have added the following configuration to my build.gradle file in my android studio project:
// execute android tests before realising a new apk
tasks.whenTaskAdded { task ->
if (task.name == 'assembleRelease')
task.dependsOn('connectedAndroidTest')
}
android {
signingConfigs {
release {
keyAlias 'key'
keyPassword 'password'
storeFile file('/path/to/release_keystore.jks')
storePassword 'password'
}
}
buildTypes {
debug {
signingConfig signingConfigs.release
}
release {
signingConfig signingConfigs.release
}
}
After doing that I'm able to run all android tests when building a release apk. If the tests are failing no apk is built.
In general you can define a dependencies for the task using the dependsOn method.
For example:
task A << {
println 'Hello from A'
}
task B << {
println 'Hello from B'
}
B.dependsOn A
You will obtain
> gradle -q B
Hello from A
Hello from B
In your case you can specify:
assemble.dependsOn test
If you would like to specify a dependency only for the release build:
assembleRelease.dependsOn test
Use:
connectedAndroidTest to run the tests on a connected emulator or device.
test to run the unit test on your local host.
I think you should dive in one certain topic: Continuous Integration.
This would allow you to run tests suites after every build or commit, build variants, publish on Google Play and much more... Start with classic Jenkins CI, which may not be the most polished and easy to use, I mean user-friendly tool, but in compare to Travis or Circle CI it provides huge configuration possibilities, nice app community and it's free to use.
Start with this article: http://www.vogella.com/tutorials/Jenkins/article.html
Other tools: I'm already using Travis for my Github projects, but Circle CI or Green House CI might be also good choice.
Hope it will help
I have some unit and instrumentation tests in my androidTest folder.
I'm trying to run these tests on my local Jenkins.
I've successfully configured my project with Jenkins and I've also created an emulator for the instrumentation tests.
So far all the resources I've seen only focus on ant and earlier versions of Gradle. The syntax for writing tasks has slightly changed in the current version and I'm confused about this.
Here is my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "myPackageName"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
}
You can write a jenkis script like this:
stage('Build & Install') {
//Build the apk and the test apk which will run the tests on the apk
sh 'chmod +x gradlew && ./gradlew --no-daemon --stacktrace clean :app:assembleDevDebug :app:assembleDevDebugAndroidTest'
}
stage('Tests') {
//Start all the existing tests in the test package
sh './gradlew --no-daemon --debug :app:connectedDevDebugAndroidTest'
}
This should install the apk and the test apk into the device and start the test cases in the test apk on installation.
Here I have given DevDebug as I have a flavor constant called Dev and build type is Debug. If you don't have any of those, you don't use them.
Create a job in junkins and (configure adb path) add this command to build steps as execute shell command or as a windows bat cmd
$ adb shell am instrument -w com.xyz.abc.test/android.test.InstrumentationTestRunner
P.S :- For better automation use robotium and spoon with junkins you can automate everything, you push a commit on git and you will get test results in your mail inbox it's that cool.
Edit
Running tests using spoon
Add these commands to build steps
./gradlew assembleDebugAndroidTest
./gradlew assembleDebug
specify debug-build.apk path and test-unaligned.apk path in spoon command properly
java -jar C:\Users\Shivam\Downloads\spoon-runner-1.1.1-jar-with-dependencies.jar --apk C:\Users\Shivam\Downloads\SpoonAndRobotiumTest\app\build\outputs\
apk\app-debug.apk --testapk C:\Users\Shivam\Downloads\SpoonAndRobotiumTest\
app\build\outputs\apk\app-debug androidTest-unaligned.apk --sdk E:\sdk
At first, you must install the Gradle Plugin on your Jenkins-CI installation. You can search it at http://yourhost/jenkins/pluginManager/
To continue integrating it, you can take a look at this presentation, specially on the lastest slides
I have a project with three different build types: debug, beta, and release. My test package is always created for debug builds, but QA uses the beta build and we want QA to run these tests on their vast array of devices.
I'm trying to create a testing apk for QA that is signed by the same key as the beta build. Looking through the Android-Gradle documentation, I don't see anything telling me that I can't do this, but I don't see anyway to configure this. Is there anyway I can configure which keystore is used when assembling a test apk? Or is there a way to create an unsigned test apk?
You can now point this to a different target, I don't know when this happened, but from the docs:
Currently only one Build Type is tested. By default it is the debug
Build Type, but this can be reconfigured with:
android {
...
testBuildType "staging"
}
This is an incomplete answer to your question in that it documents what you can't do, but the connectedAndroidTest task, which is what runs the androidTest tests in your project, is hardcoded to run against the debug build type, and I don't see a way to point it at a different build type.
Taking the advice from Is there a way to list task dependencies in Gradle? and examining the task dependency tree, if you run:
./gradlew tasks --all
you get this in your output:
Verification tasks
------------------
app:check - Runs all checks. [app:lint]
app:connectedAndroidTest - Installs and runs the tests for Build 'debug' on connected devices. [app:assembleDebug, app:assembleDebugTest]
app:connectedCheck - Runs all device checks on currently connected devices. [app:connectedAndroidTest]
app:deviceCheck - Runs all device checks using Device Providers and Test Servers.
The documentation for the connectedAndroidTest task claims it runs tests against debug, and the task dependencies (which you see with the -all flag) confirm that the task depends on assembleDebug.
Adding additional build types and flavors doesn't seem to affect the dependency on the built-in debug type.
It's possible that with greater Gradle-fu than mine, you could rewire the tasks to make the tests depend on a different build type, but doing this is likely to be fragile since it's bound to depend on things that aren't supported API in the Android Gradle plugin.
To answer your question most directly, though, if all you want is to run tests against a build with a different certificate, you could change the signing config on your debug build to use the beta certificate:
android {
signingConfigs {
beta {
keyAlias 'key'
keyPassword 'password'
storeFile file('/path/to/beta_keystore.jks')
storePassword 'password'
}
}
buildTypes {
debug {
signingConfig signingConfigs.beta
}
beta {
signingConfig signingConfigs.beta
}
}
}
I tested it and I am able to run androidTest targets against debug builds that use a custom keystore in this way. However, I doubt this solves your problem, because I suspect you want to run your tests against the beta build, not a debug build with the beta certificate.
To add a testing source set for your build variant, follow these steps:
In the Project window on the left, click the drop-down menu and
select the Project view.
Within the appropriate module folder,
right-click the src folder and click New > Directory.
For the directory name, enter "androidTestVariantName." For example,
if you have a build variant called "MyFlavor" then the directory name
shoulbe "androidTestMyFlavor." Then click OK.
Right-click on the new directory and click New > Directory. Enter
"java" as the directory name, and then click OK.
Now you can add tests to this new source set by following the steps above to add a new test. When you reach the Choose Destination Directory dialog, select the new variant test source set.
The instrumented tests in src/androidTest/ source set are shared by all build variants. When building a test APK for the "MyFlavor" variant of your app, Gradle combines both the src/androidTest/ and src/androidTestMyFlavor/ source sets.
Another way is to put following line your in default config.
Currently only one Build Type is tested. By default it is the debug Build Type, but this can be reconfigured with:
android {
...
testBuildType "staging"
}
Recently change to Android Studio from Eclipse and I have also changed the JDK from java-open-jdk to jdk1.7.0_45.
Now I'm trying to run my first app and I get this message:
Installation failed since the APK was either not signed, or signed incorrectly.
If this is a Gradle-based project, then make sure the signing configuration
is specified in the Gradle build script
Edit:
When I'm running from Android Studio I get the error displayed above. When I'm running it from the command line I don't get an error (well the app is running and I get an error but nothing to do with gradle).
I got the code from here
You can check build.gradle here at google repo
UPDATE 2:
I added this code
signingConfigs {
release {
storeFile file("john.keystore")
storePassword "john"
keyAlias "johnkeystore"
keyPassword "john"
}
}
just above the buildTypes code block in the build.gradle file.
File john.keystore is on the root of my Project. I'm running gradlew assembleRelease and I'm getting a xxx-release-unsigned.apk.
If you indeed runing build with gradle you need to configure signingConfigs. Android can configure your debug signingConfig automatically. You can make release signingConfig in a next manner:
android {
signingConfigs {
release {
storeFile file('android.keystore')
storePassword "pwd"
keyAlias "alias"
keyPassword "pwd"
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
}
Check manual on this topic here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Signing-Configurations
If you still have issues, please update question with your build.gradle. Most likely issue is laying here. I'm using JDK 1.7 and gradle builds are working fine.
I had the same problem, i went to Build->Clean Project and re-execute the project and it worked.
I have been struggling for a while with Android Studio esp. gradle and build variants.
I have change this from the build types as defined:
debug
release
As I ran into the same problem (while developing and running tests), I went to Build -> Rebuild Project and then re-launched the emulator. That worked for me.
None of the answers worked for me, even after uninstalling app from phone, I could not deploy it. What worked was installing the app o a different device, and then when I tried to deploy it on the previous device, it miracleously worked.
If your goal is not to create a custom build for your application you might want to clean all the data about it from your test device, in case you are using emulator just wipe all the data from device. Go to -> Tools -> Android -> AVD Manager -> [Device you wanna wipe] (In actions tab) -> Wipe Data.
In case of actual device just uninstall the app and all related data.
Reason is that unsigned APK has a signature, so device sees you're trying to install something with the same package name that was generated not here.
Hope this saves you some time.
In case your device has multiple users, before installing signed APK, check if the same app is not installed for other users. If it is there remove it.
For me this was the root cause of rejecting installation of signed APK.
Just to Build -> Rebuild Project and then re-launched the emulator. That worked for me.
I want to build my gradle project with a certain buildtype and deploy it on device with a single command.
My build.gradle is setup for multiple buildtypes such as live and release.
I worked with maven before and i look for an equivalent of:
mvn clean install -P release android:deploy android:run
Here is the command to build and deploy through a specified BuildType. ( Thank you Varun! )
gradle installProfilename
Where Profilename of course would be the name of the BuildType specified in build.gradle
Example.:
gradle installRelease
Would build with the release profile:
buildTypes {
release {
debuggable false
jniDebugBuild false
signingConfig signingConfigs.main
. . .
}
Addition.:
Gradle can show you what tasks are available.:
gradle tasks