Detecting if build is apk or aab at runtime?

I need to detect at runtime if an app is an APK or an AAB. If it’s not an AAB, then it throws an error if I try to access asset packs.

I see that there’s an enum at Unity.Android.Types.AndroidApplicationType that specifies between apk and aab, but I don’t see any methods that return that type, and searching Unity’s docs for that type turns up nothing.

Can someone tell me how I can do this?

thanks!

AAB is just an install package, when you do build & run with aab, the apk is extracted from aab, and apk is installed on the device.

The same happens when you install application from Google Play Console, even if you upload aab, the apk extracted from aab will be installed on the device.

More info - bundletool  |  Android Studio  |  Android Developers

My primary issue is getting that play core error if I try to access asset packs without an AAB and I was hoping I could access Unity.Android.Types.AndroidApplicationType somewhere to fix it. Do you have any suggestions for getting around that error on apk builds?

I think you can always check if you have access to java class like - com.google.android.play.core.assetpacks.AssetPackManagerFactory (using Unity - Scripting API: AndroidJavaClass), if you can’t access it, that means asset pack logic cannot work, and the build was produced without asset pack support

Might be due to PAD package is being added to the gradle dependencies only when building AAB? Not an expert, but wouldn’t including these dependencies manually solve the issue?

great! thanks! I’ll give this a try!

I’d rather not PAD my binary any more than I have to (HAR HAR HAR!)

this did the trick. Thanks!

here’s my code to assist future generations:

    /// <summary>
    /// check to see if the build is AAB or APK
    /// </summary>
    /// <returns>true if build is AAB, false if build is APK</returns>
    public bool BuildIsAAB()
    {
        if(Application.platform != RuntimePlatform.Android)
        {
            Debug.Log("non-Android build detected");
            return false;
        }

        try
        {
            AndroidJavaClass classTest = new("com.google.android.play.core.assetpacks.AssetPackManagerFactory");
            Debug.Log("AAB build detected");
            return true;
        }
        catch
        {
            Debug.Log("APK build detected");
            return false;
        }
    }