Proguard over Jogl (and Jbullet) in Android Studio

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Proguard over Jogl (and Jbullet) in Android Studio

philjord
For anyone who wants to run Proguard over Jogl (and Jbullet) in Android Studio here are the steps that worked for me.


Firstly you have to turn on minify, which basically turns on proguard.

Open your build.gradle file, in my case I have one under the project directory and one under the app, I used the one under the app for these changes. (I don't know what the project one does exactly)
To turn on minify and tell proguard where to find the rules add lines like this

 buildTypes {
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            zipAlignEnabled true
        }
    }

I also added something similar in the release build type (see my build.gradle file below for details).

Once I had done this it complained about not being able to find a file, which I discovered was the proguard-rules.pro file, as that file didn't happen to exist in my project. However a newly created project in the latest version of android studio will have it in the right place, so I created a new project and took the file from that across to my project. It turns out it's just a text file with some comments in the app folder.

Then I attempted to build and got thousands of warnings and messages, ending with a message from proguard saying it won't build with all those warnings.

It seemed like an enormous pile of warnings that would take forever to trawl through, but it turns out that proguard only cares about class files it can't find and refuses to build because of that. So it can be made to build by simply adding this line
-ignorewarnings
in the proguard-rules.pro file.

However that's a bit heavy handed so in my proguard-rules.pro file (see below) I've added specific -dontwarn lines for those classes where it's fine. This means in the future I'll get a warning if there's a real problem.

This behavior of -ignorewarnings is documented here
http://proguard.sourceforge.net/manual/refcard.html
It seems like it would be a dangerous thing, but it truth no non-trivial project could not include it (or a pile of -dontwarn lines)

Once it's built and run, you will get an output like this:
com.jogamp.opengl.ag: No default device available

This is caused by a class (or classes) using the name of another class to construct or reference it (by Class.forname or something similar). So to stop this problem you have to -keep those classes that are referred to unchanged.
The simplest version of -keep that works is to add these two lines
-keep class com.jogamp.** { *; }
-keep class jogamp.** { *; }
below the warnings section in the proguard-rules.pro file

But once again that's a bit heavy handed so I tried to close in on a more specific set of classes, as shown in my final proguard-rules.pro file (see below), if you do spend more time investigating an even tighter set of -keep statements that work, please post them here.

So my resultant build.gradle file was was this:
build.gradle
And my resultant proguard-rule.pro file was this:
proguard-rules.pro
 
And from there my debug version ran all small and obfuscated. Which is a pain for debug so I turned it off again...

If your gradle build fails with a comment about fixing warnings then you probably need to extend the -dontwarn statements to include those from your app, just grab every line in the Gradle Console (of Android Studio) that starts like this:
Warning: package1.package2.nameofclass: can't find superclass or interface ...
and add it as a line in your proguard-rules.pro file like this:
-keep package1.package2.nameofclass

I had to do this for 10 packages/classes in my app

Some background:
The reason I wanted proguard to minify my app was as an attempt to speed up JBullet, however after getting proguard to inline the various getQuick and upcast calls that appeared slow I discovered that my problem was much simpler. My cpu profiling was skewing the results, when getting a method call trace the cost of methods that are called often is misrepresented from real cost when the app is running normally (probably just the cost of recording the method call in the trace).
So after proguarding my apk I discovered that the speed was unchanged, System.out.println statements confirmed this.

So proguard does not appear to have improved performance for me at all, despite the inlining benefit mentioned on the web.

However if you want to deploy to the Google store having obfuscation and minimizing apk size on is a very good idea.


Funnily enough I think this post answers this question from 2012
http://forum.jogamp.org/Java3D-and-maybe-proguard-Operational-Questions-td4026161.html



Thanks for listening,
Phil.
Reply | Threaded
Open this post in threaded view
|

Re: Proguard over Jogl (and Jbullet) in Android Studio

gouessej
Administrator
Thanks for sharing :)
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Proguard over Jogl (and Jbullet) in Android Studio

elect
This should go in the wiki, what do you think, guys?
Reply | Threaded
Open this post in threaded view
|

Re: Proguard over Jogl (and Jbullet) in Android Studio

philjord
Elect,
I think it should (and I'm happy for it to) go in the wiki, though I'm not much of one for writing actual content, and I've never written anything in a wiki article (not even once), if you want o tell me what to write I'm happy put some words down but someone would have to take a lead and lay the article out etc.

For now I'm putting things up on the forum as I find them so others can get over their issues more quickly, and as a base of content if an Android article ever gets done.

I've foud a couple of bugs so far as well but I'm not sure if they should go in an article or just be referenced as bugs (one of them is that GLContextKeeper does appear to restore the surface/drawable in android 6.0 which is fairly major).

Phil.
Reply | Threaded
Open this post in threaded view
|

Re: Proguard over Jogl (and Jbullet) in Android Studio

philjord
In case anyone ever needs this thread apparently according to
http://stackoverflow.com/questions/12112514/android-proguard-does-not-inline

Your build.gradle file needs this line
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules-debug.pro'

in order to actually do any optimization

My early test show it does seem to remove or inline some methods at least
Reply | Threaded
Open this post in threaded view
|

Re: Proguard over Jogl (and Jbullet) in Android Studio

elect
In reply to this post by philjord
philjord wrote
Elect,
I think it should (and I'm happy for it to) go in the wiki, though I'm not much of one for writing actual content, and I've never written anything in a wiki article (not even once), if you want o tell me what to write I'm happy put some words down but someone would have to take a lead and lay the article out etc.
Phil,

I am convinced that what you wrote has high interest :)

Since Sven is busy with jogamp bureaucrazy and he is the only one who can create wiki users, we can either do you write here and I copy/paste or I just give you my credential and you write it by yourself

What do you think?