@@ -13,6 +13,7 @@ public class AndroidBuilder : MonoBehaviour {
1313 public static readonly string PROJECT_DIR = Application . dataPath . Substring ( 0 , Application . dataPath . Length - 6 ) ;
1414 public static readonly string ANDROID_EXPORT_PATH = PROJECT_DIR + "/AndroidGradleProject_v1.0" ;
1515 public static string ANDROID_PROJECT_PATH { get { return ANDROID_EXPORT_PATH ; } }
16+ public static string ANDROID_UNITYLIBRARY_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/" ;
1617 public static string ANDROID_MANIFEST_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/" ;
1718 public static string JAVA_SRC_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/java/" ;
1819 public static string JAR_LIB_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/libs/" ;
@@ -28,6 +29,14 @@ public class AndroidBuilder : MonoBehaviour {
2829 public static string BUILD_SCRIPTS_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/" ;
2930 public static string ZIP_PATH = PROJECT_DIR + "/Assets/AndroidIl2cppPatchDemo/Editor/Exe/zip.exe" ;
3031
32+ public static string UNITY_EXE_PATH = System . Diagnostics . Process . GetCurrentProcess ( ) . MainModule . FileName ;
33+ public static string UNITY_EXE_DIR = Path . GetDirectoryName ( UNITY_EXE_PATH ) ;
34+ public static string DEFAULT_SDK_PATH = UNITY_EXE_DIR + "/Data/PlaybackEngines/AndroidPlayer/SDK/" ;
35+ public static string DEFAULT_JDK_PATH = UNITY_EXE_DIR + "/Data/PlaybackEngines/AndroidPlayer/OpenJDK/" ;
36+ public static string DEFAULT_NDK_PATH = UNITY_EXE_DIR + "/Data/PlaybackEngines/AndroidPlayer/NDK/" ;
37+ public static string GRADLE_PROXY_STRING = " \" -Dhttp.proxyHost=127.0.0.1\" \" -Dhttp.proxyPort=1080\" \" -Dhttps.proxyHost=127.0.0.1\" \" -Dhttps.proxyPort=1080\" " ;
38+ //public static string GRADLE_PROXY_STRING = "";
39+
3140 static bool Exec ( string filename , string args )
3241 {
3342 System . Diagnostics . Process process = new System . Diagnostics . Process ( ) ;
@@ -69,34 +78,35 @@ static bool Exec(string filename, string args)
6978 public static bool ValidateConfig ( )
7079 {
7180 string sdkPath = EditorPrefs . GetString ( "AndroidSdkRoot" , "" ) ;
72- if ( string . IsNullOrEmpty ( sdkPath ) )
81+ if ( string . IsNullOrEmpty ( sdkPath ) ) { sdkPath = DEFAULT_SDK_PATH ; }
82+ if ( string . IsNullOrEmpty ( sdkPath ) || ! Directory . Exists ( sdkPath ) )
7383 {
7484 Debug . LogError ( "sdk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
7585 return false ;
7686 }
7787
7888 string jdkPath = EditorPrefs . GetString ( "JdkPath" , "" ) ;
79- if ( string . IsNullOrEmpty ( jdkPath ) )
89+ if ( string . IsNullOrEmpty ( jdkPath ) ) { jdkPath = DEFAULT_JDK_PATH ; }
90+ if ( string . IsNullOrEmpty ( jdkPath ) || ! Directory . Exists ( jdkPath ) )
8091 {
8192 Debug . LogError ( "jdk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
8293 return false ;
8394 }
8495
8596 string ndkPath = EditorPrefs . GetString ( "AndroidNdkRootR16b" , "" ) ;
86- if ( string . IsNullOrEmpty ( ndkPath ) )
97+ if ( string . IsNullOrEmpty ( ndkPath ) ) { ndkPath = EditorPrefs . GetString ( "AndroidNdkRoot" , "" ) ; }
98+ if ( string . IsNullOrEmpty ( ndkPath ) ) { ndkPath = DEFAULT_NDK_PATH ; }
99+ if ( string . IsNullOrEmpty ( ndkPath ) || ! Directory . Exists ( ndkPath ) )
87100 {
88- ndkPath = EditorPrefs . GetString ( "AndroidNdkRoot" , "" ) ;
89- if ( string . IsNullOrEmpty ( ndkPath ) )
90- {
91- Debug . LogError ( "ndk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
92- return false ;
93- }
101+ Debug . LogError ( "ndk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
102+ return false ;
94103 }
95104
96105 Debug . Log ( "Build Env is ready!" ) ;
97106 Debug . Log ( "Build Options:" ) ;
98107 Debug . Log ( "SDK PATH=" + sdkPath ) ;
99108 Debug . Log ( "JDK PATH=" + jdkPath ) ;
109+ Debug . Log ( "NDK PATH=" + ndkPath ) ;
100110 return true ;
101111 }
102112
@@ -117,7 +127,7 @@ public static bool ExportGradleProject()
117127 //export project
118128 string error_msg = string . Empty ;
119129 string [ ] levels = new string [ ] { "Assets/AndroidIl2cppPatchDemo/Scene/0.unity" } ;
120- BuildOptions options = BuildOptions . AcceptExternalModificationsToPlayer ;
130+ BuildOptions options = BuildOptions . None ;
121131 if ( Directory . Exists ( ANDROID_EXPORT_PATH ) ) { FileUtil . DeleteFileOrDirectory ( ANDROID_EXPORT_PATH ) ; }
122132 Directory . CreateDirectory ( ANDROID_EXPORT_PATH ) ;
123133 try
@@ -142,7 +152,55 @@ public static bool ExportGradleProject()
142152 return true ;
143153 }
144154
145- [ MenuItem ( "AndroidBuilder/Step 2: Patch Gradle Project" , false , 102 ) ]
155+ [ MenuItem ( "AndroidBuilder/Step 2: Build Il2cpp So" , false , 102 ) ]
156+ public static bool BuildIl2cppSoLib ( )
157+ {
158+ string jdkPath = EditorPrefs . GetString ( "JdkPath" , "" ) ;
159+ if ( string . IsNullOrEmpty ( jdkPath ) ) { jdkPath = DEFAULT_JDK_PATH ; }
160+ if ( string . IsNullOrEmpty ( jdkPath ) || ! Directory . Exists ( jdkPath ) )
161+ {
162+ Debug . LogError ( "jdk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
163+ return false ;
164+ }
165+
166+ //must use the jdk in Unity
167+ string gradlePath = jdkPath + "/../Tools/Gradle" ;
168+ string [ ] gradleMainJarFiles = Directory . GetFiles ( gradlePath + "/lib" , "gradle-launcher*.jar" , SearchOption . TopDirectoryOnly ) ;
169+ if ( gradleMainJarFiles . Length == 0 )
170+ {
171+ Debug . LogError ( "gradle-launcher jar file not found in " + gradlePath + "/lib" ) ;
172+ return false ;
173+ }
174+ string gradleMainJarFile = gradleMainJarFiles [ 0 ] ;
175+
176+ StringBuilder allCmd = new StringBuilder ( ) ;
177+ allCmd . AppendFormat ( "cd \" {0}\" \n \n " , ANDROID_UNITYLIBRARY_PATH ) ;
178+ allCmd . AppendFormat ( "call \" {0}\" "
179+ + " -classpath \" {1}\" org.gradle.launcher.GradleMain \" -Dorg.gradle.jvmargs=-Xmx4096m\" \" BuildIl2CppTask\" "
180+ + GRADLE_PROXY_STRING
181+ + " \n \n " ,
182+ jdkPath + "/bin/java.exe" ,
183+ gradleMainJarFile ) ;
184+
185+ string buildFile = ANDROID_UNITYLIBRARY_PATH + "/build_il2cpp.bat" ;
186+ File . WriteAllText ( ANDROID_UNITYLIBRARY_PATH + "/build_il2cpp.bat" , allCmd . ToString ( ) ) ;
187+
188+ if ( ! Exec ( buildFile , "" ) )
189+ {
190+ Debug . LogError ( "exec failed:" + buildFile ) ;
191+ return false ;
192+ }
193+
194+ string testSoPath = SO_LIB_PATH + "/arm64-v8a/libil2cpp.so" ;
195+ if ( ! File . Exists ( testSoPath ) )
196+ {
197+ Debug . LogError ( "libil2cpp.so not found:" + testSoPath + ", exec failed:" + buildFile ) ;
198+ return false ;
199+ }
200+ return true ;
201+ }
202+
203+ [ MenuItem ( "AndroidBuilder/Step 3: Patch Gradle Project" , false , 102 ) ]
146204 public static bool PatchAndroidProject ( )
147205 {
148206 //1. patch java file
@@ -180,7 +238,7 @@ public static bool PatchAndroidProject()
180238 }
181239
182240
183- [ MenuItem ( "AndroidBuilder/Step 3 : Generate Bin Patches" , false , 103 ) ]
241+ [ MenuItem ( "AndroidBuilder/Step 4 : Generate Bin Patches" , false , 103 ) ]
184242 public static bool GenerateBinPatches ( )
185243 {
186244 string assetBinDataPath = EXPORTED_ASSETS_PATH + "/bin/Data/" ;
@@ -239,11 +297,12 @@ public static bool GenerateBinPatches()
239297 return true ;
240298 }
241299
242- [ MenuItem ( "AndroidBuilder/Step 4 : Generate Build Scripts" , false , 104 ) ]
300+ [ MenuItem ( "AndroidBuilder/Step 5 : Generate Build Scripts" , false , 104 ) ]
243301 public static bool GenerateBuildScripts ( )
244302 {
245- string jdkPath = EditorPrefs . GetString ( "JdkPath" , "" ) ; ;
246- if ( string . IsNullOrEmpty ( jdkPath ) )
303+ string jdkPath = EditorPrefs . GetString ( "JdkPath" , "" ) ;
304+ if ( string . IsNullOrEmpty ( jdkPath ) ) { jdkPath = DEFAULT_JDK_PATH ; }
305+ if ( string . IsNullOrEmpty ( jdkPath ) || ! Directory . Exists ( jdkPath ) )
247306 {
248307 Debug . LogError ( "jdk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
249308 return false ;
@@ -278,6 +337,7 @@ public static bool GenerateBuildScripts()
278337 allCmd . AppendFormat ( "cd \" {0}\" \n \n " , ANDROID_EXPORT_PATH ) ;
279338 allCmd . AppendFormat ( "call \" {0}\" "
280339 + " -classpath \" {1}\" org.gradle.launcher.GradleMain \" -Dorg.gradle.jvmargs=-Xmx4096m\" \" assembleRelease\" "
340+ + GRADLE_PROXY_STRING
281341 + " -Pandroid.injected.signing.store.file=\" {2}\" "
282342 + " -Pandroid.injected.signing.store.password=testtest "
283343 + " -Pandroid.injected.signing.key.alias=test "
@@ -299,7 +359,7 @@ public static bool GenerateBuildScripts()
299359 }
300360
301361
302- [ MenuItem ( "AndroidBuilder/Step 5 : Build Apk File" , false , 105 ) ]
362+ [ MenuItem ( "AndroidBuilder/Step 6 : Build Apk File" , false , 105 ) ]
303363 public static bool BuildApk ( )
304364 {
305365 string buildApkPath = ANDROID_EXPORT_PATH + "/build_apk.bat" ;
@@ -320,38 +380,39 @@ public static bool BuildApk()
320380 return true ;
321381 }
322382
323- [ MenuItem ( "AndroidBuilder/Run Step 1-5 " , false , 1 ) ]
383+ [ MenuItem ( "AndroidBuilder/Run Step 1-6 " , false , 1 ) ]
324384 public static void BuildAll ( )
325385 {
326- //Step 1
327386 if ( ! ExportGradleProject ( ) )
328387 {
329388 Debug . LogError ( "failed to ExportGradleProject" ) ;
330389 return ;
331390 }
332391
333- //Step 2
392+ if ( ! BuildIl2cppSoLib ( ) )
393+ {
394+ Debug . LogError ( "failed to BuildIl2cppSoLig" ) ;
395+ return ;
396+ }
397+
334398 if ( ! PatchAndroidProject ( ) )
335399 {
336400 Debug . LogError ( "failed to PatchAndroidProject" ) ;
337401 return ;
338402 }
339403
340- //Step 3
341404 if ( ! GenerateBinPatches ( ) )
342405 {
343406 Debug . LogError ( "failed to GenerateBinPatches" ) ;
344407 return ;
345408 }
346409
347- //Step 4
348410 if ( ! GenerateBuildScripts ( ) )
349411 {
350412 Debug . LogError ( "failed to GenerateBuildScripts" ) ;
351413 return ;
352414 }
353415
354- //Step 5
355416 if ( ! BuildApk ( ) )
356417 {
357418 Debug . LogError ( "failed to BuildApk" ) ;
@@ -360,31 +421,33 @@ public static void BuildAll()
360421 Debug . Log ( "Done!" ) ;
361422 }
362423
363- [ MenuItem ( "AndroidBuilder/Run Step 1, 2, 4 , 5 for base version" , false , 2 ) ]
424+ [ MenuItem ( "AndroidBuilder/Run Step 1, 2, 3 , 5, 6 for base version" , false , 2 ) ]
364425 public static void BuildWithoutPatch ( )
365426 {
366- //Step 1
367427 if ( ! ExportGradleProject ( ) )
368428 {
369429 Debug . LogError ( "failed to ExportGradleProject" ) ;
370430 return ;
371431 }
372432
373- //Step 2
374433 if ( ! PatchAndroidProject ( ) )
375434 {
376435 Debug . LogError ( "failed to PatchAndroidProject" ) ;
377436 return ;
378437 }
379438
380- //Step 4
439+ if ( ! PatchAndroidProject ( ) )
440+ {
441+ Debug . LogError ( "failed to PatchAndroidProject" ) ;
442+ return ;
443+ }
444+
381445 if ( ! GenerateBuildScripts ( ) )
382446 {
383447 Debug . LogError ( "failed to GenerateBuildScripts" ) ;
384448 return ;
385449 }
386450
387- //Step 5
388451 if ( ! BuildApk ( ) )
389452 {
390453 Debug . LogError ( "failed to BuildApk" ) ;
@@ -393,7 +456,7 @@ public static void BuildWithoutPatch()
393456 Debug . Log ( "Done!" ) ;
394457 }
395458
396- [ MenuItem ( "AndroidBuilder/Run Step 1-4 for Patch Version" , false , 3 ) ]
459+ [ MenuItem ( "AndroidBuilder/Run Step 1-5 for Patch Version" , false , 3 ) ]
397460 public static void BuildPatch ( )
398461 {
399462 //Step 1
@@ -403,21 +466,18 @@ public static void BuildPatch()
403466 return ;
404467 }
405468
406- //Step 2
407469 if ( ! PatchAndroidProject ( ) )
408470 {
409471 Debug . LogError ( "failed to PatchAndroidProject" ) ;
410472 return ;
411473 }
412474
413- //Step 3
414475 if ( ! GenerateBinPatches ( ) )
415476 {
416477 Debug . LogError ( "failed to GenerateBinPatches" ) ;
417478 return ;
418479 }
419480
420- //Step 4
421481 if ( ! GenerateBuildScripts ( ) )
422482 {
423483 Debug . LogError ( "failed to GenerateBuildScripts" ) ;
0 commit comments