11#if UNITY_ANDROID
2- #if UNITY_2022 || UNITY_2021_3_5 || UNITY_2021_3_49 || UNITY_2021_3_48 || UNITY_2021_3_47 || UNITY_2021_3_46 || UNITY_2021_3_45 || UNITY_2021_3_44 || UNITY_2021_3_43 || UNITY_2021_3_42 || UNITY_2021_3_41
2+ #if UNITY_2022 || UNITY_2021_3_55 || UNITY_2021_3_54 || UNITY_2021_3_53 || UNITY_2021_3_52 || UNITY_2021_3_51 || UNITY_2021_3_50 || UNITY_2021_3_49 || UNITY_2021_3_48 || UNITY_2021_3_47 || UNITY_2021_3_46 || UNITY_2021_3_45 || UNITY_2021_3_44 || UNITY_2021_3_43 || UNITY_2021_3_42 || UNITY_2021_3_41
33// 2021.3.41f1+ Gradle version 7.5.1+
44// https://docs.unity3d.com/2021.3/Documentation/Manual/android-gradle-overview.html
55#define ANDROID_GRADLE_BUILD_JETIFIER_ENTRY_ENABLED
66#endif
77
88#if UNITY_6000_0_OR_NEWER || UNITY_2023 || ANDROID_GRADLE_BUILD_JETIFIER_ENTRY_ENABLED
9- #define ANDROID_GRADLE_BUILD_POST_PROCESSOR_ENABLED
9+ #define ANDROID_GRADLE_BUILD_PRE_PROCESSOR_ENABLED
1010#endif
1111
1212using System ;
@@ -24,8 +24,8 @@ namespace GoogleMobileAds.Editor
2424 /// with the Google Mobile Ads SDK. This includes:
2525 /// - Verify the Android Google Mobile Ads app ID is set.
2626 /// - Throw an exception if the Android Google Mobile Ads app ID is not set.
27- /// - Set minimum API level to 23.
28- /// - Set target API level to 34 .
27+ /// - Set minimum API level to 23 (the target API level may be automatically set, we should not
28+ /// hardcode it) .
2929 /// - Enable Custom Main Gradle Template.
3030 /// - Update Custom Main Gradle Template with dependencies using the Play Services Resolver.
3131 /// - Enable Custom Gradle Properties Template.
@@ -34,7 +34,6 @@ namespace GoogleMobileAds.Editor
3434 public class BuildPreProcessor : IPreprocessBuildWithReport
3535 {
3636 const int MinimumAPILevel = 23 ;
37- const int TargetAPILevel = 34 ;
3837 const string CustomGradlePropertiesTemplatesFileName = "gradleTemplate.properties" ;
3938 const string CustomMainGradleTemplateFileName = "mainTemplate.gradle" ;
4039 const string JetifierEntry =
@@ -46,29 +45,19 @@ public class BuildPreProcessor : IPreprocessBuildWithReport
4645
4746 public void OnPreprocessBuild ( BuildReport report )
4847 {
49- if ( GoogleMobileAdsSettings . LoadInstance ( ) . GoogleMobileAdsAndroidAppId . Length == 0 )
50- {
51- throw new BuildFailedException (
52- "The Android Google Mobile Ads app ID is empty. " +
53- "Add a valid app ID to your GoogleMobileAdsSettings to run ads properly." ) ;
54- }
55- if ( ! GoogleMobileAdsSettings . LoadInstance ( ) . EnableGradleBuildPostProcessor )
48+ if ( ! GoogleMobileAdsSettings . LoadInstance ( ) . EnableGradleBuildPreProcessor )
5649 {
5750 return ;
5851 }
59- #if ANDROID_GRADLE_BUILD_POST_PROCESSOR_ENABLED
52+ // For more details see, https://developers.google.com/admob/unity/android
53+ #if ANDROID_GRADLE_BUILD_PRE_PROCESSOR_ENABLED
6054 ApplyBuildSettings ( report ) ;
61- #else
62- throw new BuildFailedException (
63- "Unity Editor 2021.3.41f1 or higher is required to enable the gradle build " +
64- "post-processor. Update your build settings manually or disable the gradle build " +
65- "post-processor. For more details see, https://developers.google.com/admob/unity/android" ) ;
66- #endif
55+ #endif
6756 }
6857
6958 private void ApplyBuildSettings ( BuildReport report )
7059 {
71- Debug . Log ( "Running Android Gradle Build Post -Processor." ) ;
60+ Debug . Log ( "Running Android Gradle Build Pre -Processor." ) ;
7261
7362 // Set Minimum Api Level.
7463 if ( PlayerSettings . Android . minSdkVersion < ( AndroidSdkVersions ) MinimumAPILevel )
@@ -81,28 +70,17 @@ private void ApplyBuildSettings(BuildReport report)
8170 Debug . Log ( $ "Verified Minimum API Level is >= { MinimumAPILevel } .") ;
8271 }
8372
84- // Set Target Api Level.
85- if ( PlayerSettings . Android . targetSdkVersion < ( AndroidSdkVersions ) TargetAPILevel )
86- {
87- PlayerSettings . Android . targetSdkVersion = ( AndroidSdkVersions ) TargetAPILevel ;
88- Debug . Log ( $ "Set target API Level to: { TargetAPILevel } ") ;
89- }
90- else
91- {
92- Debug . Log ( $ "Verified Target API Level is >= { TargetAPILevel } .") ;
93- }
94-
9573 // Create Assets/Plugins folder.
96- if ( ! AssetDatabase . IsValidFolder ( "Assets/ Plugins" ) )
74+ if ( ! AssetDatabase . IsValidFolder ( Path . Combine ( "Assets" , " Plugins") ) )
9775 {
9876 AssetDatabase . CreateFolder ( "Assets" , "Plugins" ) ;
9977 AssetDatabase . Refresh ( ) ;
10078 }
10179
10280 // Create Assets/Plugins/Android folder.
103- if ( ! AssetDatabase . IsValidFolder ( "Assets/ Plugins/ Android" ) )
81+ if ( ! AssetDatabase . IsValidFolder ( Path . Combine ( "Assets" , " Plugins" , " Android") ) )
10482 {
105- AssetDatabase . CreateFolder ( "Assets/ Plugins" , "Android" ) ;
83+ AssetDatabase . CreateFolder ( Path . Combine ( "Assets" , " Plugins") , "Android" ) ;
10684 AssetDatabase . Refresh ( ) ;
10785 }
10886
@@ -115,7 +93,7 @@ private void ApplyBuildSettings(BuildReport report)
11593 #if ANDROID_GRADLE_BUILD_JETIFIER_ENTRY_ENABLED
11694 string customGradlePropertiesTemplatesFilePath = Path . Combine (
11795 Application . dataPath ,
118- "Plugins/ Android" ,
96+ "Plugins" , " Android",
11997 CustomGradlePropertiesTemplatesFileName ) ;
12098 if ( File . Exists ( customGradlePropertiesTemplatesFilePath ) )
12199 {
@@ -141,7 +119,7 @@ private void ApplyBuildSettings(BuildReport report)
141119
142120 Debug . Log ( "Resolving Android Gradle dependencies." ) ;
143121 PlayServicesResolver . ResolveSync ( true ) ;
144- Debug . Log ( "Android Build Post -Processor finished." ) ;
122+ Debug . Log ( "Android Build Pre -Processor finished." ) ;
145123 }
146124
147125 /// <summary>
@@ -154,16 +132,15 @@ private void EnsureGradleFileExists(string fileName)
154132 bool foundDisabledFile = false ;
155133
156134 // Check for target file.
157- string targetPath = Path . Combine ( Application . dataPath ,
158- $ "Plugins/Android/{ fileName } ") ;
135+ string targetPath = Path . Combine ( Application . dataPath , "Plugins" , "Android" , fileName ) ;
159136 if ( File . Exists ( targetPath ) )
160137 {
161138 foundTargetFile = true ;
162139 }
163140
164141 // Check for the ".DISABLED" file.
165- string disabledPath = Path . Combine ( Application . dataPath ,
166- $ "Plugins/Android/ { fileName } .DISABLED") ;
142+ string disabledPath = Path . Combine ( Application . dataPath , "Plugins" , "Android" ,
143+ $ " { fileName } .DISABLED") ;
167144 if ( File . Exists ( disabledPath ) )
168145 {
169146 foundDisabledFile = true ;
@@ -200,7 +177,7 @@ private void EnsureGradleFileExists(string fileName)
200177 if ( ! File . Exists ( sourceFileName ) )
201178 {
202179 throw new BuildFailedException (
203- "Android Build Post -Processor failed. " +
180+ "Android Build Pre -Processor failed. " +
204181 $ "Unable to find source { sourceFileName } . Is your file system read-only?" +
205182 "If this issue persists, contact Google Mobile Ads Support " +
206183 "at https://developers.google.com/admob/support" ) ;
0 commit comments