Extract APK Info for multiple flavors

Hello,

I have an Android project with multiple flavors. In my current workflow, the step “Deploy to Bitrise” generates one public URL per flavor, so I get as many APK as there are flavors.

I want to send the details of all generated APKs to an external webservice, using a script. I’ve added a step “APK Info” which creates the variables $ANDROID_APP_PACKAGE_NAME, $ANDROID_APP_VERSION_NAME, etc. Then I run a script which works fine, but only for one APK (I guess the lastly generated APK).

To solve that, my idea would be to run one “Apk info” step per flavor, but my Gradle Runner step only generates one $BITRISE_APK_PATH. Is that a good approach ? If yes, how can I modify the Gradle Runner step to generate multiple environnement variable (one for each flavor).

Thanks in advance for your feedback.

Hey @raphaelmina! :wave:t2:

@godreikrisztian:

So all of your apks are moved to BITRISE_DEPLOY_DIR but only the last one is exported into BITRISE_APK_PATH env.

Every step after the gradle-runner uses BITRISE_APK_PATH (so the last apk)

i think the easy solution would be to:

search every file with .apk extension in BITRISE_DEPLOY_DIR
then loop over this apks, collect the desired infos and upload per apk

You can do this with a script step:

package_name_regex="package: name='(.+)' versionCode='(.+)' versionName='(.+)' "
app_name_regex="application: label='(.+)' icon='(.+)'"

find_apks_output="$(find $BITRISE_DEPLOY_DIR -name *release.apk)"
while IFS= read -r apk
do
    apk_size=$(wc -c <"$apk")

    infos="$($ANDROID_HOME/build-tools/25.0.2/aapt dump badging $apk)"
    while read -r line 
    do
        # echo "... $line ..."
        if [[ $line =~ $package_name_regex ]]
        then
            package_name="${BASH_REMATCH[1]}"
            

            version_code="${BASH_REMATCH[2]}"
            

            version_name="${BASH_REMATCH[3]}"
        elif [[ $line =~ $app_name_regex ]]
        then
            app_name="${BASH_REMATCH[1]}"

            icon_path="${BASH_REMATCH[2]}"
            
        fi
    done <<< "$infos"

    echo "BITRISE_APK_PATH: $apk"
    echo "ANDROID_APP_PACKAGE_NAME: $package_name"
    echo "ANDROID_APK_FILE_SIZE: $apk_size bytes"
    echo "ANDROID_APP_NAME: $app_name"
    echo "ANDROID_APP_VERSION_NAME: $version_name"
    echo "ANDROID_APP_VERSION_CODE: $version_code"
    echo "ANDROID_ICON_PATH: $icon_path"
    echo "BITRISE_PUBLIC_INSTALL_PAGE_URL: $BITRISE_PUBLIC_INSTALL_PAGE_URL"

    echo
done <<< "${find_apks_output}"

Please take care when you run the script, the following path can be different, please change the version accordingly:

 infos="$($ANDROID_HOME/build-tools/>25.0.2</aapt dump badging $apk)"

Thank you for reporting this @raphaelmina!
:tada::rocket:

2 Likes

Thanks for this complete solution, it works perfect.

2 Likes