@@ -17,18 +17,20 @@ public class AndroidBuilder : MonoBehaviour {
1717 //-----------------------------------------------------------------------------------
1818 public static readonly string PROJECT_DIR = Application . dataPath . Substring ( 0 , Application . dataPath . Length - 6 ) ;
1919 public static readonly string ANDROID_EXPORT_PATH = PROJECT_DIR + "/AndroidGradleProject_v1.0" ;
20- public static string ANDROID_PROJECT_PATH { get { return ANDROID_EXPORT_PATH + "/" + PlayerSettings . productName ; } }
21- public static string ANDROID_MANIFEST_PATH = ANDROID_PROJECT_PATH + "/src/main/" ;
22- public static string JAVA_SRC_PATH = ANDROID_PROJECT_PATH + "/src/main/java/" ;
23- public static string JAR_LIB_PATH = ANDROID_PROJECT_PATH + "/libs/" ;
20+ public static string ANDROID_PROJECT_PATH { get { return ANDROID_EXPORT_PATH ; } }
21+ public static string ANDROID_MANIFEST_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/ src/main/" ;
22+ public static string JAVA_SRC_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/ src/main/java/" ;
23+ public static string JAR_LIB_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/ libs/" ;
2424 public static string SO_DIR_NAME = "jniLibs" ;
25- public static string SO_LIB_PATH = ANDROID_PROJECT_PATH + "/src/main/jniLibs/" ;
26- public static string EXPORTED_ASSETS_PATH = ANDROID_PROJECT_PATH + "/src/main/assets" ;
27- public static string R_JAVA_PATH = ANDROID_PROJECT_PATH + "/src/main/gen/" ;
28- public static string RES_PATH = ANDROID_PROJECT_PATH + "/src/main/res" ;
29- public static string MANIFEST_XML_PATH = ANDROID_PROJECT_PATH + "/src/main/AndroidManifest.xml" ;
30- public static string JAVA_OBJ_PATH = ANDROID_PROJECT_PATH + "/src/main/objs/" ;
31- public static string BUILD_SCRIPTS_PATH = ANDROID_PROJECT_PATH + "/src/main/" ;
25+ public static string SO_LIB_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/jniLibs/" ;
26+ public static string EXPORTED_ASSETS_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/assets" ;
27+ public static string R_JAVA_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/gen/" ;
28+ public static string LAUNCHER_RES_PATH = ANDROID_PROJECT_PATH + "/launcher/src/main/res" ;
29+ public static string LAUNCHER_MANIFEST_XML_PATH = ANDROID_PROJECT_PATH + "/launcher/src/main/AndroidManifest.xml" ;
30+ public static string RES_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/res" ;
31+ public static string MANIFEST_XML_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/AndroidManifest.xml" ;
32+ public static string JAVA_OBJ_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/objs/" ;
33+ public static string BUILD_SCRIPTS_PATH = ANDROID_PROJECT_PATH + "/unityLibrary/src/main/" ;
3234 public static string ZIP_PATH = PROJECT_DIR + "/Assets/AndroidIl2cppPatchDemo/Editor/Exe/zip.exe" ;
3335
3436 static bool Exec ( string filename , string args )
@@ -131,9 +133,7 @@ public static bool ExportGradleProject()
131133 EditorUserBuildSettings . androidBuildSystem = AndroidBuildSystem . Gradle ;
132134 PlayerSettings . SetScriptingBackend ( BuildTargetGroup . Android , ScriptingImplementation . IL2CPP ) ;
133135 PlayerSettings . stripEngineCode = false ;
134- #if UNITY_2018 || UNITY_2019
135- PlayerSettings . Android . targetArchitectures = AndroidArchitecture . ARMv7 | AndroidArchitecture . X86 | AndroidArchitecture . ARM64 ;
136- #endif
136+ PlayerSettings . Android . targetArchitectures = AndroidArchitecture . ARMv7 | AndroidArchitecture . ARM64 ;
137137
138138 //export project
139139 string error_msg = string . Empty ;
@@ -143,11 +143,7 @@ public static bool ExportGradleProject()
143143 Directory . CreateDirectory ( ANDROID_EXPORT_PATH ) ;
144144 try
145145 {
146- #if UNITY_2018 || UNITY_2019
147146 error_msg = BuildPipeline . BuildPlayer ( levels , ANDROID_EXPORT_PATH , EditorUserBuildSettings . activeBuildTarget , options ) . summary . result == UnityEditor . Build . Reporting . BuildResult . Succeeded ? string . Empty : "Failed to export project!" ;
148- #else
149- error_msg = BuildPipeline . BuildPlayer ( levels , ANDROID_EXPORT_PATH , EditorUserBuildSettings . activeBuildTarget , options ) ;
150- #endif
151147 }
152148 catch ( Exception e )
153149 {
@@ -179,7 +175,7 @@ public static bool PatchAndroidProject()
179175 }
180176 string javaEntranceFile = javaEntranceFiles [ 0 ] ;
181177 string allJavaText = File . ReadAllText ( javaEntranceFile ) ;
182- if ( allJavaText . IndexOf ( "bootstrap " ) > 0 )
178+ if ( allJavaText . IndexOf ( "noodle1983 " ) > 0 )
183179 {
184180 Debug . Log ( "UnityPlayerActivity.java already patched." ) ;
185181 return true ;
@@ -188,9 +184,9 @@ public static bool PatchAndroidProject()
188184 @"import android.view.WindowManager;
189185import io.github.noodle1983.Boostrap;" ) ;
190186
191- allJavaText = allJavaText . Replace ( "mUnityPlayer = new UnityPlayer(this);" ,
187+ allJavaText = allJavaText . Replace ( "mUnityPlayer = new UnityPlayer(this,this );" ,
192188 @"Boostrap.InitNativeLibBeforeUnityPlay(getApplication().getApplicationContext().getFilesDir().getPath());
193- mUnityPlayer = new UnityPlayer(this);" ) ;
189+ mUnityPlayer = new UnityPlayer(this,this );" ) ;
194190 File . WriteAllText ( javaEntranceFile , allJavaText ) ;
195191 return true ;
196192 }
@@ -209,11 +205,8 @@ public static bool GenerateBinPatches()
209205 string [ ] [ ] soPatchFile =
210206 {
211207 // path_in_android_project, filename inside zip, zip file anme
212- new string [ 3 ] { "/" + SO_DIR_NAME + "/armeabi-v7a/libil2cpp.so" , "libil2cpp.so.new" , "lib_armeabi-v7a_libil2cpp.so.zip" } ,
213- new string [ 3 ] { "/" + SO_DIR_NAME + "/x86/libil2cpp.so" , "libil2cpp.so.new" , "lib_x86_libil2cpp.so.zip" } ,
214- #if UNITY_2018 || UNITY_2019
208+ new string [ 3 ] { "/" + SO_DIR_NAME + "/armeabi-v7a/libil2cpp.so" , "libil2cpp.so.new" , "lib_armeabi-v7a_libil2cpp.so.zip" } ,
215209 new string [ 3 ] { "/" + SO_DIR_NAME + "/arm64-v8a/libil2cpp.so" , "libil2cpp.so.new" , "lib_arm64-v8a_libil2cpp.so.zip" } ,
216- #endif
217210 } ;
218211
219212 for ( int i = 0 ; i < soPatchFile . Length ; i ++ )
@@ -246,7 +239,8 @@ public static bool GenerateBinPatches()
246239
247240 if ( allZipCmds . Length > 0 )
248241 {
249- string zipPatchesFile = BUILD_SCRIPTS_PATH + "/" + "zip_patches.bat" ;
242+ string zipPatchesFile = ANDROID_EXPORT_PATH + "/" + "zip_patches.bat" ;
243+ File . WriteAllText ( zipPatchesFile , allZipCmds . ToString ( ) ) ;
250244 File . WriteAllText ( zipPatchesFile , allZipCmds . ToString ( ) ) ;
251245 if ( ! Exec ( zipPatchesFile , zipPatchesFile ) )
252246 {
@@ -260,79 +254,22 @@ public static bool GenerateBinPatches()
260254 [ MenuItem ( "AndroidBuilder/Step 4: Generate Build Scripts" , false , 104 ) ]
261255 public static bool GenerateBuildScripts ( )
262256 {
263- string sdkPath = EditorPrefs . GetString ( "AndroidSdkRoot" , "" ) ;
264257 string jdkPath = EditorPrefs . GetString ( "JdkPath" , "" ) ; ;
265- if ( string . IsNullOrEmpty ( sdkPath ) || string . IsNullOrEmpty ( jdkPath ) )
258+ if ( string . IsNullOrEmpty ( jdkPath ) )
266259 {
267- Debug . LogError ( "sdk/ jdk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
260+ Debug . LogError ( "jdk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
268261 return false ;
269262 }
270263
271- StringBuilder allCmd = new StringBuilder ( ) ;
272-
273- // gen R.java
274- string buildToolPath = sdkPath + "/build-tools/" + ANDROID_BUILD_TOOLS_VERSION + "/" ;
275- string aaptPath = buildToolPath + "/aapt.exe" ;
276- string platformJar = sdkPath + "/platforms/" + ANDROID_PLATFORM + "/android.jar" ;
277- string genRJavaCmd = " package -f -m "
278- + " -J " + R_JAVA_PATH
279- + " -M " + MANIFEST_XML_PATH
280- + " -S " + RES_PATH
281- + " -I " + platformJar ;
282- if ( ! Directory . Exists ( R_JAVA_PATH ) ) { Directory . CreateDirectory ( R_JAVA_PATH ) ; }
283- allCmd . AppendFormat ( "call \" {0}\" {1}\n \n " , aaptPath , genRJavaCmd ) ;
284-
285- //build java
286- string javacPath = jdkPath + "/bin/javac.exe" ;
287- string [ ] jarLibFiles = Directory . GetFiles ( JAR_LIB_PATH , "*.jar" , SearchOption . AllDirectories ) ;
288- string [ ] javaSrcFiles = Directory . GetFiles ( ANDROID_PROJECT_PATH , "*.java" , SearchOption . AllDirectories ) ;
289- string compileParam = "-d " + JAVA_OBJ_PATH
290- + " -source 1.7 -target 1.7 "
291- + " -classpath " + string . Join ( ";" , jarLibFiles )
292- + " -sourcepath " + JAVA_SRC_PATH
293- + " -bootclasspath "
294- + platformJar + " " + string . Join ( " " , javaSrcFiles ) ;
295- if ( ! Directory . Exists ( JAVA_OBJ_PATH ) ) { Directory . CreateDirectory ( JAVA_OBJ_PATH ) ; }
296- allCmd . AppendFormat ( "call \" {0}\" {1}\n \n " , javacPath , compileParam ) ;
297-
298- //dx
299- string dxPath = buildToolPath + "/dx.bat" ;
300- string pkgRawPath = BUILD_SCRIPTS_PATH + "/pkg_raw" ;
301- string dexPath = pkgRawPath + "/classes.dex" ;
302- string deParam = " --dex --output=" + dexPath + " " + JAVA_OBJ_PATH + " " + string . Join ( " " , jarLibFiles ) ;
303- if ( ! Directory . Exists ( pkgRawPath ) ) { Directory . CreateDirectory ( pkgRawPath ) ; }
304- allCmd . AppendFormat ( "call \" {0}\" {1}\n \n @echo on\n \n " , dxPath , deParam ) ;
305-
306- //prepare lib
307- string outputLibPath = pkgRawPath + "/lib" ;
308- if ( Directory . Exists ( outputLibPath ) ) { FileUtil . DeleteFileOrDirectory ( outputLibPath ) ; }
309- Directory . CreateDirectory ( outputLibPath ) ;
310- FileUtil . ReplaceDirectory ( SO_LIB_PATH + "/armeabi-v7a" , outputLibPath + "/armeabi-v7a" ) ;
311- FileUtil . ReplaceDirectory ( SO_LIB_PATH + "/x86" , outputLibPath + "/x86" ) ;
312- #if UNITY_2018 || UNITY_2019
313- FileUtil . ReplaceDirectory ( SO_LIB_PATH + "/arm64-v8a" , outputLibPath + "/arm64-v8a" ) ;
314- FileUtil . DeleteFileOrDirectory ( outputLibPath + "/arm64-v8a/Data" ) ;
315- #endif
316- FileUtil . DeleteFileOrDirectory ( outputLibPath + "/armeabi-v7a/Data" ) ;
317- FileUtil . DeleteFileOrDirectory ( outputLibPath + "/x86/Data" ) ;
318- var debug_files = Directory . GetFiles ( outputLibPath , "*.*" , SearchOption . AllDirectories ) . Where ( s => s . EndsWith ( ".debug" ) || s . EndsWith ( ".map" ) || s . EndsWith ( ".sym" ) ) ;
319- foreach ( string file in debug_files ) { File . Delete ( file ) ; }
320-
321- //build apk
322- string binPath = BUILD_SCRIPTS_PATH + "/bin" ;
323- if ( ! Directory . Exists ( binPath ) ) { Directory . CreateDirectory ( binPath ) ; }
324- string unaligned_apk_path = binPath + "/" + Application . identifier + "." + ".unaligned.apk" ;
325- string assetsPath = EXPORTED_ASSETS_PATH ;
326- string buildApkParam = " package -f -m -F " + unaligned_apk_path + " -M " + MANIFEST_XML_PATH + " -A " + assetsPath + " -S " + RES_PATH + " -I " + platformJar
327- + " " + pkgRawPath ;
328- allCmd . AppendFormat ( "call \" {0}\" {1}\n \n " , aaptPath , buildApkParam ) ;
329-
330- //align
331- string zipalign = buildToolPath + "/zipalign.exe" ;
332- string alignedApkName = Application . identifier + ".apk" ;
333- string alignedApkPath = binPath + "/" + alignedApkName ;
334- string zipalignParam = " -f 4 " + unaligned_apk_path + " " + alignedApkPath ;
335- allCmd . AppendFormat ( "call \" {0}\" {1}\n \n " , zipalign , zipalignParam ) ;
264+ //must use the jdk in Unity
265+ string gradlePath = jdkPath + "/../Tools/Gradle" ;
266+ string [ ] gradleMainJarFiles = Directory . GetFiles ( gradlePath + "/lib" , "gradle-launcher*.jar" , SearchOption . TopDirectoryOnly ) ;
267+ if ( gradleMainJarFiles . Length == 0 )
268+ {
269+ Debug . LogError ( "gradle-launcher jar file not found in " + gradlePath + "/lib" ) ;
270+ return false ;
271+ }
272+ string gradleMainJarFile = gradleMainJarFiles [ 0 ] ;
336273
337274 //sign
338275 string keystoreDir = PROJECT_DIR + "/AndroidKeystore" ;
@@ -348,31 +285,38 @@ public static bool GenerateBuildScripts()
348285 return false ;
349286 }
350287 }
351- string apksignerPath = buildToolPath + "/apksigner.bat" ;
352- string signParam = " sign --ks " + keystoreFile + " --ks-pass pass:testtest --key-pass pass:testtest " + alignedApkPath ;
353- allCmd . AppendFormat ( "call \" {0}\" {1}\n \n " , apksignerPath , signParam ) ;
354288
355- //del tmp apk
356- allCmd . AppendFormat ( "del /f /a /Q {0}\n \n " , unaligned_apk_path . Replace ( "/" , "\\ " ) ) ;
357- allCmd . AppendFormat ( "explorer.exe {0} \n \n " , binPath . Replace ( "//" , "/" ) . Replace ( "/" , "\\ " ) ) ;
289+ StringBuilder allCmd = new StringBuilder ( ) ;
290+ allCmd . AppendFormat ( "cd \" {0}\" \n \n " , ANDROID_EXPORT_PATH ) ;
291+ allCmd . AppendFormat ( "call \" {0}\" "
292+ + " -classpath \" {1}\" org.gradle.launcher.GradleMain \" -Dorg.gradle.jvmargs=-Xmx4096m\" \" assembleRelease\" "
293+ + " -Pandroid.injected.signing.store.file=\" {2}\" "
294+ + " -Pandroid.injected.signing.store.password=testtest "
295+ + " -Pandroid.injected.signing.key.alias=test "
296+ + " -Pandroid.injected.signing.key.password=testtest"
297+ + " \n \n " ,
298+ jdkPath + "/bin/java.exe" ,
299+ gradleMainJarFile ,
300+ keystoreFile ) ;
301+
302+ allCmd . AppendFormat ( "copy /Y \" {0}\\ launcher\\ build\\ outputs\\ apk\\ release\\ launcher-release.apk\" \" {0}\\ {1}.apk\" \n \n " ,
303+ ANDROID_EXPORT_PATH . Replace ( "//" , "/" ) . Replace ( "/" , "\\ " ) ,
304+ Application . identifier ) ;
305+
306+ allCmd . AppendFormat ( "explorer.exe {0} \n \n " , ANDROID_EXPORT_PATH . Replace ( "//" , "/" ) . Replace ( "/" , "\\ " ) ) ;
358307 allCmd . AppendFormat ( "@echo on\n \n " ) ; //explorer as the last line wont return success, so...
359- File . WriteAllText ( BUILD_SCRIPTS_PATH + "/build_apk.bat" , allCmd . ToString ( ) ) ;
360-
361- //bugs for android-23
362- //https://issuetracker.unity3d.com/issues/android-build-fails-with-failed-to-repackage-resources-error-when-api-level-23-or-lower-is-used
363- string manifestXmlPath = MANIFEST_XML_PATH ;
364- string content = File . ReadAllText ( manifestXmlPath ) ;
365- File . WriteAllText ( manifestXmlPath , content . Replace ( @"|density" , "" ) ) ;
308+ File . WriteAllText ( ANDROID_EXPORT_PATH + "/build_apk.bat" , allCmd . ToString ( ) ) ;
309+
366310 return true ;
367311 }
368312
369313
370314 [ MenuItem ( "AndroidBuilder/Step 5: Build Apk File" , false , 105 ) ]
371315 public static bool BuildApk ( )
372316 {
373- string buildApkPath = BUILD_SCRIPTS_PATH + "/build_apk.bat" ;
317+ string buildApkPath = ANDROID_EXPORT_PATH + "/build_apk.bat" ;
374318 string alignedApkName = Application . identifier + ".apk" ;
375- string alignedApkPath = BUILD_SCRIPTS_PATH + "/bin /" + alignedApkName ;
319+ string alignedApkPath = ANDROID_EXPORT_PATH + "/" + alignedApkName ;
376320
377321 if ( ! Exec ( buildApkPath , "" ) )
378322 {
0 commit comments