77using System . Linq ;
88using System . Text ;
99using Microsoft . Build . Framework ;
10+ using Microsoft . Build . Utilities ;
1011
1112public class ApkBuilder
1213{
@@ -32,6 +33,13 @@ public class ApkBuilder
3233 public string ? DiagnosticPorts { get ; set ; }
3334 public ITaskItem [ ] Assemblies { get ; set ; } = Array . Empty < ITaskItem > ( ) ;
3435
36+ private TaskLoggingHelper logger ;
37+
38+ public ApkBuilder ( TaskLoggingHelper logger )
39+ {
40+ this . logger = logger ;
41+ }
42+
3543 public ( string apk , string packageId ) BuildApk (
3644 string abi ,
3745 string mainLibraryFileName ,
@@ -209,7 +217,7 @@ public class ApkBuilder
209217 string cmake = "cmake" ;
210218 string zip = "zip" ;
211219
212- Utils . RunProcess ( zip , workingDir : assetsToZipDirectory , args : "-q -r ../assets/assets.zip ." ) ;
220+ Utils . RunProcess ( logger , zip , workingDir : assetsToZipDirectory , args : "-q -r ../assets/assets.zip ." ) ;
213221 Directory . Delete ( assetsToZipDirectory , true ) ;
214222
215223 if ( ! File . Exists ( androidJar ) )
@@ -274,7 +282,7 @@ public class ApkBuilder
274282 // if lib doesn't exist (primarly due to runtime build without static lib support), fallback linking stub lib.
275283 if ( ! File . Exists ( componentLibToLink ) )
276284 {
277- Utils . LogInfo ( $ "\n Couldn't find static component library: { componentLibToLink } , linking static component stub library: { staticComponentStubLib } .\n ") ;
285+ logger . LogMessage ( MessageImportance . High , $ "\n Couldn't find static component library: { componentLibToLink } , linking static component stub library: { staticComponentStubLib } .\n ") ;
278286 componentLibToLink = staticComponentStubLib ;
279287 }
280288
@@ -339,8 +347,8 @@ public class ApkBuilder
339347 cmakeBuildArgs += " --config Debug" ;
340348 }
341349
342- Utils . RunProcess ( cmake , workingDir : OutputDir , args : cmakeGenArgs ) ;
343- Utils . RunProcess ( cmake , workingDir : OutputDir , args : cmakeBuildArgs ) ;
350+ Utils . RunProcess ( logger , cmake , workingDir : OutputDir , args : cmakeGenArgs ) ;
351+ Utils . RunProcess ( logger , cmake , workingDir : OutputDir , args : cmakeBuildArgs ) ;
344352
345353 // 2. Compile Java files
346354
@@ -369,15 +377,15 @@ public class ApkBuilder
369377 . Replace ( "%MinSdkLevel%" , MinApiLevel ) ) ;
370378
371379 string javaCompilerArgs = $ "-d obj -classpath src -bootclasspath { androidJar } -source 1.8 -target 1.8 ";
372- Utils . RunProcess ( javac , javaCompilerArgs + javaActivityPath , workingDir : OutputDir ) ;
373- Utils . RunProcess ( javac , javaCompilerArgs + monoRunnerPath , workingDir : OutputDir ) ;
374- Utils . RunProcess ( dx , "--dex --output=classes.dex obj" , workingDir : OutputDir ) ;
380+ Utils . RunProcess ( logger , javac , javaCompilerArgs + javaActivityPath , workingDir : OutputDir ) ;
381+ Utils . RunProcess ( logger , javac , javaCompilerArgs + monoRunnerPath , workingDir : OutputDir ) ;
382+ Utils . RunProcess ( logger , dx , "--dex --output=classes.dex obj" , workingDir : OutputDir ) ;
375383
376384 // 3. Generate APK
377385
378386 string debugModeArg = StripDebugSymbols ? string . Empty : "--debug-mode" ;
379387 string apkFile = Path . Combine ( OutputDir , "bin" , $ "{ ProjectName } .unaligned.apk") ;
380- Utils . RunProcess ( aapt , $ "package -f -m -F { apkFile } -A assets -M AndroidManifest.xml -I { androidJar } { debugModeArg } ", workingDir : OutputDir ) ;
388+ Utils . RunProcess ( logger , aapt , $ "package -f -m -F { apkFile } -A assets -M AndroidManifest.xml -I { androidJar } { debugModeArg } ", workingDir : OutputDir ) ;
381389
382390 var dynamicLibs = new List < string > ( ) ;
383391 dynamicLibs . Add ( Path . Combine ( OutputDir , "monodroid" , "libmonodroid.so" ) ) ;
@@ -433,21 +441,21 @@ public class ApkBuilder
433441 // NOTE: we can run android-strip tool from NDK to shrink native binaries here even more.
434442
435443 File . Copy ( dynamicLib , Path . Combine ( OutputDir , destRelative ) , true ) ;
436- Utils . RunProcess ( aapt , $ "add { apkFile } { destRelative } ", workingDir : OutputDir ) ;
444+ Utils . RunProcess ( logger , aapt , $ "add { apkFile } { destRelative } ", workingDir : OutputDir ) ;
437445 }
438- Utils . RunProcess ( aapt , $ "add { apkFile } classes.dex", workingDir : OutputDir ) ;
446+ Utils . RunProcess ( logger , aapt , $ "add { apkFile } classes.dex", workingDir : OutputDir ) ;
439447
440448 // 4. Align APK
441449
442450 string alignedApk = Path . Combine ( OutputDir , "bin" , $ "{ ProjectName } .apk") ;
443- Utils . RunProcess ( zipalign , $ "-v 4 { apkFile } { alignedApk } ", workingDir : OutputDir ) ;
451+ Utils . RunProcess ( logger , zipalign , $ "-v 4 { apkFile } { alignedApk } ", workingDir : OutputDir ) ;
444452 // we don't need the unaligned one any more
445453 File . Delete ( apkFile ) ;
446454
447455 // 5. Generate key (if needed) & sign the apk
448456 SignApk ( alignedApk , apksigner ) ;
449457
450- Utils . LogInfo ( $ "\n APK size: { ( new FileInfo ( alignedApk ) . Length / 1000_000.0 ) : 0.#} Mb.\n ") ;
458+ logger . LogMessage ( MessageImportance . High , $ "\n APK size: { ( new FileInfo ( alignedApk ) . Length / 1000_000.0 ) : 0.#} Mb.\n ") ;
451459
452460 return ( alignedApk , packageId ) ;
453461 }
@@ -460,15 +468,15 @@ private void SignApk(string apkPath, string apksigner)
460468
461469 if ( ! File . Exists ( signingKey ) )
462470 {
463- Utils . RunProcess ( "keytool" , "-genkey -v -keystore debug.keystore -storepass android -alias " +
471+ Utils . RunProcess ( logger , "keytool" , "-genkey -v -keystore debug.keystore -storepass android -alias " +
464472 "androiddebugkey -keypass android -keyalg RSA -keysize 2048 -noprompt " +
465473 "-dname \" CN=Android Debug,O=Android,C=US\" " , workingDir : OutputDir , silent : true ) ;
466474 }
467475 else if ( Path . GetFullPath ( signingKey ) != Path . GetFullPath ( defaultKey ) )
468476 {
469477 File . Copy ( signingKey , Path . Combine ( OutputDir , "debug.keystore" ) ) ;
470478 }
471- Utils . RunProcess ( apksigner , $ "sign --min-sdk-version { MinApiLevel } --ks debug.keystore " +
479+ Utils . RunProcess ( logger , apksigner , $ "sign --min-sdk-version { MinApiLevel } --ks debug.keystore " +
472480 $ "--ks-pass pass:android --key-pass pass:android { apkPath } ", workingDir : OutputDir ) ;
473481 }
474482
@@ -499,8 +507,8 @@ public void ReplaceFileInApk(string file)
499507 if ( ! File . Exists ( apkPath ) )
500508 throw new Exception ( $ "{ apkPath } was not found") ;
501509
502- Utils . RunProcess ( aapt , $ "remove -v bin/{ Path . GetFileName ( apkPath ) } { file } ", workingDir : OutputDir ) ;
503- Utils . RunProcess ( aapt , $ "add -v bin/{ Path . GetFileName ( apkPath ) } { file } ", workingDir : OutputDir ) ;
510+ Utils . RunProcess ( logger , aapt , $ "remove -v bin/{ Path . GetFileName ( apkPath ) } { file } ", workingDir : OutputDir ) ;
511+ Utils . RunProcess ( logger , aapt , $ "add -v bin/{ Path . GetFileName ( apkPath ) } { file } ", workingDir : OutputDir ) ;
504512
505513 // we need to re-sign the apk
506514 SignApk ( apkPath , apksigner ) ;
0 commit comments