Everyday ailments

Depressed error

Third party warehouse exception added to AS (possibly earlier after 4.2)

  • Error message:Build was configured to prefer settings repositories over project repositories but repository
  • Solution: Google moved the new tripartite library ~~~ so the new tripartite library configuration should be put insetting.gradle, as follows:

The Gradle repository reported an unsafe message

  • Error message:Using insecure protocols with repositories.....
  • Solution: Add labelsallowInsecureProtocol = trueAs shown in the following
Maven {allowInsecureProtocol = true url '-- '}Copy the code

Import UI girl gave SVG error

  • Error message:SVG Width (0) and height (0) cannot be <= 0
  • Solution: in (app) build. GradledefaultConfig tags to add the following configuration
defaultConfig {
    vectorDrawables.useSupportLibrary = true
}
Copy the code

Weird and normal

The signature file does not have an MD5 value

  • Cause: The new JDK does not support MD5
  • Solution 1:
  1. downloadJdk1.8.0 _201(The version I downloaded)
  2. Configuring environment Variables
  3. Rerun theKeytool -v -list -keystore [full path to your signature file]
  • Solution 2:
  1. Go to wechat open platform to download the signature tool
  2. After installation, enter the name of the package to be signed (the APP to be signed and the signature tool APP must be installed on the same mobile phone).

Special cases Fragment overlap

  • Cause: When an Activity is abnormally rebuilt, it will go through a normal life cycle and automatically restore the Fragment it was attached to. This will cause duplicate fragments to appear
  • Solution: Reuse reconstructed fragments
Add (R.I.D. rameLayout, fragment, "corresponding to unique Tag") //2. Override fun onCreate(savedInstanceState: Bundle?) { if (savedInstanceState ! = null) {/ / query reusable fragments, sentenced to empty after direct reuse val fragments. = supportFragmentManager findFragmentByTag ()}}Copy the code

Static desktop shortcut icon

  • Problem Description: After creating a static desktop icon, click the icon to open the corresponding Activity. Return Returns directly to the desktop.
  • Solution: Add an intent tag to open the home page and then jump to the Activity with the corresponding shortcut icon. As follows:
<shortcut android:enabled="true" android:icon="@drawable/ic_collections_black_24dp" android:shortcutDisabledMessage="@string/layout_collect" android:shortcutId="collection_id" android:shortcutLongLabel="@string/layout_collect" android:shortcutShortLabel="@string/layout_collect"> // Add an Intent tag to open the MainActivity, Then open the target Activity < intent android: action = "android. Intent. Action. The VIEW" android: targetClass = "your MainActivity (full path)" Android: targetPackage = "your package name" / > < intent android: action = "android. Intent. Action. The VIEW" Android :targetPackage=" your package name "/> </shortcut>Copy the code

Paint setting transparency does not work

  • Problem cause: view the source code found in the setting of the color of the mColor parameter for direct assignment, and set the transparency is to read the mColor parameter after RGB assignment
  • Solution: First set the color and then set the transparency, like this:
// Set the color mTextPaint!! .color = color. WHITE // Set transparency (0~255) mTextPaint!! .alpha = 100Copy the code

The Material Tablayout cannot be fully laid on the tablet

  • The width of Tablayout is set to match_parent and tabMode is set to fixed. In the mobile phone normal spread full display, but in the tablet can not spread full, just center display.
  • Solution: Add the following labels (based on Material :1.2.1)
android:layout_width="match_parent"
app:tabMode="fixed"
app:tabGravity="fill"
Copy the code

The freshness of the edition

The Android 7.0

Android7.0 split screen life cycle mentality

Split screen has a life cycle, specific application needs. Delay adaptation can be configured in the manifest file as follows:

android:resizeableActivity="false"
Copy the code

The Android 8.0

The front desk Service

Add permissions < USES – permission android: name = “android. Permission. FOREGROUND_SERVICE” / >

class SimpleService : Service() {
    override fun onCreate() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notification: Notification =Notification.Builder(this, "0-0")
                .setContentTitle("Title")
                .setContentText("Content")
                .setSmallIcon(R.mipmap.ic_launcher)
                .build()
            val notificationChannel =NotificationChannel("0-0", "CHANNEL_ONE_NAME", NotificationManager.IMPORTANCE_MIN)
            val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            manager.createNotificationChannel(notificationChannel)
            startForeground(1, notification)
        }
    }
}
Copy the code

The Android 9.0

Network Request problem

  • Scheme 1: Set the request mode to HTTPS
  • Solution 2: Do the following

Create the netword_security file in the res -> XML directory

<? The XML version = "1.0" encoding = "utf-8"? > <network-security-config xmlns:android="http://schemas.android.com/apk/res/android"> <base-config cleartextTrafficPermitted="true" /> </network-security-config>Copy the code

Add the configuration under the manifest file Application TAB

<application
    ...
    android:networkSecurityConfig="@xml/netword_security""
    ...
    >
    <activity
        ...
    </activity>
</application>
Copy the code

The Android 10.0

Quick adaptation on Android10.0

Want to fit but don’t have time… Then be lazy! The list file is configured as follows:

android:requestLegacyExternalStorage="true"
Copy the code

Daily maintenance

tip

TextView Medium text weight (ultimately dominated by IOS)

Set the following properties to approximate the effect of Medium word weight…

TextView.getPaint().setFakeBoldText(true);
Copy the code

TextView content automatically scrolls to the last line (slidable TextView)

Without further ado, get right to the code. (There seems to be nothing to say ~~~)

Private val mRect = Rect() private val mStringBuild = StringBuilder() private fun addMessage(MSG){// Add content Mstringbuild. appendLine(MSG) mtextView.text = mStringBuild var contentHeight = 0 for (I in 0 until mTextView.lineCount) { mRect.setEmpty() mTextView.getLineBounds(i, MRect) contentHeight += mrect.height ()} Remember to subtract) val textViewDisplayHeight = mTextView.height if (contentHeight > textViewDisplayHeight) {// Slide the difference mTextView.scrolly  = (contentHeight - textViewDisplayHeight) } }Copy the code

Multi-context configuration variables (notably interfaces)

The key tagbuildConfigField()

  1. Configure default variables (Debug environment), all in app-build.gradle
DefaultConfig {applicationId "com.night.instaprodemo" minSdk 23 targetSdk 31 versionCode 1 versionName "1.0" TestInstrumentationRunner "androidx test. Runner. AndroidJUnitRunner" / / configure default variable buildConfigField (' String ', 'SERVICE_URL', 'defaultConfig data ')}Copy the code
  1. Configure environment variables
BuildTypes {release {// configure the corresponding environment variable buildConfigField('String', 'SERVICE_URL', '"https://juejin.cn/"') minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), SigningConfig SigningConfigs. release} Develop {buildConfigField('String', 'SERVICE_URL', '" https://juejin.cn/post/6983162979252437005 ") minifyEnabled false / / configure corresponding environment signature signingConfig signingConfigs. Release}}Copy the code
  1. use

Call buildConfig.service_URL where needed

Custom generated APK name

Add the following at the end of the Appbuild. gradleAndroid TAB

def getTime() { return new Date().format("yyyyMMdd_HH-mm", TimeZone. GetDefault ()} android {/ / -- -- -- -- -- -- -- -- -- -- -- -- -- I'm ellipses -- -- -- -- -- -- -- -- -- -- -- -- -- buildTypes {/ / -- -- -- -- -- -- -- -- -- -- -- -- -- I'm ellipses -- -- -- -- -- -- -- -- -- -- -- -- --} / / -- -- -- -- -- -- -- -- -- -- -- -- -- I'm ellipses -- -- -- -- -- -- -- -- -- -- -- -- - android. ApplicationVariants. All {variant - > the variant. The outputs. All {the if (' debug '= = variant.buildType.name) { outputFileName = "debug.apk" } if ('release' == variant.buildType.name) { outputFileName = "release-v${variant.versionName}-${getTime()}.apk" } } } }Copy the code

The special control

  • TextClock fast implementation of digital clock, please see

Convenient tool

  • GsonFormat JAR package to download
  • Icon compression
  • Various cover art
  • Typora- Good writing helper
  • Translation-AS English – Chinese Translation plug-in