Class is implemented in both, One of the two will be used. Which one is undefined

后端 未结 5 887
感情败类
感情败类 2020-12-04 19:15

I have an issue with dependencies included in Cocoapods.

I have a Framework project (MyFramework target), which also has App target (MyFrameworkExampleApp). When I t

5条回答
  •  误落风尘
    2020-12-04 20:03

    Updated: I write a blog article for my solution: https://medium.com/@GalvinLi/tinysolution-fix-cocoapods-duplicate-implement-warning-5a2e1a505ea8

    And a demo project: https://github.com/bestwnh/TinySolution


    I got the solution idea from internet but can't find one solution work, so I make a workaround by myself. Maybe the code is a bit long, but it work. Hope it can help someone.

    The auto_process_target(,,) is the key function, just change it to fit your project and all should work fine. (Because I use one framework for multi app target, so I make the app target parameter be an array.)

    post_install do |installer|
    
      # you should change the sample auto_process_target method call to fit your project
    
      # sample for the question
      auto_process_target(['MyFrameworkExampleApp'], 'MyFramework', installer)
      # sample for the multi app use on same framework
      auto_process_target(['exampleiOSApp', 'exampleMacApp'], 'exampleFramework', installer)
    
    end
    
    # the below code no need to modify
    
    def auto_process_target(app_target_names, embedded_target_name, installer)
      words = find_words_at_embedded_target('Pods-' + embedded_target_name,
                                            installer)
      handle_app_targets(app_target_names.map{ |str| 'Pods-' + str },
                         words,
                         installer)
    end
    
    def find_line_with_start(str, start)
      str.each_line do |line|
          if line.start_with?(start)
            return line
          end
      end
      return nil
    end
    
    def remove_words(str, words)
      new_str = str
      words.each do |word| 
        new_str = new_str.sub(word, '')
      end
      return new_str
    end
    
    def find_words_at_embedded_target(target_name, installer)
      target = installer.pods_project.targets.find { |target| target.name == target_name }
      target.build_configurations.each do |config|
        xcconfig_path = config.base_configuration_reference.real_path
        xcconfig = File.read(xcconfig_path)
        old_line = find_line_with_start(xcconfig, "OTHER_LDFLAGS")
    
        if old_line == nil 
          next
        end
        words = old_line.split(' ').select{ |str| str.start_with?("-l") }.map{ |str| ' ' + str }
        return words
      end
    end
    
    def handle_app_targets(names, words, installer)
      installer.pods_project.targets.each do |target|
        if names.index(target.name) == nil
          next
        end
        puts "Updating #{target.name} OTHER_LDFLAGS"
        target.build_configurations.each do |config|
          xcconfig_path = config.base_configuration_reference.real_path
          xcconfig = File.read(xcconfig_path)
          old_line = find_line_with_start(xcconfig, "OTHER_LDFLAGS")
    
          if old_line == nil 
            next
          end
          new_line = remove_words(old_line, words)
    
          new_xcconfig = xcconfig.sub(old_line, new_line)
          File.open(xcconfig_path, "w") { |file| file << new_xcconfig }
        end
      end
    end
    

    If all work. You will see Update xxxx OTHER_LDFLAGS when you pod install or pod update. Then the warning gone.

提交回复
热议问题