Android Question ProGuard

Informatix

Expert
Licensed User
Longtime User
In Proguard, there's nothing to set specifically for Basic4Android. Here's a working example:
B4X:
-injars 'C:\Program Files\Basic4android\Libraries+\MyLib.jar'
-outjars 'C:\Program Files\Basic4android\Libraries+\MyLib_proguard.jar'

-libraryjars 'C:\Program Files\Basic4android\Libraries\B4AShared.jar'
-libraryjars 'C:\Program Files\Basic4android\Libraries\Core.jar'
-libraryjars 'C:\Android\android-sdk\platforms\android-19\android.jar'

-optimizations !code/allocation/variable
-optimizationpasses 2
-allowaccessmodification
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,EnclosingMethod,*Annotation*
-keepparameternames
-renamesourcefileattribute SourceFile

# Preserve the special static methods that are required in all enumeration
# classes.
-keepclassmembers class * extends java.lang.Enum {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

# Keep - Library. Keep all public and protected classes, fields, and methods.
-keep public class * {
    public protected <fields>;
    public protected <methods>;
}

# Also keep - Serialization code. Keep all fields and methods that are used for
# serialization.
-keepclassmembers class * extends java.io.Serializable {
    static final long serialVersionUID;
    static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

# Keep names - Native method names. Keep all native class/method names.
-keepclasseswithmembers,allowshrinking class * {
    native <methods>;
}

# Keep names - _class method names. Keep all .class method names. This may be
# useful for libraries that will be obfuscated again with different obfuscators.
-keepclassmembers,allowshrinking class * {
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String,boolean);
}
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
There is another feature in ProGuard I think would be nice to have in B4A. ProGuard can eleminate unused code from the apk. Especially when using a library which requires one of the support libraries which are getting bigger and bigger it would be nice to reduce apk size with the use of ProGuard.

Anyone tried this with b4a if it will be worth the effort?
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
There is another feature in ProGuard I think would be nice to have in B4A. ProGuard can eleminate unused code from the apk. Especially when using a library which requires one of the support libraries which are getting bigger and bigger it would be nice to reduce apk size with the use of ProGuard.

Anyone tried this with b4a if it will be worth the effort?
AFAIK ProGuard does not work with APK, only with Java class files. Where did you find this information? That being said, you can try to run ProGuard on your classes before they are converted to Dalvik bytecode but it does not seem that CustomBuildAction allows that.
Apart obfuscation and shrinking, an interesting feature of ProGuard is its code optimization. Your code is modified (at the bytecode level) to be more efficient.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
There is another feature in ProGuard I think would be nice to have in B4A. ProGuard can eleminate unused code from the apk. Especially when using a library which requires one of the support libraries which are getting bigger and bigger it would be nice to reduce apk size with the use of ProGuard.

Anyone tried this with b4a if it will be worth the effort?
[Mode Expert]A trivial way of removing unneeded code is to open the JAR file of your libraries with an archiver (they are .zip in fact) and remove the .class file of the unused classes.[/Mode Expert]
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
AFAIK ProGuard does not work with APK, only with Java class files. Where did you find this information? That being said, you can try to run ProGuard on your classes before they are converted to Dalvik bytecode but it does not seem that CustomBuildAction allows that.
Apart obfuscation and shrinking, an interesting feature of ProGuard is its code optimization. Your code is modified (at the bytecode level) to be more efficient.

Sorry, thats what I meant. It modifies the class files but the result is a smaller apk but it seems that we need a new #CustomBuildAction to use this feature.

Maybe Erel will have a look at it in the future. But I think it is not of a high priority. If I see the size of some apps in the PlayStore most B4A apps are really small. :)
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Maybe Erel will have a look at it in the future. But I think it is not of a high priority. If I see the size of some apps in the PlayStore most B4A apps are really small. :)
Android developpers using Java are used to store graphics at different resolutions in the res folder. This is not encouraged in B4A where a single file, in the Files folder, is used for all resolutions. It's the only reason that I see for the size difference because B4A apps have many reasons to be bigger than pure-Java apps (we use a lot of wrappers).
 
Upvote 0
Top