Other [beta] B4A v14.2 is available for download - R8 optimizer

Erel

B4X founder
Staff member
Licensed User
Longtime User
A new beta version is available for download. This update fixes two regression bugs and adds support for R8 optimizer.
Download link: www.b4x.com/android/files/beta.exe

The fixed bugs:
1. Intermittent "signature does not match..." error in debug mode.
2. Resources of implicit referenced libraries were missing in some cases.

And about R8:

The Java compiler generates Java bytecode. The bytecode is then converted to an Android specific format named DEX. This step is done by default with a tool named D8.

There is an alternative"dexing" tool available named R8. R8 goes farther than D8 and it also optimizes, shrinks and obfuscates the compiled code, including the references. It evolved from a tool named Proguard.

The downsides of R8 are:
1. It is much slower than D8. It does more work and it isn't incremental.
2. It can break the app if not configured properly and configuration isn't always trivial. With that said, it is not too complicated and mostly requires some patience.
The configuration is done with "proguard files".

Starting from Feb. 2027, apps with compiled code (DEX files), larger than 10mb, need to be optimized or they will become less visible in Google Play: https://android-developers.googlebl...ty-memory-optimization-secure-onboarding.html

The main source of problems is dynamically called APIs, for example with JavaObject. The B4A compiler tries to identify such calls and generates a set of "keep" rules but not all cases can be traced automatically and these calls can also happen inside dependant libraries.
Another type of error is related to missing optional dependencies. These errors happen during compilation and are simple to fix with a "-dontwarn" rule.

Two relevant files are generated during compilation when R8 is enabled:
Objects\manifest_auto.proguard - rules based on AndroidManifest.xml, JavaObject calls and a few other hueristics.
Objects\manifest_explicit.proguard - manually added rules. These rules are added in the manifest editor with the new AddProguardText keyword.
Example:
B4X:
AddProguardText(
    -keep class okhttp3.OkHttpClient { *; }
    -keep class okhttp3.Dispatcher { *; }
    -keep class okhttp3.internal.connection.RealCall$CallReference { *; }
    -keep class okhttp3.internal.connection.RealCall { *; }
    -keep class okhttp3.Request { *; }
)
Note that it can be called multiple times.

Additionaly, jars can have an accompanying <library>.proguard file, b4xlibs can have an embedded proguard.txt file, and AARs can also embed proguard rules. If adventerous check Objects\r8_arguments.txt to see the arguments that are passed to R8 tool.
Existing libraries compiled as jars will probably require manual rules. If possible library developers should recompile and distribute the generated proguard file. b4xlibs are handled as source code internally so will be processed by the compiler (although manual rules might be needed and can be embedded, see SimpleMediaManager for example).

B4A relies on dynamic calling and B4A modules are excluded from renaming and removal by default. When you compile a library as jar, a proguard file will be generated as well.
With non-small projects, most of the dexed code comes from referenced SDKs and R8 can easily shrink the DEX size by ~80%.

R8 emits a mapping file named Objects\mapping.txt. This file maps the original names with the new names. Note that it is not related to B4A obfuscation.
The file is also embedded in the AAB file when building an app bundle.
If you get a runtime error about a missing obfuscated class then find its original name by searching the following term in this file: "-> [obfuscated name]:".

Enabling R8:
B4X:
#R8Optimizer: True
Once you tested it and especially in release mode, you can limit it to the app bundle:
B4X:
#if AAB
#R8Optimizer: True
#End If

To conclude:

1. D8 is still the default dexer and it works exactly as before.
2. It is recommended to test your app with #R8Optimizer: True. Report issues encountered in the forum and we will help you solve them. Later on I will create a list of common issues and their solutions, similar to B4J standalone package thread.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Which views, and especially custom views, should we adapt for R8? Based on what criteria?
In most cases - none. Especially with b4xlibs.
If compiled as jar then it is recommended to compile it again with the new version and distribute the proguard file together with the jar and xml files.
 
Upvote 0

asales

Expert
Licensed User
Longtime User
I publish an update of a app with few users and the message has gone.

This app has not Google Play Billing and uses only banner and because this I don't get this error:
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Attached are updated versions of FirebaseAdMob2 and GooglePlayBilling, including a missing dependency and new proguard rules. Copy all files to the internal libraries folder.
I updated the libraries and tried to compile aab (obfuscated release) with FirebaseAdMob2 and GooglePlayBilling.
Here is the error:
B4X:
B4A Version: 14.20 BETA #1
Parsing code.    (0.19s)
    Java Version: 19
Building folders structure.    (0.02s)
Compiling code.    (0.25s)
 
ObfuscatorMap.txt file created in Objects folder.
Compiling layouts code.    (0.01s)
Organizing libraries.    Error
Maven artifact not found: androidx.room/room-common-jvm
Source: FirebaseAdMob2

edit: Using the latest proguard rule doesn't change.

update:
Now I am able to compile after installing the missing file using SDK Manager.
1790876462682.png


update: It fixed the issue for FirebaseAdMob2 (without GooglePlayBilling) but then I try to compile another app with GooglePlayBilling, I get another error.

B4X:
B4A Version: 14.20 BETA #1
Parsing code.    (0.19s)
    Java Version: 19
Building folders structure.    (0.09s)
Compiling code.    (0.22s)
  
ObfuscatorMap.txt file created in Objects folder.
Compiling layouts code.    (0.01s)
Organizing libraries.    (0.00s)
    (AndroidX SDK)
Compiling resources    (0.12s)
Linking resources    (1.25s)
    build tools: 36.0.0, android jar: android-36
Compiling generated Java code.    (1.72s)
Selecting dexer based on #R8Optimizer flag.    (0.00s)
Dexing and optimizing with R8    Error
Error: Missing class androidx.interpolator.view.animation.FastOutLinearInInterpolator (referenced from: android.view.animation.Interpolator androidx.vectordrawable.graphics.drawable.AnimationUtilsCompat.loadInterpolator(android.content.Context, int))
Missing class androidx.interpolator.view.animation.FastOutSlowInInterpolator (referenced from: android.view.animation.Interpolator androidx.vectordrawable.graphics.drawable.AnimationUtilsCompat.loadInterpolator(android.content.Context, int))
Missing class androidx.interpolator.view.animation.LinearOutSlowInInterpolator (referenced from: android.view.animation.Interpolator androidx.vectordrawable.graphics.drawable.AnimationUtilsCompat.loadInterpolator(android.content.Context, int))
Compilation failed
Exception in thread "main" java.lang.RuntimeException: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
    at com.android.tools.r8.internal.Nu.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:131)
    at com.android.tools.r8.R8.main(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:5)
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
    at Version.fakeStackEntry(Version_8.9.27.java:0)
    at com.android.tools.r8.S.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:5)
    at com.android.tools.r8.internal.Nu.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:82)
    at com.android.tools.r8.internal.Nu.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:32)
    at com.android.tools.r8.internal.Nu.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:31)
    at com.android.tools.r8.R8.b(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:48)
    at com.android.tools.r8.R8.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:1589)
    at com.android.tools.r8.internal.Nu.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:118)
    ... 1 more
Caused by: com.android.tools.r8.internal.g: Missing class androidx.interpolator.view.animation.FastOutLinearInInterpolator (referenced from: android.view.animation.Interpolator androidx.vectordrawable.graphics.drawable.AnimationUtilsCompat.loadInterpolator(android.content.Context, int))
Missing class androidx.interpolator.view.animation.FastOutSlowInInterpolator (referenced from: android.view.animation.Interpolator androidx.vectordrawable.graphics.drawable.AnimationUtilsCompat.loadInterpolator(android.content.Context, int))
Missing class androidx.interpolator.view.animation.LinearOutSlowInInterpolator (referenced from: android.view.animation.Interpolator androidx.vectordrawable.graphics.drawable.AnimationUtilsCompat.loadInterpolator(android.content.Context, int))
    at com.android.tools.r8.internal.Le0.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:21)
    at com.android.tools.r8.internal.Le0.error(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:1)
    at com.android.tools.r8.shaking.M.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:1975)
    at com.android.tools.r8.shaking.M.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:1622)
    at com.android.tools.r8.R8.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:1510)
    at com.android.tools.r8.R8.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:206)
    at com.android.tools.r8.R8.c(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:8)
    at com.android.tools.r8.R8.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:1588)
    at com.android.tools.r8.internal.Nu.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:28)
    ... 4 more
    Suppressed: java.lang.RuntimeException: com.android.tools.r8.internal.g: Missing class androidx.interpolator.view.animation.FastOutLinearInInterpolator (referenced from: android.view.animation.Interpolator androidx.vectordrawable.graphics.drawable.AnimationUtilsCompat.loadInterpolator(android.content.Context, int))
Missing class androidx.interpolator.view.animation.FastOutSlowInInterpolator (referenced from: android.view.animation.Interpolator androidx.vectordrawable.graphics.drawable.AnimationUtilsCompat.loadInterpolator(android.content.Context, int))
Missing class androidx.interpolator.view.animation.LinearOutSlowInInterpolator (referenced from: android.view.animation.Interpolator androidx.vectordrawable.graphics.drawable.AnimationUtilsCompat.loadInterpolator(android.content.Context, int))
        at com.android.tools.r8.internal.Le0.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:27)
        at com.android.tools.r8.shaking.M.a(R8_8.9.27_3afcc9fea5cc5cd06373667ec09d5fe106db8a4a77576689534e667542385a93:1977)
        ... 10 more
    Caused by: [CIRCULAR REFERENCE: com.android.tools.r8.internal.g: Missing class androidx.interpolator.view.animation.FastOutLinearInInterpolator (referenced from: android.view.animation.Interpolator androidx.vectordrawable.graphics.drawable.AnimationUtilsCompat.loadInterpolator(android.content.Context, int))
Missing class androidx.interpolator.view.animation.FastOutSlowInInterpolator (referenced from: android.view.animation.Interpolator androidx.vectordrawable.graphics.drawable.AnimationUtilsCompat.loadInterpolator(android.content.Context, int))
Missing class androidx.interpolator.view.animation.LinearOutSlowInInterpolator (referenced from: android.view.animation.Interpolator androidx.vectordrawable.graphics.drawable.AnimationUtilsCompat.loadInterpolator(android.content.Context, int))]

1790877748448.png

update:
It turns out it is the AppCompat library I am using in the app.
To fix it (answer from Google AI Mode), add the following to the Manifest Editor:
B4X:
' Fix for the missing class androidx.interpolator error under R8 full optimization
AddProguardText(
-keep class androidx.interpolator.view.animation.** { *; }
-dontwarn androidx.interpolator.view.animation.**
)
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Not sure it is just me.
B4A IDE window opens in 50% width and "snap" to the right side of my screen.
I close and open again but the position and size were not remembered on next open.
The list of recent projects are also not remembered.
I think something must be wrong with INI or config.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
About AppCompat, you can remove those rules and instead add:
B4X:
#AdditionalJar: androidx.interpolator:interpolator
#AdditionalJar: androidx.tracing:tracing

I've updated AppCompat.xml with these.

Not sure it is just me.
B4A IDE window opens in 50% width and "snap" to the right side of my screen.
I close and open again but the position and size were not remembered on next open.
The list of recent projects are also not remembered.
I think something must be wrong with INI or config.
Maybe there are several instances of B4A open?
You can check the ini file and see whether it gets updated when you close the IDE.

C:\Users\<user name>\AppData\Roaming\Anywhere Software\Basic4android\b4xV5.ini
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
About AppCompat, you can remove those rules and instead add:
B4X:
#AdditionalJar: androidx.interpolator:interpolator
#AdditionalJar: androidx.tracing:tracing

I've updated AppCompat.xml with these.


Maybe there are several instances of B4A open?
You can check the ini file and see whether it gets updated when you close the IDE.

C:\Users\<user name>\AppData\Roaming\Anywhere Software\Basic4android\b4xV5.ini
I backup the old ini file and deleted the b4xV5.ini in B4A installation path.
I think I may have wrongly copied the ini file there while testing the deployment without installation.
It can also happen when I have B4A opened during installation of the beta.
It is now generated a new INI file and the problems gone.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
BTW, the R8 related compile-time errors are actually a good thing. R8 is more strict in regarding to missing dependencies and it can help fix configuration issues that might fail at runtime and are more difficult to debug.
I wouldn't say so. One of the many strengths of B4A (and the other IDEs) is precisely how easy it is to set up.

30 seconds and you're ready to go (okay, let's say 60 seconds :) ).

Google, we love you! <-- Just "kidding"! :mad:
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
The first time right after install I ran with #r8optimizer i got all kinds of errors so I went with just doing a normal compile with #r8optimizer set to false
and get this.

Seems like the first time I ran with #r8 none of the dex-ing happened.

This time it does but I get an error about 7-Zip

Is 7-Zip a NOW required?

1791034657537.png


Is
 
Upvote 0
Top