1
- // Copyright (C) 2011 Xamarin, Inc. All rights reserved.
1
+ // Copyright (C) 2011 Xamarin, Inc. All rights reserved.
2
2
3
3
using System ;
4
4
using System . Collections . Generic ;
12
12
13
13
namespace Xamarin . Android . Tasks
14
14
{
15
- public class CreateMultiDexMainDexClassList : ToolTask
15
+ public class CreateMultiDexMainDexClassList : JavaToolTask
16
16
{
17
17
[ Required ]
18
18
public string ClassesOutputDirectory { get ; set ; }
19
19
20
20
[ Required ]
21
- public string ProguardHome { get ; set ; }
21
+ public string ProguardJarPath { get ; set ; }
22
+
23
+ [ Required ]
24
+ public string AndroidSdkBuildToolsPath { get ; set ; }
22
25
23
26
[ Required ]
24
27
public ITaskItem [ ] JavaLibraries { get ; set ; }
25
28
26
29
public string MultiDexMainDexListFile { get ; set ; }
27
30
public ITaskItem [ ] CustomMainDexListFiles { get ; set ; }
28
31
32
+ Action < CommandLineBuilder > commandlineAction ;
33
+ string tempJar ;
34
+ bool writeOutputToKeepFile = false ;
35
+
29
36
public override bool Execute ( )
30
37
{
31
38
Log . LogDebugMessage ( "CreateMultiDexMainDexClassList" ) ;
@@ -35,45 +42,69 @@ public override bool Execute ()
35
42
Log . LogDebugTaskItems ( " CustomMainDexListFiles:" , CustomMainDexListFiles ) ;
36
43
Log . LogDebugMessage ( " ToolExe: {0}" , ToolExe ) ;
37
44
Log . LogDebugMessage ( " ToolPath: {0}" , ToolPath ) ;
38
- Log . LogDebugMessage ( " ProguardHome : {0}" , ProguardHome ) ;
45
+ Log . LogDebugMessage ( " ProguardJarPath : {0}" , ProguardJarPath ) ;
39
46
40
47
if ( CustomMainDexListFiles != null && CustomMainDexListFiles . Any ( ) ) {
41
48
var content = string . Concat ( CustomMainDexListFiles . Select ( i => File . ReadAllText ( i . ItemSpec ) ) ) ;
42
49
File . WriteAllText ( MultiDexMainDexListFile , content ) ;
43
50
return true ;
44
51
}
45
52
46
- EnvironmentVariables = MonoAndroidHelper . GetProguardEnvironmentVaribles ( ProguardHome ) ;
53
+ tempJar = Path . Combine ( Path . GetTempPath ( ) , Path . GetRandomFileName ( ) + ".jar" ) ;
54
+ commandlineAction = GenerateProguardCommands ;
55
+ // run proguard first
56
+ var retval = base . Execute ( ) ;
57
+ if ( ! retval || Log . HasLoggedErrors )
58
+ return false ;
59
+
60
+ commandlineAction = GenerateMainDexListBuilderCommands ;
61
+ // run java second
62
+
63
+ return base . Execute ( ) && ! Log . HasLoggedErrors ;
47
64
48
- return base . Execute ( ) ;
49
65
}
50
66
51
67
protected override string GenerateCommandLineCommands ( )
52
68
{
53
69
var cmd = new CommandLineBuilder ( ) ;
70
+ commandlineAction ( cmd ) ;
71
+ return cmd . ToString ( ) ;
72
+ }
54
73
55
- cmd . AppendSwitch ( "--output" ) ;
56
- cmd . AppendFileNameIfNotNull ( MultiDexMainDexListFile ) ;
57
-
74
+ void GenerateProguardCommands ( CommandLineBuilder cmd )
75
+ {
76
+ var enclosingChar = OS . IsWindows ? " \" " : string . Empty ;
58
77
var jars = JavaLibraries . Select ( i => i . ItemSpec ) . Concat ( new string [ ] { ClassesOutputDirectory } ) ;
59
- string files = string . Join ( Path . PathSeparator . ToString ( ) , jars . Select ( s => '\' ' + s + '\' ' ) ) ;
60
- if ( OS . IsWindows )
61
- cmd . AppendSwitch ( '"' + files + '"' ) ;
62
- else
63
- cmd . AppendSwitch ( files ) ;
64
-
65
- return cmd . ToString ( ) ;
78
+ cmd . AppendSwitchIfNotNull ( "-jar " , ProguardJarPath ) ;
79
+ cmd . AppendSwitchUnquotedIfNotNull ( "-injars " , $ "{ enclosingChar } '" + string . Join ( $ "'{ Path . PathSeparator } '", jars ) + $ "'{ enclosingChar } ") ;
80
+ cmd . AppendSwitch ( "-dontwarn" ) ;
81
+ cmd . AppendSwitch ( "-forceprocessing" ) ;
82
+ cmd . AppendSwitchIfNotNull ( "-outjars " , tempJar ) ;
83
+ cmd . AppendSwitchIfNotNull ( "-libraryjars " , $ "'{ Path . Combine ( AndroidSdkBuildToolsPath , "lib" , "shrinkedAndroid.jar" ) } '") ;
84
+ cmd . AppendSwitch ( "-dontoptimize" ) ;
85
+ cmd . AppendSwitch ( "-dontobfuscate" ) ;
86
+ cmd . AppendSwitch ( "-dontpreverify" ) ;
87
+ cmd . AppendSwitchIfNotNull ( "-include " , $ "'{ Path . Combine ( AndroidSdkBuildToolsPath , "mainDexClasses.rules" ) } '") ;
66
88
}
67
89
68
- protected override string ToolName {
69
- get {
70
- return OS . IsWindows ? "mainDexClasses.bat" : "mainDexClasses" ;
71
- }
90
+ void GenerateMainDexListBuilderCommands ( CommandLineBuilder cmd )
91
+ {
92
+ var jars = JavaLibraries . Select ( i => i . ItemSpec ) . Concat ( new string [ ] { ClassesOutputDirectory } ) ;
93
+ cmd . AppendSwitchIfNotNull ( "-Djava.ext.dirs=" , Path . Combine ( AndroidSdkBuildToolsPath , "lib" ) ) ;
94
+ cmd . AppendSwitch ( "com.android.multidex.MainDexListBuilder" ) ;
95
+ cmd . AppendSwitch ( tempJar ) ;
96
+ cmd . AppendSwitchUnquotedIfNotNull ( "" , "\" " + string . Join ( $ "{ Path . PathSeparator } ", jars ) + "\" " ) ;
97
+ writeOutputToKeepFile = true ;
72
98
}
73
99
74
- protected override string GenerateFullPathToTool ( )
100
+ protected override void LogEventsFromTextOutput ( string singleLine , MessageImportance messageImportance )
75
101
{
76
- return Path . Combine ( ToolPath , ToolExe ) ;
102
+ var match = CodeErrorRegEx . Match ( singleLine ) ;
103
+ var exceptionMatch = ExceptionRegEx . Match ( singleLine ) ;
104
+
105
+ if ( writeOutputToKeepFile && ! match . Success && ! exceptionMatch . Success )
106
+ File . AppendAllText ( MultiDexMainDexListFile , singleLine ) ;
107
+ base . LogEventsFromTextOutput ( singleLine , messageImportance ) ;
77
108
}
78
109
}
79
110
}
0 commit comments