preface

When we write a Flutter project, every time we execute pub clean, the.ios project will be re-created and the bitcode of the project will reset to true. The project will fail to compile. I’m sure you’ve all encountered this before. Considering the complexity of manual operation made my team members very upset. At the same time, I have recently learned Python, so I can use this to practice my hand, and use py script to deal with this problem with one click, so everyone can also be happy to develop ~😆

Bitcode problem

thinking

I thought about using it beforeshellrubyAnd so on to achieve this, the search found that someone put forward ideasportalI’ve tried itstackoverflowAfter 25 likes, try to find it works, but fail to compile and give up. Later usepbxprojjustPythonIt’s an opportunity to put what you learn into practice.

#pod install
xcodebuild -target house591 -configuration Debug ENABLE_BITCODE=YES
xcodebuild -target house591 -configuration Test ENABLE_BITCODE=YES
xcodebuild -target house591 -configuration Release ENABLE_BITCODE=YES

Copy the code

The environment

The first thing you need to do is install Python3, which I won’t go into. If you want to use a py virtual environment, you can refer to this article using PyEnv. I’m using Python 3.8.10.

The plug-in

The Xcode Build Setting is set using a third-party plug-in portal

# Install as follows
pip3 install pbxproj
Copy the code

Use the Bitcode core code to set up the Flutter project

# set bitcode
def set_bit_code_path(path, is_close_bitcode=True) :
    project = XcodeProject.load(path)
    bitcode_str = "NO"
    if not is_close_bitcode:
        bitcode_str = "YES"
    print(F "set_bit_code_path Current project path:{path} bitcode_str: {bitcode_str}")
    set_bit_code_flag(project, bitcode_str, configuration_name="Debug")
    set_bit_code_flag(project, bitcode_str, configuration_name="Profile")
    set_bit_code_flag(project, bitcode_str, configuration_name="Release")
    project.save()


Set the bitcode parameter
def set_bit_code_flag(project, bitcode_str, target_name="Runner", configuration_name='Debug') :
    project.set_flags('ENABLE_BITCODE', bitcode_str, target_name=target_name, configuration_name=configuration_name)
Copy the code

use

Methods a

  • Direct terminal execution, which we are using ourselvesSSH tools shuttleOne-click execution – current our project toFlutterThe direction of the technology stack turned, so three were builtFlutter moduleBusiness components. Therefore, I will only create three script paths for the Flutter project and add script commands as necessary.
  • Of course, we can also directly terminal execution, as follows
cd /Users/zhengzeqin/Desktop/GitLab/TWHouseScript; python disable_bitcode.py -p '/Users/zhengzeqin/Desktop/GitLab/tw591_salehouse' -s close
Copy the code
  • Actually,shuttleThe command is usedjsonTo set, you can combinejsonnetTo generate it dynamically so that it can be reused to others

Way 2

If you do not want to do that, multiple projects cooperate with the shuttle command. The path can also be written in scripts. Each Flutter project is configured with its own Python script command. Remember to define its own project path

Define the project Module file name
_module_names = ['tw591_salehouse']
The parent directory of the project
_file_path = "/Users/zhengzeqin/Desktop/GitLab/"
# flutter project.pbxpro
_flutter_pro = '/.ios/Runner.xcodeproj/project.pbxproj'

# set bitcode
def set_bit_code(file_path, module_name) :
    path = file_path + module_name + _flutter_pro
    # print(" current project path: ", path)
    set_bit_code_path(path)
    
The local file configuration path handles bitcode
def handle_projects_bit_code() :
    for module_name in _module_names:
        set_bit_code(_file_path, module_name)
        
if __name__ == "__main__":
    The local file configuration path handles bitcode
    handle_projects_bit_code()
    # If a flutter is configured, read the project.pbxproj path
    current_path = os.path.abspath(os.curdir) + _flutter_pro
    print(current_path)
Copy the code

Complete script code

# -*- coding: utf-8 -*-
# -*- author: zhengzeqin -*-
# -*- date: 2022-03-01 -*-

The dependency library needs to be installed
# pip3 install pbxproj

import os
import getopt
from pbxproj import XcodeProject
import sys

# Define the file name of the Flutter project Module
_module_names = ['tw591_salehouse']
The parent directory of the project
_file_path = "/Users/zhengzeqin/Desktop/GitLab/"
# flutter project.pbxpro
_flutter_pro = '/.ios/Runner.xcodeproj/project.pbxproj'


The local file configuration path handles bitcode
def handle_projects_bit_code() :
    for module_name in _module_names:
        set_bit_code(_file_path, module_name)


# set bitcode
def set_bit_code(file_path, module_name) :
    path = file_path + module_name + _flutter_pro
    # print(" current project path: ", path)
    set_bit_code_path(path)


# set bitcode
def set_bit_code_path(path, is_close_bitcode=True) :
    project = XcodeProject.load(path)
    bitcode_str = "NO"
    if not is_close_bitcode:
        bitcode_str = "YES"
    print(F "set_bit_code_path Current project path:{path} bitcode_str: {bitcode_str}")
    set_bit_code_flag(project, bitcode_str, configuration_name="Debug")
    set_bit_code_flag(project, bitcode_str, configuration_name="Profile")
    set_bit_code_flag(project, bitcode_str, configuration_name="Release")
    project.save()


Set the bitcode parameter
def set_bit_code_flag(project, bitcode_str, target_name="Runner", configuration_name='Debug') :
    project.set_flags('ENABLE_BITCODE', bitcode_str, target_name=target_name, configuration_name=configuration_name)

if __name__ == "__main__":
    The local file configuration path handles bitcode
    # handle_projects_bit_code()
    # If a flutter is configured, read the project.pbxproj path
    # current_path = os.path.abspath(os.curdir) + _flutter_pro
    # print(current_path)

    argv = sys.argv[1:]
    # Project path
    project_path = ""
    # default off
    is_close_bitcode = True
    try:
        opts, args = getopt.getopt(argv, "p:s:"["path="."switch="])
    except getopt.GetoptError:
        print('disable_bitcode.py -p "Project path "')
        sys.exit(2)

    print("opts ===>", opts)

    for opt, arg in opts:
        if opt in ["-p"."--path"]:
            project_path = arg
            if len(project_path) == 0:
                print('Please enter the address of the project')
                sys.exit('Please enter the address of the project')
        if opt in ["-s"."--switch"] :if arg == "open":
                is_close_bitcode = False
            if arg == "close":
                is_close_bitcode = True

    Get the path to the project you want to fix
    path = project_path + _flutter_pro
    Set bitcode = false
    set_bit_code_path(path, is_close_bitcode)
Copy the code

Think about 🤔

The above solution is iOS bitcode = true compilation failure, in fact, Android also has a low default version caused compilation failure, can we also solve the problem through script?