@@ -13,6 +13,7 @@ public class AndroidBuilder : MonoBehaviour {
13
13
public static readonly string PROJECT_DIR = Application . dataPath . Substring ( 0 , Application . dataPath . Length - 6 ) ;
14
14
public static readonly string ANDROID_EXPORT_PATH = PROJECT_DIR + "/AndroidGradleProject_v1.0" ;
15
15
public static string ANDROID_PROJECT_PATH { get { return ANDROID_EXPORT_PATH ; } }
16
+ public static string ANDROID_UNITYLIBRARY_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/" ;
16
17
public static string ANDROID_MANIFEST_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/" ;
17
18
public static string JAVA_SRC_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/java/" ;
18
19
public static string JAR_LIB_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/libs/" ;
@@ -28,6 +29,14 @@ public class AndroidBuilder : MonoBehaviour {
28
29
public static string BUILD_SCRIPTS_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/" ;
29
30
public static string ZIP_PATH = PROJECT_DIR + "/Assets/AndroidIl2cppPatchDemo/Editor/Exe/zip.exe" ;
30
31
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
+
31
40
static bool Exec ( string filename , string args )
32
41
{
33
42
System . Diagnostics . Process process = new System . Diagnostics . Process ( ) ;
@@ -69,34 +78,35 @@ static bool Exec(string filename, string args)
69
78
public static bool ValidateConfig ( )
70
79
{
71
80
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 ) )
73
83
{
74
84
Debug . LogError ( "sdk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
75
85
return false ;
76
86
}
77
87
78
88
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 ) )
80
91
{
81
92
Debug . LogError ( "jdk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
82
93
return false ;
83
94
}
84
95
85
96
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 ) )
87
100
{
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 ;
94
103
}
95
104
96
105
Debug . Log ( "Build Env is ready!" ) ;
97
106
Debug . Log ( "Build Options:" ) ;
98
107
Debug . Log ( "SDK PATH=" + sdkPath ) ;
99
108
Debug . Log ( "JDK PATH=" + jdkPath ) ;
109
+ Debug . Log ( "NDK PATH=" + ndkPath ) ;
100
110
return true ;
101
111
}
102
112
@@ -117,7 +127,7 @@ public static bool ExportGradleProject()
117
127
//export project
118
128
string error_msg = string . Empty ;
119
129
string [ ] levels = new string [ ] { "Assets/AndroidIl2cppPatchDemo/Scene/0.unity" } ;
120
- BuildOptions options = BuildOptions . AcceptExternalModificationsToPlayer ;
130
+ BuildOptions options = BuildOptions . None ;
121
131
if ( Directory . Exists ( ANDROID_EXPORT_PATH ) ) { FileUtil . DeleteFileOrDirectory ( ANDROID_EXPORT_PATH ) ; }
122
132
Directory . CreateDirectory ( ANDROID_EXPORT_PATH ) ;
123
133
try
@@ -142,7 +152,55 @@ public static bool ExportGradleProject()
142
152
return true ;
143
153
}
144
154
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 ) ]
146
204
public static bool PatchAndroidProject ( )
147
205
{
148
206
//1. patch java file
@@ -180,7 +238,7 @@ public static bool PatchAndroidProject()
180
238
}
181
239
182
240
183
- [ MenuItem ( "AndroidBuilder/Step 3 : Generate Bin Patches" , false , 103 ) ]
241
+ [ MenuItem ( "AndroidBuilder/Step 4 : Generate Bin Patches" , false , 103 ) ]
184
242
public static bool GenerateBinPatches ( )
185
243
{
186
244
string assetBinDataPath = EXPORTED_ASSETS_PATH + "/bin/Data/" ;
@@ -239,11 +297,12 @@ public static bool GenerateBinPatches()
239
297
return true ;
240
298
}
241
299
242
- [ MenuItem ( "AndroidBuilder/Step 4 : Generate Build Scripts" , false , 104 ) ]
300
+ [ MenuItem ( "AndroidBuilder/Step 5 : Generate Build Scripts" , false , 104 ) ]
243
301
public static bool GenerateBuildScripts ( )
244
302
{
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 ) )
247
306
{
248
307
Debug . LogError ( "jdk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
249
308
return false ;
@@ -278,6 +337,7 @@ public static bool GenerateBuildScripts()
278
337
allCmd . AppendFormat ( "cd \" {0}\" \n \n " , ANDROID_EXPORT_PATH ) ;
279
338
allCmd . AppendFormat ( "call \" {0}\" "
280
339
+ " -classpath \" {1}\" org.gradle.launcher.GradleMain \" -Dorg.gradle.jvmargs=-Xmx4096m\" \" assembleRelease\" "
340
+ + GRADLE_PROXY_STRING
281
341
+ " -Pandroid.injected.signing.store.file=\" {2}\" "
282
342
+ " -Pandroid.injected.signing.store.password=testtest "
283
343
+ " -Pandroid.injected.signing.key.alias=test "
@@ -299,7 +359,7 @@ public static bool GenerateBuildScripts()
299
359
}
300
360
301
361
302
- [ MenuItem ( "AndroidBuilder/Step 5 : Build Apk File" , false , 105 ) ]
362
+ [ MenuItem ( "AndroidBuilder/Step 6 : Build Apk File" , false , 105 ) ]
303
363
public static bool BuildApk ( )
304
364
{
305
365
string buildApkPath = ANDROID_EXPORT_PATH + "/build_apk.bat" ;
@@ -320,38 +380,39 @@ public static bool BuildApk()
320
380
return true ;
321
381
}
322
382
323
- [ MenuItem ( "AndroidBuilder/Run Step 1-5 " , false , 1 ) ]
383
+ [ MenuItem ( "AndroidBuilder/Run Step 1-6 " , false , 1 ) ]
324
384
public static void BuildAll ( )
325
385
{
326
- //Step 1
327
386
if ( ! ExportGradleProject ( ) )
328
387
{
329
388
Debug . LogError ( "failed to ExportGradleProject" ) ;
330
389
return ;
331
390
}
332
391
333
- //Step 2
392
+ if ( ! BuildIl2cppSoLib ( ) )
393
+ {
394
+ Debug . LogError ( "failed to BuildIl2cppSoLig" ) ;
395
+ return ;
396
+ }
397
+
334
398
if ( ! PatchAndroidProject ( ) )
335
399
{
336
400
Debug . LogError ( "failed to PatchAndroidProject" ) ;
337
401
return ;
338
402
}
339
403
340
- //Step 3
341
404
if ( ! GenerateBinPatches ( ) )
342
405
{
343
406
Debug . LogError ( "failed to GenerateBinPatches" ) ;
344
407
return ;
345
408
}
346
409
347
- //Step 4
348
410
if ( ! GenerateBuildScripts ( ) )
349
411
{
350
412
Debug . LogError ( "failed to GenerateBuildScripts" ) ;
351
413
return ;
352
414
}
353
415
354
- //Step 5
355
416
if ( ! BuildApk ( ) )
356
417
{
357
418
Debug . LogError ( "failed to BuildApk" ) ;
@@ -360,31 +421,33 @@ public static void BuildAll()
360
421
Debug . Log ( "Done!" ) ;
361
422
}
362
423
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 ) ]
364
425
public static void BuildWithoutPatch ( )
365
426
{
366
- //Step 1
367
427
if ( ! ExportGradleProject ( ) )
368
428
{
369
429
Debug . LogError ( "failed to ExportGradleProject" ) ;
370
430
return ;
371
431
}
372
432
373
- //Step 2
374
433
if ( ! PatchAndroidProject ( ) )
375
434
{
376
435
Debug . LogError ( "failed to PatchAndroidProject" ) ;
377
436
return ;
378
437
}
379
438
380
- //Step 4
439
+ if ( ! PatchAndroidProject ( ) )
440
+ {
441
+ Debug . LogError ( "failed to PatchAndroidProject" ) ;
442
+ return ;
443
+ }
444
+
381
445
if ( ! GenerateBuildScripts ( ) )
382
446
{
383
447
Debug . LogError ( "failed to GenerateBuildScripts" ) ;
384
448
return ;
385
449
}
386
450
387
- //Step 5
388
451
if ( ! BuildApk ( ) )
389
452
{
390
453
Debug . LogError ( "failed to BuildApk" ) ;
@@ -393,7 +456,7 @@ public static void BuildWithoutPatch()
393
456
Debug . Log ( "Done!" ) ;
394
457
}
395
458
396
- [ MenuItem ( "AndroidBuilder/Run Step 1-4 for Patch Version" , false , 3 ) ]
459
+ [ MenuItem ( "AndroidBuilder/Run Step 1-5 for Patch Version" , false , 3 ) ]
397
460
public static void BuildPatch ( )
398
461
{
399
462
//Step 1
@@ -403,21 +466,18 @@ public static void BuildPatch()
403
466
return ;
404
467
}
405
468
406
- //Step 2
407
469
if ( ! PatchAndroidProject ( ) )
408
470
{
409
471
Debug . LogError ( "failed to PatchAndroidProject" ) ;
410
472
return ;
411
473
}
412
474
413
- //Step 3
414
475
if ( ! GenerateBinPatches ( ) )
415
476
{
416
477
Debug . LogError ( "failed to GenerateBinPatches" ) ;
417
478
return ;
418
479
}
419
480
420
- //Step 4
421
481
if ( ! GenerateBuildScripts ( ) )
422
482
{
423
483
Debug . LogError ( "failed to GenerateBuildScripts" ) ;
0 commit comments