10
10
#if UNITY_IOS
11
11
using UnityEditor . iOS . Xcode ;
12
12
#endif
13
+ #if UNITY_2019_3_OR_NEWER
14
+ using UnityEditor . iOS . Xcode . Extensions ;
15
+ #endif
13
16
14
17
namespace AdjustSdk
15
18
{
16
19
public class AdjustEditor : AssetPostprocessor
17
20
{
18
- [ MenuItem ( "Assets/Adjust/Export Unity Package" ) ]
19
- public static void ExportAdjustUnityPackage ( )
20
- {
21
- string exportedFileName = "Adjust.unitypackage" ;
22
- string assetsPath = "Assets/Adjust" ;
23
- List < string > assetsToExport = new List < string > ( ) ;
24
-
25
- // Adjust Assets.
26
- assetsToExport . Add ( assetsPath + "/3rd Party/SimpleJSON.cs" ) ;
27
-
28
- assetsToExport . Add ( assetsPath + "/Android/adjust-android.jar" ) ;
29
- assetsToExport . Add ( assetsPath + "/Android/AdjustAndroid.cs" ) ;
30
- assetsToExport . Add ( assetsPath + "/Android/AdjustAndroidManifest.xml" ) ;
31
-
32
- assetsToExport . Add ( assetsPath + "/Editor/AdjustEditor.cs" ) ;
33
- assetsToExport . Add ( assetsPath + "/Editor/AdjustSettings.cs" ) ;
34
- assetsToExport . Add ( assetsPath + "/Editor/AdjustSettingsEditor.cs" ) ;
35
- assetsToExport . Add ( assetsPath + "/Editor/AdjustCustomEditor.cs" ) ;
36
- assetsToExport . Add ( assetsPath + "/Editor/AdjustEditorPreprocessor.cs" ) ;
37
-
38
- assetsToExport . Add ( assetsPath + "/ExampleGUI/ExampleGUI.cs" ) ;
39
- assetsToExport . Add ( assetsPath + "/ExampleGUI/ExampleGUI.prefab" ) ;
40
- assetsToExport . Add ( assetsPath + "/ExampleGUI/ExampleGUI.unity" ) ;
41
-
42
- assetsToExport . Add ( assetsPath + "/iOS/ADJAttribution.h" ) ;
43
- assetsToExport . Add ( assetsPath + "/iOS/ADJConfig.h" ) ;
44
- assetsToExport . Add ( assetsPath + "/iOS/ADJEvent.h" ) ;
45
- assetsToExport . Add ( assetsPath + "/iOS/ADJEventFailure.h" ) ;
46
- assetsToExport . Add ( assetsPath + "/iOS/ADJEventSuccess.h" ) ;
47
- assetsToExport . Add ( assetsPath + "/iOS/ADJLogger.h" ) ;
48
- assetsToExport . Add ( assetsPath + "/iOS/ADJSessionFailure.h" ) ;
49
- assetsToExport . Add ( assetsPath + "/iOS/ADJSessionSuccess.h" ) ;
50
- assetsToExport . Add ( assetsPath + "/iOS/ADJSubscription.h" ) ;
51
- assetsToExport . Add ( assetsPath + "/iOS/Adjust.h" ) ;
52
- assetsToExport . Add ( assetsPath + "/iOS/AdjustiOS.cs" ) ;
53
- assetsToExport . Add ( assetsPath + "/iOS/AdjustSdk.a" ) ;
54
- assetsToExport . Add ( assetsPath + "/iOS/AdjustUnity.h" ) ;
55
- assetsToExport . Add ( assetsPath + "/iOS/AdjustUnity.mm" ) ;
56
- assetsToExport . Add ( assetsPath + "/iOS/AdjustUnityDelegate.h" ) ;
57
- assetsToExport . Add ( assetsPath + "/iOS/AdjustUnityDelegate.mm" ) ;
58
-
59
- assetsToExport . Add ( assetsPath + "/Prefab/Adjust.prefab" ) ;
60
-
61
- assetsToExport . Add ( assetsPath + "/Unity/Adjust.cs" ) ;
62
- assetsToExport . Add ( assetsPath + "/Unity/AdjustAppStoreSubscription.cs" ) ;
63
- assetsToExport . Add ( assetsPath + "/Unity/AdjustAttribution.cs" ) ;
64
- assetsToExport . Add ( assetsPath + "/Unity/AdjustConfig.cs" ) ;
65
- assetsToExport . Add ( assetsPath + "/Unity/AdjustEnvironment.cs" ) ;
66
- assetsToExport . Add ( assetsPath + "/Unity/AdjustEvent.cs" ) ;
67
- assetsToExport . Add ( assetsPath + "/Unity/AdjustEventFailure.cs" ) ;
68
- assetsToExport . Add ( assetsPath + "/Unity/AdjustEventSuccess.cs" ) ;
69
- assetsToExport . Add ( assetsPath + "/Unity/AdjustLogLevel.cs" ) ;
70
- assetsToExport . Add ( assetsPath + "/Unity/AdjustPlayStoreSubscription.cs" ) ;
71
- assetsToExport . Add ( assetsPath + "/Unity/AdjustSessionFailure.cs" ) ;
72
- assetsToExport . Add ( assetsPath + "/Unity/AdjustSessionSuccess.cs" ) ;
73
- assetsToExport . Add ( assetsPath + "/Unity/AdjustUtils.cs" ) ;
74
-
75
- AssetDatabase . ExportPackage (
76
- assetsToExport . ToArray ( ) ,
77
- exportedFileName ,
78
- ExportPackageOptions . IncludeDependencies | ExportPackageOptions . Interactive ) ;
79
- }
21
+ private const int AdjustEditorPostProcesssBuildPriority = 90 ;
22
+ private const string TargetUnityIphonePodfileLine = "target 'Unity-iPhone' do" ;
23
+ private const string UseFrameworksPodfileLine = "use_frameworks!" ;
24
+ private const string UseFrameworksDynamicPodfileLine = "use_frameworks! :linkage => :dynamic" ;
25
+ private const string UseFrameworksStaticPodfileLine = "use_frameworks! :linkage => :static" ;
80
26
81
- [ PostProcessBuild ]
27
+ [ PostProcessBuild ( AdjustEditorPostProcesssBuildPriority ) ]
82
28
public static void OnPostprocessBuild ( BuildTarget target , string projectPath )
83
29
{
84
30
RunPostBuildScript ( target : target , projectPath : projectPath ) ;
@@ -169,17 +115,9 @@ private static void RunPostBuildScript(BuildTarget target, string projectPath =
169
115
Debug . Log ( "[Adjust]: Obj-C exceptions enabled successfully." ) ;
170
116
}
171
117
172
- if ( xcodeProject . ContainsFileByProjectPath ( "Libraries/Adjust/Native/iOS/AdjustSigSdk.a" ) )
173
- {
174
- if ( ! string . IsNullOrEmpty ( xcodeTargetUnityFramework ) )
175
- {
176
- xcodeProject . AddBuildProperty ( xcodeTargetUnityFramework , "OTHER_LDFLAGS" , "-force_load $(PROJECT_DIR)/Libraries/Adjust/Native/iOS/AdjustSigSdk.a" ) ;
177
- }
178
- else
179
- {
180
- xcodeProject . AddBuildProperty ( xcodeTarget , "OTHER_LDFLAGS" , "-force_load $(PROJECT_DIR)/Libraries/Adjust/Native/iOS/AdjustSigSdk.a" ) ;
181
- }
182
- }
118
+ // potential AdjustSigSdk.xcframework embedding
119
+ Debug . Log ( "[Adjust]: Checking whether AdjustSigSdk.xcframework needs to be embedded or not..." ) ;
120
+ EmbedAdjustSignatureIfNeeded ( projectPath , xcodeProject , xcodeTarget ) ;
183
121
184
122
// Save the changes to Xcode project file.
185
123
xcodeProject . WriteToFile ( xcodeProjectPath ) ;
@@ -188,6 +126,93 @@ private static void RunPostBuildScript(BuildTarget target, string projectPath =
188
126
}
189
127
190
128
#if UNITY_IOS
129
+ // dynamic xcframework embedding logic adjusted and taken from:
130
+ // https://github.com/AppLovin/AppLovin-MAX-Unity-Plugin/blob/master/DemoApp/Assets/MaxSdk/Scripts/IntegrationManager/Editor/AppLovinPostProcessiOS.cs
131
+ private static void EmbedAdjustSignatureIfNeeded ( string buildPath , PBXProject project , string targetGuid )
132
+ {
133
+ var podsDirectory = Path . Combine ( buildPath , "Pods" ) ;
134
+
135
+ if ( ! Directory . Exists ( podsDirectory ) || ! ShouldEmbedDynamicLibraries ( buildPath ) )
136
+ {
137
+ Debug . Log ( "[Adjust]: No need to embed AdjustSigSdk.xcframework." ) ;
138
+ return ;
139
+ }
140
+
141
+ var dynamicLibraryPathToEmbed = GetAdjustSignaturePathToEmbed ( podsDirectory , buildPath ) ;
142
+ if ( dynamicLibraryPathToEmbed == null ) {
143
+ return ;
144
+ }
145
+
146
+ Debug . Log ( "[Adjust]: It needs to be embedded. Starting the embedding process..." ) ;
147
+ #if UNITY_2019_3_OR_NEWER
148
+ var fileGuid = project . AddFile ( dynamicLibraryPathToEmbed , dynamicLibraryPathToEmbed ) ;
149
+ project . AddFileToEmbedFrameworks ( targetGuid , fileGuid ) ;
150
+ #else
151
+ string runpathSearchPaths ;
152
+ runpathSearchPaths = project . GetBuildPropertyForAnyConfig ( targetGuid , "LD_RUNPATH_SEARCH_PATHS" ) ;
153
+ runpathSearchPaths += string . IsNullOrEmpty ( runpathSearchPaths ) ? "" : " " ;
154
+
155
+ // check if runtime search paths already contains the required search paths for dynamic libraries
156
+ if ( runpathSearchPaths . Contains ( "@executable_path/Frameworks" ) ) {
157
+ return ;
158
+ }
159
+
160
+ runpathSearchPaths += "@executable_path/Frameworks" ;
161
+ project . SetBuildProperty ( targetGuid , "LD_RUNPATH_SEARCH_PATHS" , runpathSearchPaths ) ;
162
+ #endif
163
+ Debug . Log ( "[Adjust]: Embedding process completed." ) ;
164
+ }
165
+
166
+ private static bool ShouldEmbedDynamicLibraries ( string buildPath )
167
+ {
168
+ var podfilePath = Path . Combine ( buildPath , "Podfile" ) ;
169
+ if ( ! File . Exists ( podfilePath ) ) {
170
+ return false ;
171
+ }
172
+
173
+ // if the Podfile doesn't have a `Unity-iPhone` target, we should embed the dynamic libraries
174
+ var lines = File . ReadAllLines ( podfilePath ) ;
175
+ var containsUnityIphoneTarget = lines . Any ( line => line . Contains ( TargetUnityIphonePodfileLine ) ) ;
176
+ if ( ! containsUnityIphoneTarget ) {
177
+ return true ;
178
+ }
179
+
180
+ // if the Podfile does not have a `use_frameworks! :linkage => static` line, we should not embed the dynamic libraries
181
+ var useFrameworksStaticLineIndex = Array . FindIndex ( lines , line => line . Contains ( UseFrameworksStaticPodfileLine ) ) ;
182
+ if ( useFrameworksStaticLineIndex == - 1 ) {
183
+ return false ;
184
+ }
185
+
186
+ // if more than one of the `use_frameworks!` lines are present, CocoaPods will use the last one
187
+ var useFrameworksLineIndex = Array . FindIndex ( lines , line => line . Trim ( ) == UseFrameworksPodfileLine ) ; // check for exact line to avoid matching `use_frameworks! :linkage => static/dynamic`
188
+ var useFrameworksDynamicLineIndex = Array . FindIndex ( lines , line => line . Contains ( UseFrameworksDynamicPodfileLine ) ) ;
189
+
190
+ // check if `use_frameworks! :linkage => :static` is the last line of the three
191
+ // if it is, we should embed the dynamic libraries
192
+ return useFrameworksLineIndex < useFrameworksStaticLineIndex && useFrameworksDynamicLineIndex < useFrameworksStaticLineIndex ;
193
+ }
194
+
195
+ private static string GetAdjustSignaturePathToEmbed ( string podsDirectory , string buildPath )
196
+ {
197
+ var adjustSignatureFrameworkToEmbed = "AdjustSigSdk.xcframework" ;
198
+
199
+ // both .framework and .xcframework are directories, not files
200
+ var directories = Directory . GetDirectories ( podsDirectory , adjustSignatureFrameworkToEmbed , SearchOption . AllDirectories ) ;
201
+ if ( directories . Length <= 0 ) {
202
+ return null ;
203
+ }
204
+
205
+ var dynamicLibraryAbsolutePath = directories [ 0 ] ;
206
+ var relativePath = GetDynamicLibraryRelativePath ( dynamicLibraryAbsolutePath ) ;
207
+ return relativePath ;
208
+ }
209
+
210
+ private static string GetDynamicLibraryRelativePath ( string dynamicLibraryAbsolutePath )
211
+ {
212
+ var index = dynamicLibraryAbsolutePath . LastIndexOf ( "Pods" , StringComparison . Ordinal ) ;
213
+ return dynamicLibraryAbsolutePath . Substring ( index ) ;
214
+ }
215
+
191
216
private static void HandlePlistIosChanges ( string projectPath )
192
217
{
193
218
const string UserTrackingUsageDescriptionKey = "NSUserTrackingUsageDescription" ;
0 commit comments