2323using UnityEditor ;
2424using UnityEditor . Build ;
2525using UnityEditor . Build . Reporting ;
26+ using UnityEditor . Android ;
2627using UnityEngine ;
2728using System . IO ;
2829using System . Linq ;
2930using PlayEveryWare . EpicOnlineServices ;
3031using System . Collections . Generic ;
32+ using System . Text . RegularExpressions ;
3133
34+ #if UNITY_ANDROID
3235public class EOSOnPreprocessBuild_android : IPreprocessBuildWithReport
3336{
3437 public int callbackOrder { get { return 3 ; } }
@@ -40,19 +43,13 @@ private static string GetAndroidEOSValuesConfigPath()
4043 return Path . Combine ( assetsPathname , "eos_dependencies.androidlib/res/values/eos_values.xml" ) ;
4144 }
4245
43- //-------------------------------------------------------------------------
44- private static string GetPackageName ( )
45- {
46- return "com.playeveryware.eos" ;
47- }
48-
49-
5046 //-------------------------------------------------------------------------
5147 public void OnPreprocessBuild ( BuildReport report )
5248 {
5349 if ( report . summary . platform == BuildTarget . Android )
5450 {
5551 InstallEOSDependentLibrary ( ) ;
52+ ConfigureGradleTemplateProperties ( ) ;
5653 ConfigureEOSDependentLibrary ( ) ;
5754 }
5855 }
@@ -101,8 +98,8 @@ static private void InstallFiles(string[] filenames, string pathToInstallFrom,
10198 //-------------------------------------------------------------------------
10299 private string GetPlatformSpecificAssetsPath ( string subpath )
103100 {
104- string packagePathname = Path . GetFullPath ( "Packages/" + GetPackageName ( ) + "/ PlatformSpecificAssets~/" + subpath ) ;
105- string streamingAssetsSamplesPathname = Path . Combine ( Application . dataPath , "../PlatformSpecificAssets/" + subpath ) ;
101+ string packagePathname = Path . GetFullPath ( Path . Combine ( "Packages" , EOSPackageInfo . GetPackageName ( ) , " PlatformSpecificAssets~" , subpath ) ) ;
102+ string streamingAssetsSamplesPathname = Path . Combine ( Application . dataPath , ".." , "PlatformSpecificAssets" , subpath ) ;
106103 string pathToInstallFrom = "" ;
107104
108105 if ( Directory . Exists ( packagePathname ) )
@@ -114,6 +111,10 @@ private string GetPlatformSpecificAssetsPath(string subpath)
114111 {
115112 pathToInstallFrom = streamingAssetsSamplesPathname ;
116113 }
114+ else
115+ {
116+ Debug . LogError ( "PreprocessBuildError : EOS Plugin Package Missing" ) ;
117+ }
117118 return pathToInstallFrom ;
118119 }
119120
@@ -131,6 +132,23 @@ bool DoesGradlePropertiesContainSetting(string gradleTemplatePathname, string se
131132 return false ;
132133 }
133134
135+ //-------------------------------------------------------------------------
136+ void DisableGradleProperty ( string gradleTemplatePathname , string setting )
137+ {
138+ var gradleTemplateToWrite = new List < string > ( ) ;
139+
140+ foreach ( string line in File . ReadAllLines ( gradleTemplatePathname ) )
141+ {
142+ if ( line . Contains ( setting ) && ! line . StartsWith ( "#" ) )
143+ {
144+ }
145+ else
146+ {
147+ gradleTemplateToWrite . Add ( line ) ;
148+ }
149+ }
150+ File . WriteAllLines ( gradleTemplatePathname , gradleTemplateToWrite . ToArray ( ) ) ;
151+ }
134152 //-------------------------------------------------------------------------
135153 void ReplaceOrSetGradleProperty ( string gradleTemplatePathname , string setting , string value )
136154 {
@@ -157,47 +175,152 @@ void ReplaceOrSetGradleProperty(string gradleTemplatePathname, string setting, s
157175 File . WriteAllLines ( gradleTemplatePathname , gradleTemplateToWrite . ToArray ( ) ) ;
158176 }
159177
178+ //-------------------------------------------------------------------------
179+ int GetTargetAPI ( )
180+ {
181+ var playerApiTarget = PlayerSettings . Android . targetSdkVersion ;
182+ if ( playerApiTarget == AndroidSdkVersions . AndroidApiLevelAuto )
183+ {
184+ int maxVersion = 0 ;
185+ var apiRegex = new Regex ( @"android-(\d+)" ) ;
186+ //find max installed android api
187+ foreach ( var dir in Directory . GetDirectories ( Path . Combine ( AndroidExternalToolsSettings . sdkRootPath , "platforms" ) ) )
188+ {
189+ var dirName = Path . GetFileName ( dir ) ;
190+ var match = apiRegex . Match ( dirName ) ;
191+ if ( match . Success && match . Groups . Count == 2 )
192+ {
193+ if ( int . TryParse ( match . Groups [ 1 ] . Value , out int matchVal ) )
194+ {
195+ if ( matchVal > maxVersion )
196+ {
197+ maxVersion = matchVal ;
198+ }
199+ }
200+ }
201+ }
202+ if ( maxVersion == 0 )
203+ {
204+ return 29 ;
205+ }
206+ return maxVersion ;
207+ }
208+ else
209+ {
210+ return ( int ) playerApiTarget ;
211+ }
212+ }
213+
214+ //-------------------------------------------------------------------------
215+ string GetBuildTools ( )
216+ {
217+ var toolsRegex = new Regex ( @"(\d+)\.(\d+)\.(\d+)" ) ;
218+ int maxMajor = 0 , maxMinor = 0 , maxPatch = 0 ;
219+ //find highest usable build tools version
220+ #if UNITY_2022_2_OR_NEWER
221+ const int highestVersion = 32 ;
222+ #else
223+ const int highestVersion = 30 ;
224+ #endif
225+ foreach ( var dir in Directory . GetDirectories ( Path . Combine ( AndroidExternalToolsSettings . sdkRootPath , "build-tools" ) ) )
226+ {
227+ var dirName = Path . GetFileName ( dir ) ;
228+ var match = toolsRegex . Match ( dirName ) ;
229+ int majorVersion = 0 , minorVersion = 0 , patchVersion = 0 ;
230+ bool success = match . Success && match . Groups . Count == 4 &&
231+ int . TryParse ( match . Groups [ 1 ] . Value , out majorVersion ) &&
232+ int . TryParse ( match . Groups [ 2 ] . Value , out minorVersion ) &&
233+ int . TryParse ( match . Groups [ 3 ] . Value , out patchVersion ) ;
234+ if ( success )
235+ {
236+ if ( majorVersion > highestVersion )
237+ {
238+ continue ;
239+ }
240+ if ( majorVersion > maxMajor ||
241+ ( majorVersion == maxMajor && minorVersion > maxMinor ) ||
242+ ( majorVersion == maxMajor && minorVersion == maxMinor && patchVersion > maxPatch ) )
243+ {
244+ maxMajor = majorVersion ;
245+ maxMinor = minorVersion ;
246+ maxPatch = patchVersion ;
247+ }
248+ }
249+ }
250+ if ( maxMajor == 0 )
251+ {
252+ return "30.0.3" ;
253+ }
254+ else
255+ {
256+ return $ "{ maxMajor } .{ maxMinor } .{ maxPatch } ";
257+ }
258+ }
259+ //-------------------------------------------------------------------------
260+ void WriteConfigMacros ( string filepath )
261+ {
262+ var contents = File . ReadAllText ( filepath ) ;
263+ string apiVersion = GetTargetAPI ( ) . ToString ( ) ;
264+ string buildTools = GetBuildTools ( ) ;
265+ string newContents = contents . Replace ( "**APIVERSION**" , apiVersion ) . Replace ( "**TARGETSDKVERSION**" , apiVersion ) . Replace ( "**BUILDTOOLS**" , buildTools ) ;
266+ File . WriteAllText ( filepath , newContents ) ;
267+ }
268+
160269 //-------------------------------------------------------------------------
161270 public void InstallEOSDependentLibrary ( )
162271 {
163272 string packagedPathname = GetPlatformSpecificAssetsPath ( "EOS/Android/" ) ;
164273
165274 if ( Directory . Exists ( packagedPathname ) )
166275 {
167- string assetsPathname = Path . Combine ( Application . dataPath , "Plugins/Android/EOS/" ) ;
276+ string assetsPathname = Path . Combine ( Application . dataPath , "Plugins" , "Android" , "EOS" ) ;
277+ string buildGradlePath = "eos_dependencies.androidlib/build.gradle" ;
168278 string [ ] filenames =
169279 {
170280 "eos_dependencies.androidlib/AndroidManifest.xml" ,
171- "eos_dependencies.androidlib/build.gradle" ,
281+ buildGradlePath ,
172282 "eos_dependencies.androidlib/project.properties" ,
173283 "eos_dependencies.androidlib/res/values/eos_values.xml" ,
174284 "eos_dependencies.androidlib/res/values/styles.xml"
175285
176286 } ;
177287 InstallFiles ( filenames , packagedPathname , assetsPathname ) ;
178288
179- // Unity has a fixed location for the gradleTemplate.properties file. (as of 2021)
180- string gradleTemplatePathname = Path . Combine ( Application . dataPath , "Plugins/Android/gradleTemplate.properties" ) ;
181- if ( File . Exists ( gradleTemplatePathname ) )
182- {
183- if ( ! DoesGradlePropertiesContainSetting ( gradleTemplatePathname , "android.useAndroidX=true" ) )
184- {
185- ReplaceOrSetGradleProperty ( gradleTemplatePathname , "android.useAndroidX" , "true" ) ;
186- }
187- }
188- else
289+ WriteConfigMacros ( Path . Combine ( assetsPathname , buildGradlePath ) ) ;
290+ }
291+ }
292+ //-------------------------------------------------------------------------
293+ public void ConfigureGradleTemplateProperties ( )
294+ {
295+ // Unity has a fixed location for the gradleTemplate.properties file. (as of 2021)
296+ string gradleTemplatePathname = Path . Combine ( Application . dataPath , "Plugins" , "Android" , "gradleTemplate.properties" ) ;
297+
298+ // If the custom gradle template properties option is disabled, delete gradleTemplate.properties.DISABLED
299+ File . Delete ( gradleTemplatePathname + ".DISABLED" ) ;
300+
301+ // Then create a copy of gradleTemplate.properties in the target folder
302+ // Once gradleTemplate.properties file exists, the custom gradle template properties option is automatically enabled
303+ if ( File . Exists ( gradleTemplatePathname ) )
304+ {
305+ if ( ! DoesGradlePropertiesContainSetting ( gradleTemplatePathname , "android.useAndroidX=true" ) )
189306 {
190- // Use one we have bundled
191- string bundledGradleTemplatePathname = GetPlatformSpecificAssetsPath ( "EOS/Android/gradleTemplate.properties" ) ;
192- File . Copy ( bundledGradleTemplatePathname , gradleTemplatePathname ) ;
307+ ReplaceOrSetGradleProperty ( gradleTemplatePathname , "android.useAndroidX" , "true" ) ;
193308 }
194309 }
310+ else
311+ {
312+ // Use one we have bundled
313+ string bundledGradleTemplatePathname = Path . Combine ( GetPlatformSpecificAssetsPath ( "EOS/Android/" ) , "gradleTemplate.properties" ) ;
314+ File . Copy ( bundledGradleTemplatePathname , gradleTemplatePathname ) ;
315+ }
316+ #if UNITY_2022_2_OR_NEWER
317+ DisableGradleProperty ( gradleTemplatePathname , "android.enableR8" ) ;
318+ #endif
195319 }
196-
197320 //-------------------------------------------------------------------------
198321 public void ConfigureEOSDependentLibrary ( )
199322 {
200- string configFilePath = Path . Combine ( Application . streamingAssetsPath , "EOS" , EOSManager . ConfigFileName ) ;
323+ string configFilePath = Path . Combine ( Application . streamingAssetsPath , "EOS" , EOSPackageInfo . ConfigFileName ) ;
201324 var eosConfigFile = new EOSConfigFile < EOSConfig > ( configFilePath ) ;
202325 eosConfigFile . LoadConfigFromDisk ( ) ;
203326 string clientIDAsLower = eosConfigFile . currentEOSConfig . clientID . ToLower ( ) ;
@@ -211,7 +334,7 @@ public void ConfigureEOSDependentLibrary()
211334 if ( node != null )
212335 {
213336 string eosProtocolScheme = node . InnerText ;
214- string storedClientID = eosProtocolScheme . Split ( "." ) . Last ( ) ;
337+ string storedClientID = eosProtocolScheme . Split ( '.' ) . Last ( ) ;
215338
216339 if ( storedClientID != clientIDAsLower )
217340 {
@@ -221,4 +344,5 @@ public void ConfigureEOSDependentLibrary()
221344 }
222345 }
223346
224- }
347+ }
348+ #endif
0 commit comments