Use Proguard with Unity

Hi,

I am looking to strip/reduce my APK size. I read that Proguard could do that by removing useless code and by shrinking some Jar libraries.

Do you know how to use Proguard within Unity ?

I read some post like How to use Proguard in Unity? - Unity Engine - Unity Discussions but no answer founded yet.

Thank you.

Ps : I already compressed textures.

I never tried it myself, but AFAIK, Unity doesn’t let you hook into the build process - it runs all the Android toolchain for you in order to create an APK so you cannot run ProGuard in the middle (ProGuard runs at the code level, not on an APK).

The other option would be to generate an Android project, and then you could patch in a build script to add ProGuard to that. Not sure what would be the effects on build size, but there may be other, better options to cut the build size…

Thanks Liortal

I export an empty Unity project and compile it with Android Studio using Proguard (by the way, I needed to update Proguard because it doesn’t support Java 1.8). It works, and the apk has been reduced by 10kb using Proguard (wohou)

BUT I cannot figure how to compile an Unity Android Google Project with depedencies (like Facebook plugins or even play-services only). Each time I receive multiple error.

Here is what I do:

  • export the Unity project (a ones with google play-service_lib)
  • open Android Studio, click on Import project (Eclipse ADT, Gradle, etc.)
  • select the repository where I build the Android Project
  • Click on Create project from existing sources, then next next next… finish
  • (by the way, Android Studio ask if I want to migrate the project to Gradle)

I can make and build and even install the project, but when I start it, it begins and crash at the first scene.
Logcat say : AndroidJavaException: java.lang.ClassNotFoundException: com.google.android.gms.ads.AdRequest$Builder

(And yes, it works if I build directly from Unity)

I could not event build a more complex project with facebook plugins and other stuff. When I build, it says:
Error : Android Packager: [appliname] java.util.zip.ZipException: duplicate entry: AndroidManifest.xml
(I try to google it, but no solution founded)

Do you know what I am doing wrong ? Maybe in the import process ?

Never tried to export a Google Android project, so no idea.

I would assume that Unity generates this project and then compiles it for you to produce an APK, so this option should just give the intermediate output, but I may be wrong on this.

I did it !!!

Export Android Google Project with Unity,
Then import my project and their library into Eclipse with ADT Android
Then I had to manually link all missing libraries (pain in my ****)
Then I had to remove in some .jar stuff like Android manifest and some icons which was detected as Duplicated by compilator. (simply open them with 7zip and manually remove)

now, next step : using Proguard within Eclipse !

1 Like

Ok, to use proguard within Unity it was simple:
Add a line in under project.properties:

proguard.config=[patchToAndroidSDK]\tools\proguard\proguard-android-optimize.txt

Results:
A build using Facebook and Google Play Services plugins weight
47 Mo using Unity or Eclipse
45.2 Mo using Proguard
I think Proguard is doing his job with Facebook which is the only not offusqued library

Thanks for your help liortal !

where i can find project.properties ??

you have to create one yourself at the root of the Android project i think.

I just don’t get it to work.

Did someone managed to get ProGuard to work with a Unity Android export?
I tried to use Proguard directly onto the export, and i tried to import it first into Android Studio and run ProGuard over the imported project. Both times, i get like 100 Warning about how proguard wasn’t able to find classes or access stuff.

Does someone how to do this or if there are any other ways to obfuscate my code? More important, are there other ways to reduce the filesize of APKs??

The Huge builds Unity creates is (in my opinion) the most annoying thing of the engine.

By the way - Unity 5.5 beta has the option to export a gradle project, with ProGuard support.

@HeaDiii you have to deal with the proguard output and fix it :eyes:

@Yury-Habets It would be great if you can point on how to use the proguard in Unity. I see proguard.txt files are getting ignored if kept with in the android libraries and also there is one more option in Minify - Gradle(Experimental) along with Proguard.

Can any one from Unity give detailed instructions on where to place our proguard details?
Thanks,
VB Team

1 Like

Starting with Unity 2017.1 (i think), you can specify to use a proguard file under player settings. How to configure this file is a black art of its own… I don’t think it has anything to do with Unity though. it’s really up to your project and the libraries you use.

1 Like

@liortal correct - this is the main proguard file for the project.

Speaking of libraries - you should be able to have your own proguard file, and your own build.gradle which has a reference to your proguard file. (haven’t tried that but it should work IMO)

@Yury-Habets we are using a custom gradle file with a custom proguard file.

Note that starting with Unity 2017.1, you can specify to use a proguard file under Player settings which may lead to inconsistent results. For example:

Player Settings → Custom proguard file is enabled (creates it under Assets/Plugins/Android/proguard-user.txt)
Custom gradle file specifies a proguard file with a different name (for example - proguard-unity.txt)

if you are using Unity3d 5.6.3f1 onwards, you may use the proguard-user.txt file together with the mainTemplate.gradle to proceed with Proguard.

1 Like

So we need to keep the gradle file and refer the proguard? I was thinking it will by default consider proguard.txt file.

Let me try that.

We tried this but found setting proguard to off in the plugin’s build.gradle is not considered. Can you please confirm the behaviour?

@Voxel-Busters

Can you provide more details? Maybe file a case with a repro project.

In the meanwhile, this is what I found with regards to AAR and proguard:
https://stackoverflow.com/questions/26983248/proguard-ignores-config-file-of-library
https://stackoverflow.com/questions/40232189/how-to-apply-proguard-to-aar-library-in-android
https://stackoverflow.com/questions/41572630/consumerproguardfiles-vs-proguardfiles

Will try to integrate it into Unity plugins like OBBDownloader.

1 Like

@liortal what is used in this case? What do you expect it to use?
If you are using a custom build.gradle - then the player settings won’t be applied - my assumption.