美文网首页
Fastlane自动打包apk

Fastlane自动打包apk

作者: Mob_Developer | 来源:发表于2019-04-19 17:27 被阅读0次

    初始化Fastlane

    切换到android项目根目录,执行fastlane init命令。根据fastlane提示,做相应的操作。初始化完成后,在根目录下,fastlane会自动创建fastlane文件夹,在fastlane文件夹下有AppfileFastfile两个文件。Appfile用来配置应用的基本信息,如包名、AppleId、teamID等。Fastfile就是fastlane定义自动化任务的地方。

    编写Fastfile代码

    将以下代码复制到Fastfile中

    # This file contains the fastlane.tools configuration
    # You can find the documentation at https://docs.fastlane.tools
    #
    # For a list of all available actions, check out
    #
    #     https://docs.fastlane.tools/actions
    #
    # For a list of all available plugins, check out
    #
    #     https://docs.fastlane.tools/plugins/available-plugins
    #
    
    # Uncomment the line if you want fastlane to automatically update itself
    # update_fastlane
    
    default_platform(:android)
    
    platform :android do
      desc "以下【./】代表的是android项目根目录"
      
      puts("------设置App名称,App版本更新------")
      # next 表示提前退出
    
      # 设置App名称
      apk_name = "功夫熊猫";
      android_change_string_app_name(newName: apk_name);
      # versionCode + 1
      increment_version_code(app_project_dir: './app')
      # versionName增加 可选值 'patch'、'minor'、'major'
      increment_version_name(app_project_dir: './app', bump_type: 'patch')
    
      puts("------打包前App信息:------")
      # 获取版本名称
      version_name = get_version_name(app_project_dir: './app');
      # 获取版本号
      version_code = get_version_code(app_project_dir: './app');
      puts(apk_name + "; versionCode: " + version_code + "; versionName: " + version_name);
    
      lane :beta do
        puts("开始打包Android APK!");
        gradle(
          task: 'assemble', 
          build_type: 'Release',
           print_command: true,   
          properties: {
            "android.injected.signing.store.file" => "./keystores/keystore",
            "android.injected.signing.store.password" => "12345678",
            "android.injected.signing.key.alias" => "12345",
            "android.injected.signing.key.password" => "12345678",
          },
        );
        puts("------打包后App信息:------")
        app_info;
    
        # 获取Apk默认输出路径
        apk_file_path = lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH];
        # 重新命名Fastlane默认Apk文件,并移动到/fastlane/packages目录下
        fastlane_directory = Dir.pwd
        apk_name = apk_name + "_" + version_code + "_" + version_name + ".apk";
        apk_path = fastlane_directory + "/packages/" +  apk_name;
        sh("mv", apk_file_path, apk_path);
        sh("open", fastlane_directory + "/packages");
    
        # 打印apk的完整路径
        puts("apk保存路径: " + apk_path);
        say("Android打包完成");
        notification(title: "Android打包完成", message: "保存路径:" + apk_path);
      end
    
    end
    
    

    根据项目的具体修改,修改以上代码的内容:
    正常来讲,只要修改apk_name以及keystore的信息即可。

    安装必要插件:

    bundle exec fastlane add_plugin android_change_string_app_name
    bundle exec fastlane add_plugin android_versioning
    bundle exec fastlane add_plugin app_info
    

    更多插件,可参照Fastlane可用插件

    执行打包命令

    bundle exec fastlane beta。打包完成后的apk文件放在fastlane/packages目录下。

    问题及解决方案

    • 问题:Execution failed for task ':react-native-version-number:verifyReleaseResources' ——
      解决方案

    相关文章

      网友评论

          本文标题:Fastlane自动打包apk

          本文链接:https://www.haomeiwen.com/subject/rarcgqtx.html