Can't generate signed APK in GitLab CI - android

I'm testing gitlab ci/cd and I'm trying to build a signed APK but my
script failed. What should I change or add?
I add variables KEYSTORE_FILE, KEYSTORE_PASSWORD, KEY_ALIAS, KEY_PASSWORD
with values.
assembleRelease:
stage: release
script:
- echo $KEYSTORE_FILE | base64 -d > my.keystore
- ./gradlew assembleRelease
-Pandroid.injected.signing.store.file=$(pwd)/my.keystore
-Pandroid.injected.signing.store.password=$KEYSTORE_PASSWORD
-Pandroid.injected.signing.key.alias=$KEY_ALIAS
-Pandroid.injected.signing.key.password=$KEY_PASSWORD
artifacts:
paths:
- app/build/outputs/apk/release
Error:
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:packageRelease'.
1 exception was raised by workers:
java.lang.RuntimeException:
com.android.ide.common.signing.KeytoolException: Failed to read key from
store "/builds/juantamad.02072019/2019_samplebuild/my.keystore": null
But it should be successfully

Provided you have correct indentations (as posted they are not), the problem is likely to be caused by multi-line command - it just executes - ./gradlew assembleRelease first, without further arguments
To wrap the long line you can use YAML multi-line strings:
script:
- ...
- >
./gradlew assembleRelease
-Pandroid.injected.signing.store.file=$(pwd)/my.keystore
-Pandroid.injected.signing.store.password=$KEYSTORE_PASSWORD
-Pandroid.injected.signing.key.alias=$KEY_ALIAS
-Pandroid.injected.signing.key.password=$KEY_PASSWORD
Read more:
In YAML, how do I break a string over multiple lines?
https://gitlab.com/snippets/1717579
--
Another possible cause - if your env var is set as Protected in project settings,
and you are working in a branch which is not Protected.
Then var is not passed to your job

Related

android bitbucket pipleine build ./gradlew assembleQADebug failure but local build works?

I enabled the pipleline and created the bitbuket-pipelines.yaml for my android project in bitbucket repo.
The pipeline build seems started fine and I can see the gradle tasks are being executed in the output console, just like executing the same gradlew build on my local machine:
./gradlew assembleQADebug
however, one of the task failed in the bitbucket cloud, I read the error, it is for the Jetpack Navigigation component SafeArgs:
...
> Task :app:compileQADebugKotlin
e: /opt/atlassian/pipelines/agent/build/.../AaaFragment.kt: (71, 86): Unresolved reference: actionAbcFragment
e: /opt/atlassian/pipelines/agent/build/.../XxxFragment.kt: (73, 86): Unresolved reference: actionZzzFragment
e: ...
Any tips or guess how to find out why this works in local but failed in fail in bitbucket pipleline?
The following is my simple yaml file:
image: androidsdk/android-31
pipelines:
branches:
playground/bitbucketbuild:
- parallel:
- step:
name: Build Android
memory: 2048
caches:
- gradle
script:
- cd $PROJ_ROOT && ./gradlew assembleQADebug #- ./gradlew clean bundleQARelease
- echo $PWD && ls -R app/build/outputs
- step: # step to run unit tests
name: Unit Tests
caches:
- gradle
script:
- cd $PROJ_ROOT && ./gradlew test

GitLab CI SAST for Android : Job mobsf-android-sast failed

I got an error with the job mobsf-android-sast, do i miss some configurations for mobsf?
Log:
$ /analyzer run
[INFO] [MobSF] [2022-06-17T14:02:46Z] ▶ GitLab MobSF analyzer v3.0.0
......
......
[INFO] [MobSF] [2022-06-17T14:02:49Z] ▶ Zipping: keystore.properties
[INFO] [MobSF] [2022-06-17T14:02:49Z] ▶ Zipping: sonar-project.properties
[FATA] [MobSF] [2022-06-17T14:02:49Z] ▶ failed to upload archive to mobsf: error requesting scan: Post "http://localhost:8000/api/v1/upload": dial tcp [::1]:8000: connect: connection refused
Uploading artifacts for failed job
Uploading artifacts...
WARNING: gl-sast-report.json: no matching files
ERROR: No files to upload
Cleaning up project directory and file based variables
ERROR: Job failed: command terminated with exit code 1
.gitlab-ci.yml
include:
- template: Security/SAST.gitlab-ci.yml
variables:
SAST_EXPERIMENTAL_FEATURES: "true"
sast:
stage: test
variables:
SAST_EXPERIMENTAL_FEATURES: "true"
DS_DISABLE_DIND: "true"
sast:
stage: test
dependencies:
- build
artifacts:
reports:
sast: gl-sast-report.json
paths:
- gl-sast-report.json
tags:
- docker
try this one

Gitlab CI :- Getting pending status after push on the gitlab

I am using the Gitlab-ci for the creating the build on the push,I have also creating the .gitlab-ci.yml inside my root directory. With each push the code on the Gitlab , i am getting the following message which are as follow
This job is stuck because you don't have any active runners that can run this job.
I have already enable the Shared Runners in the Setting getting above problem like Not having active member.
Please check my gitlab-ci.yml file data
image: jangrewe/gitlab-ci-android
stages:
- build
before_script:
- export GRADLE_USER_HOME=$(pwd)/.gradle
- chmod +x ./gradlew
cache:
key: ${CI_PROJECT_ID}
paths:
- .gradle/
build:
stage: build
script:
- ./gradlew assembleDebug
artifacts:
paths:
- app/build/outputs/
My all push suspended to create the build , please check it once.
Even though i have created the runner manually but they become fail to execute and getting following excetion
ERROR: Job failed (system failure): getting Kubernetes config: invalid configuration: no configuration has been provided
Please help me to short out from the problem. Thanks
If you have active shared-runner, then try to give the tags of the runner in the .gitlab-ci.yml. Say if the tag for the shared-runner is dev-ci, then find below the updated code:
image: jangrewe/gitlab-ci-android
stages:
- build
before_script:
- export GRADLE_USER_HOME=$(pwd)/.gradle
- chmod +x ./gradlew
cache:
key: ${CI_PROJECT_ID}
paths:
- .gradle/
build:
stage: build
tags:
- dev-ci
script:
- ./gradlew assembleDebug
artifacts:
paths:
- app/build/outputs/
Screenshot of the runner tags:
Setting up a runner
Before setting up a runner for your project, you need to first:
Install gitlab-runner on a server separate than where GitLab is installed.
Register a runner [there are 3 types of runners: shared, group and specific runners. Depending on requirement, you can choose which runner to register.]
While registering the runner, you need to give tags and the type of runner executor you need.
After registering the runner, go to Project >> Settings >> CI/CD >> Runners, you can see the runner is online. If in case you are using Group or Shared runner, then you need to enable the runner in Project settings.
In your case, you can use Specific Runner with Docker as Runner-Executor and then, in the .gitlab-ci.yml use the tags you provided while registering the runner.

Job failed using Gitlab

I have used the Gitlab for run the CI plan.
Problem sequence:
Run the CI plan, with below yaml script, now CI getting passed
Again run the CI plan, now CI getting failed with below error code
warning: failed to remove MediaDrmCts/app/build/outputs/apk/debug/output.json: Invalid argument
Environment:
Gitlab runner in Windows PC
Shell
Yaml Code:
assembleDebug:
stage: build
script:
- echo 'start building...'
- cd MediaDrmCts
- ./gradlew clean assemble
artifacts:
paths:
- MediaDrmCts/app/build/outputs/
debugTests:
stage: test
script:
- cd MediaDrmCts
- ./gradlew -Pci --console=plain :app:testDebug
Error Log:
Please help us to resolve the issue?
This could be related to the issues in GitLab with killing processes on Windows:
tl;dr the windows runner (current version is around ~13.6 with no fix yet) can't kill the full tree of processes started in the job properly, so processes holding file locks are left around for the next job/pipeline to fail on when it attempts to clean up
https://gitlab.com/gitlab-org/gitlab-runner/-/issues/3185
https://gitlab.com/gitlab-org/gitlab-runner/-/issues/3121
I've had similar issues in the past and a workaround is to clean up troublesome directories in the job that makes them, rather than letting the git clean command clean them up in the next job.
One option:
debugTests:
stage: test
script:
- cd MediaDrmCts
- ./gradlew -Pci --console=plain :app:testDebug
after_script:
- if (Test-Path ./MediaDrmCts/app/build) { Remove-Item ./MediaDrmCts/app/build -Recurse -Force; }
...
Another option
If your builds aren't sensitive to how clean the repo folder is, then you can try turning off the git clean -ffdx step in your .gitlab-ci.yml file:
...
variables:
GIT_CLEAN_FLAGS: none
...
which will tell the GitLab runner not to attempt cleaning up the repository of extra files before each run.
Keep in mind that this may have some unintended consequences, such as the wrong files being left over from previous runs on other branches, so be wary, and have good tests!

Why assembleDebug success but assembleRelease failed in react native?

I have tried to assemble my project as a release. But it always failed. Below are some of the script that I use to build my apk as release:-
BUILD SUCCESSFUL
.\gradlew assembleDebug --no-daemon
.\gradlew app:assembleDebug
BUILD FAILED
.\gradlew app:assembleRelease
.\gradlew assembleRelease -x bundleReleaseJsAndAssets
The result of the failed mention on every failed build:-
> Execution failed for task ':app:mergeReleaseResources'
> Task :app:mergeReleaseResources
> FAILED cvc-type.3.1.3: The value '' of element 'id' is not valid.
I also have try to create keystore using keytool and apply it to the signingConfigs in
./android/app/build.gradle
but no success on release. So, I'm reverting and use default debug.keystore value
Referring various answer from forum, SO question, tutorial I have try to apply. I'm totally stuck.
Does anyone face this issue and able to overcome the obstacles?
Edited: Error log when build
Using .\gradlew assembleRelease
D:\Project\Clients\android>.\gradlew assembleRelease
> Configure project :react-native-activity-result
WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
WARNING: The specified Android SDK Build Tools version (23.0.1) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle Plugin 3.4.1.
Android SDK Build Tools 28.0.3 will be used.
To suppress this warning, remove "buildToolsVersion '23.0.1'" from your build.gradle file, as each version of the Android Gradle Plugin now has a default version of the build tools.
> Task :app:bundleReleaseJsAndAssets
warning: the transform cache was reset.
> Task :react-native-activity-result:verifyReleaseResources FAILED
> Task :app:bundleReleaseJsAndAssets
Loading dependency graph, done.
info Writing bundle output to:, D:\Project\Clients\android\app\build\generated\assets\react\release\index.android.bundle
info Writing sourcemap output to:, D:\Project\Clients\android\app\build\intermediates\sourcemaps\release\index.android.bundle.packager.map
info Done writing bundle output
info Done writing sourcemap output
info Copying 47 asset files
info Done copying assets
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':react-native-activity-result:verifyReleaseResources'.
> 1 exception was raised by workers:
com.android.builder.internal.aapt.v2.Aapt2Exception: Android resource linking failed
error: resource android:style/TextAppearance.Material.Widget.Button.Borderless.Colored not found.
error: resource android:style/TextAppearance.Material.Widget.Button.Colored not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values-v26\values-v26.xml:7: error: resource android:attr/colorError not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values-v26\values-v26.xml:11: error: resource android:attr/colorError not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values-v26\values-v26.xml:15: error: style attribute 'android:attr/keyboardNavigationCluster' not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values-v28\values-v28.xml:7: error: resource android:attr/dialogCornerRadius not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values-v28\values-v28.xml:11: error: resource android:attr/dialogCornerRadius not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2734: error: resource android:attr/fontStyle not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2735: error: resource android:attr/font not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2736: error: resource android:attr/fontWeight not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2737: error: resource android:attr/fontVariationSettings not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2738: error: resource android:attr/ttcIndex not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2902: error: resource android:attr/startX not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2905: error: resource android:attr/startY not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2908: error: resource android:attr/endX not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2911: error: resource android:attr/endY not found.
D:\Project\Clients\node_modules\react-native-activity-result\android\build\intermediates\res\merged\release\values\values.xml:2919: error: resource android:attr/offset not found.
error: failed linking references.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.4.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 2m 54s
210 actionable tasks: 22 executed, 188 up-to-date
D:\Project\Clients\android>
Used to face this, it was an APPT2 issue (at least in my case). From the screenshot shared, it tells you Duplicate resources. This may happen after you performed a js bundling, and few folders with resources were created.
Below's how I take care APPT2 issue:
Try navigating to (android/app/build/intermediates/res/merged/release/
See if there's any drawable-* folder
Perform rm -rf android/app/build/intermediates/res/merged/release/drawable-*)
Try again ./gradlew assembleRelease (you probably still fail to build here)
Back to the same folder, remove .DS_Store
Build again
Hopefully succeed
In some react native version it is a bug. you can do the following:
in the node_modules/react-native folder, find the react.gradle file,
then after the doFirst add the following code:
doLast {
def moveFunc = { resSuffix ->
File originalDir =
file("$buildDir/generated/res/react/release/drawable-${resSuffix}");
if (originalDir.exists()) {
File destDir =
file("$buildDir/../src/main/res/drawable-${resSuffix}");
ant.move(file: originalDir, tofile: destDir);
}
}
moveFunc.curry("ldpi").call()
moveFunc.curry("mdpi").call()
moveFunc.curry("hdpi").call()
moveFunc.curry("xhdpi").call()
moveFunc.curry("xxhdpi").call()
moveFunc.curry("xxxhdpi").call()
}
then you can use the ./gradlew assembleRelease generate the apk.
At the same time, when you want to know what the errr is during the build process, you can use ./gradlew assembleRelease --info commod
I FINALLY CAN CREATE RELEASE APK
Create a keystore by running command keytool -genkey -v -keystore myappkeystore.keystore -alias myappalias-keyalg RSA -keysize 2048 -validity 10000 and place it on .\android\app. Details instruction can be follow from here
Open file named build.gradle and look for section name signingConfigs
Insert below script after debug keyname and replace with what key that you register:-
release {
storeFile file('mycreatedkeystore.keystore')
storePassword 'mypassword'
keyAlias 'mykeyalias'
keyPassword 'mypassword'
}
Open blank notepad and paste below script:-
#echo off
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )
set driveName="%CD:~0,2%"
set currentFolder="%~dp0"
setlocal enableextensions enabledelayedexpansion
set /A count=1
set list=
CD %driveName%
echo ===========================================
for /d %%D in (*) do (
set "listName=%%~f"
set list=%listName%%1
rem echo !listName! !
echo !count!^. %%~D
set /a count+=1
)
echo ===========================================
:startAgain
set choice=
set /p choice=Type in target folder from listing to build release APK (e.g. targetfolder):
if not '%choice%'=='' (
if exist %choice% (
set choice=%choice%
CD %choice%
goto performbuild
)
)
echo Invalid directory name!
goto startAgain
:performbuild
echo Script will start build target APK to release on %choice%...
timeout /t 2 /nobreak>nul
call react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/
rem start "" %currentFolder%%choice%^\android\app\src\main\res
timeout /t 2 /nobreak>nul
cd %currentFolder%%choice%^\android\app\src\main\res
echo Validating duplicating resources
if exist "drawable-hdpi" rmdir "drawable-hdpi" /q /s
if exist "drawable-mdpi" rmdir "drawable-mdpi" /q /s
if exist "drawable-xhdpi" rmdir "drawable-xhdpi" /q /s
if exist "drawable-xxhdpi" rmdir "drawable-xxhdpi" /q /s
if exist "drawable-xxxhdpi" rmdir "drawable-xxxhdpi" /q /s
if exist "raw" rmdir "raw" /q /s
cd %currentFolder%%choice%^\android
call gradlew assembleRelease -x bundleReleaseJsAndAssets
start "" %currentFolder%%choice%^\android\app\build\outputs\apk\release"
cd %currentFolder%%choice%
:choiceOpt
set /P answ=Build complete. Do you want to rebuild again[Y/N]?
if /I "%answ%" EQU "Y" goto :performbuild
if /I "%answ%" EQU "N" goto :justexit
goto :choiceOpt
:justexit
echo Exiting
timeout /t 2 /nobreak>nul
exit
Save the file with any name and set the extension to .BAT
Place the BAT file on one folder up of your root project e.g.
Your project folder:- C:\Project\Mobile\MyCurrentProject
Your batch file folder:- C:\Project\Mobile\releaseTool.bat
Run the releaseTool.bat and the batch file with build the release version
I search and found several script to accomplish certain oart and combine it to make this batch script for RELEASE APK version
Disclaimer: I'm not an expert to elaborate the process or argue regarding the way I'm doing it (if it is not practical) because I have failed to build what I wanted and pieces-by-pieces solution is combined and become what I provided.

Categories

Resources