@@ -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
196192 // generate removal test file
@@ -218,11 +214,8 @@ public static bool GenerateBinPatches()
218214 string [ ] [ ] soPatchFile =
219215 {
220216 // path_in_android_project, filename inside zip, zip file anme
221- new string [ 3 ] { "/" + SO_DIR_NAME + "/armeabi-v7a/libil2cpp.so" , "libil2cpp.so.new" , "lib_armeabi-v7a_libil2cpp.so.zip" } ,
222- new string [ 3 ] { "/" + SO_DIR_NAME + "/x86/libil2cpp.so" , "libil2cpp.so.new" , "lib_x86_libil2cpp.so.zip" } ,
223- #if UNITY_2018 || UNITY_2019
217+ new string [ 3 ] { "/" + SO_DIR_NAME + "/armeabi-v7a/libil2cpp.so" , "libil2cpp.so.new" , "lib_armeabi-v7a_libil2cpp.so.zip" } ,
224218 new string [ 3 ] { "/" + SO_DIR_NAME + "/arm64-v8a/libil2cpp.so" , "libil2cpp.so.new" , "lib_arm64-v8a_libil2cpp.so.zip" } ,
225- #endif
226219 } ;
227220
228221 for ( int i = 0 ; i < soPatchFile . Length ; i ++ )
@@ -255,7 +248,8 @@ public static bool GenerateBinPatches()
255248
256249 if ( allZipCmds . Length > 0 )
257250 {
258- string zipPatchesFile = BUILD_SCRIPTS_PATH + "/" + "zip_patches.bat" ;
251+ string zipPatchesFile = ANDROID_EXPORT_PATH + "/" + "zip_patches.bat" ;
252+ File . WriteAllText ( zipPatchesFile , allZipCmds . ToString ( ) ) ;
259253 File . WriteAllText ( zipPatchesFile , allZipCmds . ToString ( ) ) ;
260254 if ( ! Exec ( zipPatchesFile , zipPatchesFile ) )
261255 {
@@ -269,79 +263,22 @@ public static bool GenerateBinPatches()
269263 [ MenuItem ( "AndroidBuilder/Step 4: Generate Build Scripts" , false , 104 ) ]
270264 public static bool GenerateBuildScripts ( )
271265 {
272- string sdkPath = EditorPrefs . GetString ( "AndroidSdkRoot" , "" ) ;
273266 string jdkPath = EditorPrefs . GetString ( "JdkPath" , "" ) ; ;
274- if ( string . IsNullOrEmpty ( sdkPath ) || string . IsNullOrEmpty ( jdkPath ) )
267+ if ( string . IsNullOrEmpty ( jdkPath ) )
275268 {
276- Debug . LogError ( "sdk/ jdk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
269+ Debug . LogError ( "jdk path is empty! please config via menu path:Edit/Preference->External tools." ) ;
277270 return false ;
278271 }
279272
280- StringBuilder allCmd = new StringBuilder ( ) ;
281-
282- // gen R.java
283- string buildToolPath = sdkPath + "/build-tools/" + ANDROID_BUILD_TOOLS_VERSION + "/" ;
284- string aaptPath = buildToolPath + "/aapt.exe" ;
285- string platformJar = sdkPath + "/platforms/" + ANDROID_PLATFORM + "/android.jar" ;
286- string genRJavaCmd = " package -f -m "
287- + " -J " + R_JAVA_PATH
288- + " -M " + MANIFEST_XML_PATH
289- + " -S " + RES_PATH
290- + " -I " + platformJar ;
291- if ( ! Directory . Exists ( R_JAVA_PATH ) ) { Directory . CreateDirectory ( R_JAVA_PATH ) ; }
292- allCmd . AppendFormat ( "call \" {0}\" {1}\n \n " , aaptPath , genRJavaCmd ) ;
293-
294- //build java
295- string javacPath = jdkPath + "/bin/javac.exe" ;
296- string [ ] jarLibFiles = Directory . GetFiles ( JAR_LIB_PATH , "*.jar" , SearchOption . AllDirectories ) ;
297- string [ ] javaSrcFiles = Directory . GetFiles ( ANDROID_PROJECT_PATH , "*.java" , SearchOption . AllDirectories ) ;
298- string compileParam = "-d " + JAVA_OBJ_PATH
299- + " -source 1.7 -target 1.7 "
300- + " -classpath " + string . Join ( ";" , jarLibFiles )
301- + " -sourcepath " + JAVA_SRC_PATH
302- + " -bootclasspath "
303- + platformJar + " " + string . Join ( " " , javaSrcFiles ) ;
304- if ( ! Directory . Exists ( JAVA_OBJ_PATH ) ) { Directory . CreateDirectory ( JAVA_OBJ_PATH ) ; }
305- allCmd . AppendFormat ( "call \" {0}\" {1}\n \n " , javacPath , compileParam ) ;
306-
307- //dx
308- string dxPath = buildToolPath + "/dx.bat" ;
309- string pkgRawPath = BUILD_SCRIPTS_PATH + "/pkg_raw" ;
310- string dexPath = pkgRawPath + "/classes.dex" ;
311- string deParam = " --dex --output=" + dexPath + " " + JAVA_OBJ_PATH + " " + string . Join ( " " , jarLibFiles ) ;
312- if ( ! Directory . Exists ( pkgRawPath ) ) { Directory . CreateDirectory ( pkgRawPath ) ; }
313- allCmd . AppendFormat ( "call \" {0}\" {1}\n \n @echo on\n \n " , dxPath , deParam ) ;
314-
315- //prepare lib
316- string outputLibPath = pkgRawPath + "/lib" ;
317- if ( Directory . Exists ( outputLibPath ) ) { FileUtil . DeleteFileOrDirectory ( outputLibPath ) ; }
318- Directory . CreateDirectory ( outputLibPath ) ;
319- FileUtil . ReplaceDirectory ( SO_LIB_PATH + "/armeabi-v7a" , outputLibPath + "/armeabi-v7a" ) ;
320- FileUtil . ReplaceDirectory ( SO_LIB_PATH + "/x86" , outputLibPath + "/x86" ) ;
321- #if UNITY_2018 || UNITY_2019
322- FileUtil . ReplaceDirectory ( SO_LIB_PATH + "/arm64-v8a" , outputLibPath + "/arm64-v8a" ) ;
323- FileUtil . DeleteFileOrDirectory ( outputLibPath + "/arm64-v8a/Data" ) ;
324- #endif
325- FileUtil . DeleteFileOrDirectory ( outputLibPath + "/armeabi-v7a/Data" ) ;
326- FileUtil . DeleteFileOrDirectory ( outputLibPath + "/x86/Data" ) ;
327- var debug_files = Directory . GetFiles ( outputLibPath , "*.*" , SearchOption . AllDirectories ) . Where ( s => s . EndsWith ( ".debug" ) || s . EndsWith ( ".map" ) || s . EndsWith ( ".sym" ) ) ;
328- foreach ( string file in debug_files ) { File . Delete ( file ) ; }
329-
330- //build apk
331- string binPath = BUILD_SCRIPTS_PATH + "/bin" ;
332- if ( ! Directory . Exists ( binPath ) ) { Directory . CreateDirectory ( binPath ) ; }
333- string unaligned_apk_path = binPath + "/" + Application . identifier + "." + ".unaligned.apk" ;
334- string assetsPath = EXPORTED_ASSETS_PATH ;
335- string buildApkParam = " package -f -m -F " + unaligned_apk_path + " -M " + MANIFEST_XML_PATH + " -A " + assetsPath + " -S " + RES_PATH + " -I " + platformJar
336- + " " + pkgRawPath ;
337- allCmd . AppendFormat ( "call \" {0}\" {1}\n \n " , aaptPath , buildApkParam ) ;
338-
339- //align
340- string zipalign = buildToolPath + "/zipalign.exe" ;
341- string alignedApkName = Application . identifier + ".apk" ;
342- string alignedApkPath = binPath + "/" + alignedApkName ;
343- string zipalignParam = " -f 4 " + unaligned_apk_path + " " + alignedApkPath ;
344- allCmd . AppendFormat ( "call \" {0}\" {1}\n \n " , zipalign , zipalignParam ) ;
273+ //must use the jdk in Unity
274+ string gradlePath = jdkPath + "/../Tools/Gradle" ;
275+ string [ ] gradleMainJarFiles = Directory . GetFiles ( gradlePath + "/lib" , "gradle-launcher*.jar" , SearchOption . TopDirectoryOnly ) ;
276+ if ( gradleMainJarFiles . Length == 0 )
277+ {
278+ Debug . LogError ( "gradle-launcher jar file not found in " + gradlePath + "/lib" ) ;
279+ return false ;
280+ }
281+ string gradleMainJarFile = gradleMainJarFiles [ 0 ] ;
345282
346283 //sign
347284 string keystoreDir = PROJECT_DIR + "/AndroidKeystore" ;
@@ -357,31 +294,38 @@ public static bool GenerateBuildScripts()
357294 return false ;
358295 }
359296 }
360- string apksignerPath = buildToolPath + "/apksigner.bat" ;
361- string signParam = " sign --ks " + keystoreFile + " --ks-pass pass:testtest --key-pass pass:testtest " + alignedApkPath ;
362- allCmd . AppendFormat ( "call \" {0}\" {1}\n \n " , apksignerPath , signParam ) ;
363297
364- //del tmp apk
365- allCmd . AppendFormat ( "del /f /a /Q {0}\n \n " , unaligned_apk_path . Replace ( "/" , "\\ " ) ) ;
366- allCmd . AppendFormat ( "explorer.exe {0} \n \n " , binPath . Replace ( "//" , "/" ) . Replace ( "/" , "\\ " ) ) ;
298+ StringBuilder allCmd = new StringBuilder ( ) ;
299+ allCmd . AppendFormat ( "cd \" {0}\" \n \n " , ANDROID_EXPORT_PATH ) ;
300+ allCmd . AppendFormat ( "call \" {0}\" "
301+ + " -classpath \" {1}\" org.gradle.launcher.GradleMain \" -Dorg.gradle.jvmargs=-Xmx4096m\" \" assembleRelease\" "
302+ + " -Pandroid.injected.signing.store.file=\" {2}\" "
303+ + " -Pandroid.injected.signing.store.password=testtest "
304+ + " -Pandroid.injected.signing.key.alias=test "
305+ + " -Pandroid.injected.signing.key.password=testtest"
306+ + " \n \n " ,
307+ jdkPath + "/bin/java.exe" ,
308+ gradleMainJarFile ,
309+ keystoreFile ) ;
310+
311+ allCmd . AppendFormat ( "copy /Y \" {0}\\ launcher\\ build\\ outputs\\ apk\\ release\\ launcher-release.apk\" \" {0}\\ {1}.apk\" \n \n " ,
312+ ANDROID_EXPORT_PATH . Replace ( "//" , "/" ) . Replace ( "/" , "\\ " ) ,
313+ Application . identifier ) ;
314+
315+ allCmd . AppendFormat ( "explorer.exe {0} \n \n " , ANDROID_EXPORT_PATH . Replace ( "//" , "/" ) . Replace ( "/" , "\\ " ) ) ;
367316 allCmd . AppendFormat ( "@echo on\n \n " ) ; //explorer as the last line wont return success, so...
368- File . WriteAllText ( BUILD_SCRIPTS_PATH + "/build_apk.bat" , allCmd . ToString ( ) ) ;
369-
370- //bugs for android-23
371- //https://issuetracker.unity3d.com/issues/android-build-fails-with-failed-to-repackage-resources-error-when-api-level-23-or-lower-is-used
372- string manifestXmlPath = MANIFEST_XML_PATH ;
373- string content = File . ReadAllText ( manifestXmlPath ) ;
374- File . WriteAllText ( manifestXmlPath , content . Replace ( @"|density" , "" ) ) ;
317+ File . WriteAllText ( ANDROID_EXPORT_PATH + "/build_apk.bat" , allCmd . ToString ( ) ) ;
318+
375319 return true ;
376320 }
377321
378322
379323 [ MenuItem ( "AndroidBuilder/Step 5: Build Apk File" , false , 105 ) ]
380324 public static bool BuildApk ( )
381325 {
382- string buildApkPath = BUILD_SCRIPTS_PATH + "/build_apk.bat" ;
326+ string buildApkPath = ANDROID_EXPORT_PATH + "/build_apk.bat" ;
383327 string alignedApkName = Application . identifier + ".apk" ;
384- string alignedApkPath = BUILD_SCRIPTS_PATH + "/bin /" + alignedApkName ;
328+ string alignedApkPath = ANDROID_EXPORT_PATH + "/" + alignedApkName ;
385329
386330 if ( ! Exec ( buildApkPath , "" ) )
387331 {
0 commit comments