Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android零碎知识点整理 #55

Open
soapgu opened this issue Jun 18, 2021 · 0 comments
Open

Android零碎知识点整理 #55

soapgu opened this issue Jun 18, 2021 · 0 comments
Labels
安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented Jun 18, 2021

  • 前言

安卓开发过程中,遇到了一些有点冷而又相对重要的知识点。特地去开篇显得少了。汇总下一起发布总结下。

  • 1.Build Variant

Android Studio 会自动为您创建“debug”build 类型和“release”build 类型
如果要改变build类型,在菜单栏中依次选择 Build > Select Build Variant。
吐槽下要一个个改,不能批量改,真是蠢。

与代码交互
app模块会自动生成BuildConfig类

public final class BuildConfig {
  public static final boolean DEBUG = false;
  public static final String APPLICATION_ID = "com.xxx.xxxx";
  public static final String BUILD_TYPE = "release";
  public static final int VERSION_CODE = 83;
  public static final String VERSION_NAME = "8.3";
}

直接上代码。API30全屏处理有新的api

protected void hideBottomUIMenu() {
        if( !BuildConfig.DEBUG ) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
                View decorView = getWindow().getDecorView();
                int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
                decorView.setSystemUiVisibility(uiOptions);
            } else {
                getWindow().getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
                getWindow().getInsetsController().hide(WindowInsets.Type.navigationBars() | WindowInsets.Type.statusBars());
            }
        }
    }

应用场景是这样的。我在其中一个module里面声明了一个receiver。但是在app实际组装过程中,并不是所有的app都要。有的想要注册这个receiver,有的不想要,怎么办。
官挡的推荐是从app模块的build.gradle里面注入。我们直接上代码

  1. 子模块的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.space365.basic">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application android:allowBackup="false">
        <receiver
            android:name=".BootReceiver"
            android:enabled="${boot}"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

这里${boot}作为占位变量

  1. app的build.gradle
defaultConfig {
        applicationId "com.space365.app.gate.shgbit"
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        versionCode 1
        versionName "1.0"
        manifestPlaceholders = [boot:true]
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

这里使用manifestPlaceholders变量定义变量数组

@soapgu soapgu added the 安卓 安卓 label Jun 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant