转载请注明出处:/post/726490…

本文出自 容华谢后的博客

往期回顾:

Chromium内核浏览器编译记(一)踩坑实录

Chromium内核浏览器编译记(二)UI定制

0.写在前面

最近由于业务需求,需求编译最新版别的Chromium内核,现在最新的版别是116.0.5845.59,编译的过程中发现有一些代码逻辑修正的当地都变了,所以在此记录下,共享给我们。

定制需求是这样的:

  • 修正包名、版别号、使用图标、使用称号

  • 不显现使用第一次启动时的设置引导页

  • 使用全屏显现,移除主页的一切菜单栏

  • 添加点击两次退出使用的功能

  • 默许不加载新标签页

1.修正包名

源码途径:/chromium/src/chrome/android/chrome_public_apk_tmpl.gni

将下面两个特点的值替换成你要修正的包名就能够了:

chrome_public_manifest_package = "org.chromium.chrome"
chrome_public_test_manifest_package = "org.chromium.chrome.tests"

2.修正版别号

源码途径:/chromium/src/build/config/android/config.gni

将下面四个特点的值替换成你要修正的版别号就能够了:

android_default_version_code = "10000"
android_default_version_name = "1.00.00"
android_override_version_code = "10000"
android_override_version_name = "1.00.00"

3.修正使用图标

源码途径:/chromium/src/chrome/android/java/res_chromium_base

在这个目录下,有几个mipmap文件夹,把里边的图标替换下就能够,和AS项目是一样的。

4.修正使用称号

源码途径:/chromium/src/chrome/android/java/res_chromium_base/values/channel_constants.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <string name="app_name" translatable="false">使用称号</string>
</resources>

5.屏蔽启动引导页

源码途径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/init/AsyncInitializationActivity.java

将requiresFirstRunToBeCompleted办法的返回值修正为false:

/**
 * Overriding this function is almost always wrong.
 * @return Whether or not the user needs to go through First Run before using this Activity.
 */
protected boolean requiresFirstRunToBeCompleted(Intent intent) {
    return false;
}

源码途径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java

删去或注释掉dispatch办法中下面的判别逻辑:

// Check if we should push the user through First Run.
if (FirstRunFlowSequencer.launch(mActivity, mIntent, false /* requiresBroadcast */,
            false /* preferLightweightFre */)) {
    return Action.FINISH_ACTIVITY;
}

6.使用全屏显现

源码途径:/chromium/src/chrome/android/java/res/values/styles.xml

在上面的源码中,找到Base.Theme.Chromium.WithWindowAnimation主题,添加下面的全屏设置特点:

<style name="Base.Theme.Chromium.WithWindowAnimation">
   ...
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowActionBar">false</item>
</style>

7.移除主页的一切菜单栏

源码途径:/chromium/src/chrome/android/java/res_app/layout/main.xml

在CoordinatorLayoutForPointer控件中,添加layout_marginTop特点,其实是往上移动了UI,把菜单栏隐藏了:

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
	...
    <org.chromium.components.browser_ui.widget.CoordinatorLayoutForPointer
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" 
        android:layout_marginTop="-96dp">
</mergr>        

8.添加点击两次退出使用的功能

源码途径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/app/ChromeActivity.java

在ChromeActivity类中添加下面的办法:

public int getTabsCount() {
    TabModelSelector modelSelector = getTabModelSelector();
    if (modelSelector == null) return 0;
    return modelSelector.getTotalTabCount();
}    

源码途径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java

修正minimizeAppAndCloseTabOnBackPress办法:

private boolean mExitApp;
private boolean minimizeAppAndCloseTabOnBackPress(@Nullable Tab currentTab) {
    if (currentTab == null) {
        BackPressManager.record(BackPressHandler.Type.MINIMIZE_APP_AND_CLOSE_TAB);
        MinimizeAppAndCloseTabBackPressHandler.record(MinimizeAppAndCloseTabType.MINIMIZE_APP);
        assertOnLastBackPress();
        moveTaskToBack(true);
        return true;
    }
    // TAB history handler has a higher priority and should navigate page back before
    // minimizing app and closing tab.
    assert !currentTab.canGoBack()
        : "Tab should be navigated back before closing or exiting app";
    final boolean shouldCloseTab = backShouldCloseTab(currentTab);
    final WebContents webContents = currentTab.getWebContents();
    // Minimize the app if either:
    // - we decided not to close the tab
    // - we decided to close the tab, but it was opened by an external app, so we will go
    //   exit Chrome on top of closing the tab
    final boolean minimizeApp = false;
    BackPressManager.record(BackPressHandler.Type.MINIMIZE_APP_AND_CLOSE_TAB);
    assertOnLastBackPress();
    if (minimizeApp) {
        if (shouldCloseTab) {
            MinimizeAppAndCloseTabBackPressHandler.record(
                    MinimizeAppAndCloseTabType.MINIMIZE_APP_AND_CLOSE_TAB);
            sendToBackground(currentTab);
        } else {
            MinimizeAppAndCloseTabBackPressHandler.record(
                    MinimizeAppAndCloseTabType.MINIMIZE_APP);
            sendToBackground(null);
        }
        return true;
    } else if (shouldCloseTab) {
        if (getTabsCount() > 1) {
            MinimizeAppAndCloseTabBackPressHandler.record(MinimizeAppAndCloseTabType.CLOSE_TAB);
            if (webContents != null) webContents.dispatchBeforeUnload(false);
            return true;
        }
        if (mExitApp) {
            MinimizeAppAndCloseTabBackPressHandler.record(MinimizeAppAndCloseTabType.CLOSE_TAB);
            if (webContents != null) webContents.dispatchBeforeUnload(false);
            return false;
        }
        Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
        mExitApp = true;
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                mExitApp = false;
            }
        }, 2000);
        return true;
    }
    return false;
}

9.默许不加载新标签页

源码途径:/chromium/src/components/embedder_support/android/java/src/org/chromium/components/embedder_support/util/UrlConstants.java

把下面两个参数,改成你想要显现的网页地址就能够了:

public static final String NTP_URL = "chrome-native://newtab/";
public static final String NTP_NON_NATIVE_URL = "chrome://newtab/";

10.导航栏布景

新版别的浏览器,底部导航栏布景是白色的,如果有修正布景色彩的需求,能够改这儿:

源码途径:/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/tabbed_mode/TabbedNavigationBarColorController.java

把updateNavigationBarColor办法中,setNavigationBarIconColor参数改成false:

@SuppressLint("NewApi")
private void updateNavigationBarColor() {
    ...
    UiUtils.setNavigationBarIconColor(mRootView, false);
}

修正下面两个办法,冒号后面修正成你想要设置的导航栏布景色彩就能够了:

private @ColorInt int getNavigationBarColor(boolean forceDarkNavigationBar) {
    return forceDarkNavigationBar
            ? mContext.getColor(R.color.toolbar_background_primary_dark)
            : mContext.getColor(R.color.navigation_bubble_arrow);
}
private @ColorInt int getNavigationBarDividerColor(boolean forceDarkNavigationBar) {
    return forceDarkNavigationBar
            ? mContext.getColor(R.color.bottom_system_nav_divider_color_light)
            : mContext.getColor(R.color.navigation_bubble_arrow);
}

11.遇到的问题

更新源码的时候,遇到这个提示:

NOTICE: You have PROXY values set in your environment, but gsutilin depot_tools does not (yet) obey them.
Also, --no_auth prevents the normal BOTO_CONFIG environmentvariable from being used.
To use a proxy in this situation, please supply those settingsin a .boto file pointed to by the NO_AUTH_BOTO_CONFIG environmentvariable.

被墙了,现已设置了大局署理,还需求再加个装备,将下面的装备保存成文件**.boto**,然后装备下环境变量

[boto]
proxy=127.0.0.1
proxy_port=端口号

环境变量:

export PATH="$PATH:$HOME/.boto"

留意:文章中是把depot_tool下载到了【home/用户名】目录下,所以使用【$HOME】代表,环境变量要根据实际下载的途径修正。

12.编译脚本

最终在附上args.gn编译脚本:

target_os = "android"
target_cpu = "arm"
# Release版别
is_debug = false
# 不允许长途调试
enable_remoting = false
# 使用Chrome官方的编译优化主张
is_official_build = true
# 分散链接编译
is_component_build = false
# 不使用官方API秘钥
use_official_google_api_keys = false
# 封闭支撑NACL,这是一种Chrome插件,由于安全性,稳定性存在问题,现已很少使用了
enable_nacl = false
# 支撑H264编码
proprietary_codecs = true
# 解码器
ffmpeg_branding = "Chrome"
# 处理不能播放MP4格式的视频问题
rtc_use_h264  = true
declare_args(){
  # 全体优化,对整个程序的多个编译单元进行大局优化,获取更好的性能和代码体积优化
  full_wpo_on_official = true
  # 删去内核层支撑调试的符号文件
  remove_webcore_debug_symbols = false
  # 初始化FFmpeg
  rtc_initialize_ffmpeg = true
  # 支撑OpenH264
  use_openh264 = true
}

13.写在最终

到这儿,Chromium源码的UI定制就完成了,有问题能够给我留言谈论,谢谢。