how to create an apk file from gradle script - android

How to create an anpk file using gradle script.I have one android project.I want to create its apk but using gradle script not through android studio.
Is there any such script which will create apk.

You can use gradlew script to build your apk file. Gradlew script is part of the project which is created using Android Studio. You can run gradle script like this
gradlew assembleDebug
or
gradlew assembleRelease
first option will generate apk in debug configuration. Second will generate release APK. There is also several other things that you need to prepare before you run this script. The most important thing is that you need to create keystore and point it in your gradle file.
To create keystore you can use this command:
keytool -genkey -v -keystore keystore.jks -alias your-alias -keyalg RSA -keysize 2048 -validity 10000
keytool should be placed in bin folder in your java root directory.

If you run gradle tasks you will see available task for your project. Here is the Build section
Build tasks
-----------
assemble - Assembles all variants of all applications and secondary packages.
assembleAndroidTest - Assembles all the Test applications.
assembleDebug - Assembles all Debug builds.
assembleDebugAndroidTest - Assembles the android (on device) tests for the Debug build.
assembleRelease - Assembles all Release builds.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
clean - Deletes the build directory.
compileDebugAndroidTestSources
compileDebugSources
compileDebugUnitTestSources
compileReleaseSources
compileReleaseUnitTestSources
mockableAndroidJar - Creates a version of android.jar that's suitable for unit tests.
So
gradle build
should do the job

You can create your own script using available gradle commands.
Here is a detailed article for build, test and deploy via bash script.
Following is the sample script you can use.
enter code
#Define all paths, constants here
PROJECT_DIR='/Users/mayuri/CODE/AndroidBuildAutomationSample/'
OUTPUT_DIR='/Users/mayuri/CODE/AndroidBuildAutomationSample/OUTPUT_DIR/'
#Enter project dir
cd $PROJECT_DIR
#Start Build Process
echo "\n\n\nCleaning...\n"
./gradlew clean
echo "\n\n\ncleanBuildCache...\n"
./gradlew cleanBuildCache
echo "\n\n\n build...\n"
./gradlew build
echo "\n\n\n assembleDebug...\n"
./gradlew assembleDebug
#Install APK on device / emulator
echo "installDebug...\n"
./gradlew installDebug
Remember to give execute permissions to your script file
chmod +x automate.sh

Execute the following commands from the directory of the associated project.
# build project, runs both the assemble and check task
gradle build
# build project complete from scratch
gradle clean build
# speedup second grandle build by holding it in memory
gradle build --daemon
By default, the Gradle build creates two .apk files in the build/outputs/apk folder. Check this link for further information
.

Related

Google login not working on bitbucket CI built apk

We are using bitbucket for code hosting. Firebase app distribution for distributing the apk. The application has google login for signing into the app. We have three build types, debug, staging and release. We have updated the google-services.json file from the Firebase project for each type.
We are using automated builds from our bitbucket using a bitbucket-pipelines.yml file which is as follows:
pipelines:
pull-requests:
dev:
- parallel:
- step: # step to build Android debug application
name: Android Debug Application
caches: # caching speed up subsequent execution https://support.atlassian.com/bitbucket-cloud/docs/cache-dependencies/
- gradle
size: 2x # Double resources available for this step.
script:
- chmod +x gradlew
- ./gradlew assembleDebug
artifacts:
- app/build/outputs/** # artifacts are files that are produced by a step https://support.atlassian.com/bitbucket-cloud/docs/use-artifacts-in-steps/
- step: # step to run unit tests
name: Unit Tests
caches:
- gradle # caching speed up subsequent execution https://support.atlassian.com/bitbucket-cloud/docs/cache-dependencies/
script:
- chmod +x gradlew
- ./gradlew testDebugUnitTest # test reporting is automatically enabled https://support.atlassian.com/bitbucket-cloud/docs/test-reporting-in-pipelines/
tags:
release-stage-*: #This tag triggers a pipeline when a tag starting with “release-stage-” is pushed.
- step:
name: Deploy To Firebase App Distribution
deployment: 'Staging'
caches:
- gradle
size: 2x # Double resources available for this step.
script:
- export FIREBASE_TOKEN=$FIREBASE_REFRESH_TOKEN
- echo "$SIGNING_JKS_FILE" | base64 -di > release-keystore.jks
- chmod +x gradlew
- ./gradlew --stop
# Build an unsigned apk and deploy apk to Firebase App Distribution
- ./gradlew assembleStaging appDistributionUploadStaging
artifacts:
- app/build/outputs/apk/staging/app-staging.apk
release-prod-*: #This tag triggers a pipeline when a tag starting with “release-prod-” is pushed. Also make sure its only created from 'master'
- step:
name: Deploy To Google Play
deployment: 'Production'
caches:
- gradle
size: 2x # Double resources available for this step.
script:
- echo "$SIGNING_JKS_FILE" | base64 -di > release-keystore.jks
- base64 --decode > release-keystore.jks $SIGNING_JKS_FILE
- chmod +x gradlew
- ./gradlew --stop
# Build a signed release aab
- ./gradlew bundleRelease
artifacts:
- app/release/app-release.aab
Now the problem is that builds that are built on my local machine for debug/staging have no issues at all. The google login works on those builds and all other features as well.
But as soon as a build is made from the release-stage-<build_number> tag from the pipeline, a build is generated and pushed to firebase app distribution but the build has issues with google login, specifically google login does not work. ApiException 10
Following are the locations that I have put in my google-services.json files at:
app/src/debug/ -> For debug builds
app/src/staging -> For staging builds
app/src/ -> For release builds
I have made the following assumption till now.
Since the CI machine that builds the apk is not fixed, ie. it changes every time the release-stage-* tag is created and pushed, the SHA fingerprint of the generated build is different which does not communicate well with firebase project that is in place causing the google login to not work.
My question is: Is the assumption that I have made, correct?
If yes, how do I overcome this to automate build generation that has working google login on my android app?
If no, what exactly is going wrong here, as I am totally out of ideas at this point.

How to change gradlew .env file?

I have a react native project.
I want to build a release to publish on play store, here in react native official documentation to publish an app we can see:
Generating the release AAB
Run the following in a terminal:
cd android
./gradlew bundleRelease
Here is the first outputs I got:
./gradlew bundleRelease
Starting a Gradle Daemon (subsequent builds will be faster)
> Configure project :app
Reading env from: .env
gradlew is reading my env vars in .env file while I got a .env.production that I want gradlew to read instead.
How can I do this ?
I would recommend you to use react-native-config if you want to use other type of .env files for different environments.

How to regenerate an apk?

This morning I generated an apk via this command:
./gradlew bundleRelease
which generated the following:
However, now I need to generate a newer version of the apk as I made some changes to the application.
I tried just running the above command again, but that did not seem to work. The files are all the same ones. How do I regenerate an apk? Do I need to delete the old ones first?
Clean your older build first using
./gradlew clean
Then build your apk using
# for debug
./gradlew assembleDebug
# for release
./gradlew assembleRelease
# if you want all the build variants, just using below command
./gradlew assemble
Your apks can be found inside output/apk directory
bundleRelease generates a bundle - filing in just the bundle directory you see. You should be using assembleRelease if you want to generate an APK and the apk directory:
./gradlew assembleRelease

Is it possible to build debug and release versions at the same time?

Every time when I need to build the debug and release ver for the QA what I do:
In BuildVariants change to Debug make a build APK.
Then I change the BuildVariants to Release and build one more APK.
So, question is if it is possible to build two versions at the same time?
If you go to the root folder of the proyect, you can compile the debug version using the command line:
gradlew.bat assembleDebug
Or if you want to compile a release:
gradlew.bat assembleRelease
Takng that into account, you can create a script, named "compile.bat" for example, with this content:
#echo off
title Compiling...
echo Compiling the debug and realease apk's...
gradlew.bat assembleDebug
gradlew.bat assembleRelease
#echo Finished!
pause
You can build an APK through the terminal, then you can launch as many build processes as you like.
$ ./gradlew assembleFlavouraFlavourbRelease & ./gradlew assembleFlavouraFlavourbDebug &

Which gradle task builds AndroidTest .*apk and doesn't do anything additional

Let's say I have a build variant named debugAutomation.
I wan't to build this variant into .*apk file using gradle command. I do:
./gradlew assembleDebugAutomation
This provides me with application .*apk. But to perform automation tests in espresso I also need test .*apk. So I do again:
./gradlew assembleDebugAutomationAndroidTest
And .*apk with -androidTest is in my outputs/apk/ folder of my project.
Where is the trick and problem?
a) while performing assembleDebugAutomation I have only application .*apk built.
b) while performing assembleDebugAutomationAndroidTest then during build process THIS APK BEING INSTALLED ON ALL CURRENTLY VISIBLE DEVICES IN ADB.
My question is: How can I build androidTest .*apk with gradle command so it ONLY BUILDS APK and DOESN'T INSTALL IT on currently visible devices in ADB during build process?
To just assemble into an APK:
./gradlew assembleDebug
That will build however, but won't do any automation tests, nor would it install to devices.
./gradlew assembleDebug
Builds the debug APK.
./gradlew assembleDebugAndroidTest
Builds the test APK
Or if you have a flavour in your gradle file it would be:
./gradlew assemble<FlavourName>DebugAndroidTest

Categories

Resources