Skip to content

Android - Optimization #392

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/UnoCore/Targets/Android/Android.uxl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
<Set Outputs.AAR="app/build/outputs/aar/app-@(Build.Configuration:ToLower).aar" />
<Set Outputs.APK="app/build/outputs/apk/@(Build.Configuration:ToLower)/app-@(Build.Configuration:ToLower).apk" />
<Set Outputs.Bundle="app/build/outputs/bundle/@(Build.Configuration:ToLower)/app-@(Build.Configuration:ToLower).aab" />

<Set ProGuard.PrependRulesFile="@(Project.Android.Optimize.PrependRulesFile:ToLower)" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming: I think Proguard should be written with lower-case g (several instances). (In Gradle we write proguardSomething and not proGuardSomething.)

<Set ProGuard.AppendRulesFile="@(Project.Android.Optimize.AppendRulesFile:ToLower)" />

<!-- Run-time properties -->

Expand Down
44 changes: 41 additions & 3 deletions lib/UnoCore/Targets/Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ android {
targetSdkVersion @(SDK.TargetVersion)
versionCode = @(Project.Android.VersionCode)
versionName = '@(Project.Android.VersionName)'
multiDexEnabled @(Project.Android.MultiDexEnabled:IsSet:Test(@(Project.Android.MultiDexEnabled:Bool),true))

#if @(Project.Android.Optimize.MinifyEnabled:Test(1, 0))
//minifier handles multi-dexing for release
#if @(DEBUG:Defined)
multiDexEnabled @(Project.Android.MultiDexEnabled:IsSet:Test(@(Project.Android.MultiDexEnabled:Bool),true))
#endif
#else
multiDexEnabled @(Project.Android.MultiDexEnabled:IsSet:Test(@(Project.Android.MultiDexEnabled:Bool),true))
#endif

ndk {
#if @(DEBUG:Defined)
Expand Down Expand Up @@ -83,6 +91,12 @@ android {

}

//prevent gradle build crash - https://stackoverflow.com/a/47689687/2139770
dexOptions {
javaMaxHeapSize "4g"
}


#if @(Project.Android.Bundle.Language.EnableSplit:IsSet) || @(Project.Android.Bundle.Density.EnableSplit:IsSet) || @(Project.Android.Bundle.ABI.EnableSplit:IsSet)
bundle {
#if @(Project.Android.Bundle.Language.EnableSplit:IsSet)
Expand Down Expand Up @@ -133,8 +147,32 @@ android {
#elif @(Project.Android.Key.Store:IsSet)
signingConfig = signingConfigs.release
#endif
minifyEnabled = false
proguardFiles 'proguard-rules.pro'

#if @(Project.Android.Optimize.MinifyEnabled:IsSet)
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled @(Project.Android.Optimize.MinifyEnabled:ToLower)
#else
minifyEnabled true
#endif

#if @(Project.Android.Optimize.ShrinkResources:IsSet)
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources @(Project.Android.Optimize.ShrinkResources:ToLower)
#else
shrinkResources true
#endif
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
#if @(Project.Android.Optimize.DefaultAndroidProGuard:IsSet)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming: I don't think we should repeat Android in this property name. I suggest Project.Android.Optimize.DefaultProguard:IsSet.

'@(Project.Android.Optimize.DefaultAndroidProGuard:ToLower)'
#else
'proguard-android.txt'
#endif
), 'proguard-rules.pro'
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/tool/engine/Targets/AndroidBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,20 @@ public override void DeleteOutdated(Disk disk, IEnvironment env)
var lines = new List<string> {"## This file was generated by Uno compiler."};
var src = env.GetOutputPath("Java.SourceDirectory");

// Prepend user-defined proguard rules.
var proGuardPrependFile = env.GetString("ProGuard.PrependRulesFile");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming: proguardPrependFile, Proguard.PrependRulesFile?

if (proGuardPrependFile.IsValidPath())
lines.AddRange(File.ReadAllLines(proGuardPrependFile));

// Add existing Java classes.
if (Directory.Exists(src))
VisitSourceDirectoryRecursive(src, src, lines);

// Append user-defined proguard rules.
var proGuardAppendFile = env.GetString("ProGuard.AppendRulesFile");
if (proGuardAppendFile.IsValidPath())
lines.AddRange(File.ReadAllLines(proGuardAppendFile));

// End with newline.
lines.Add("");

Expand Down