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…
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.
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)
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
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.
@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
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.
@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)
@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.