Android differentiated multi-channel performance optimization compilation method

I. Introduction:

A recent performance optimization app adds several third-party performance testing framework:

1, leakcanary (memory leak detection)

2, blockcanary (Processed detection means)

Because the project development android studio tool compiles but in the full compile time is compiled together with make way and the whole project, so it is necessary to automatically put the above performance testing tool incorporated into convenient AS compile the development of self-debugging, but make the compiled version can not take on top tool (tips and logs user experience), but also to meet all developers use the same code independently of each other, git libraries use the same code we can use the above function.

For the above situation requires differentiation compile the code, and is compatible with android studio and make a script to compile way.

 

Second, analysis

1.leakcanary blockcanary tools and methods used in the application only needs to be done to initialize class, does not involve the other code, it is necessary to differentiate distinguishing application.

2.application need to be declared in AndroidManifest.xml, so it is necessary to differentiate AndroidManifest.xml file.

3. Add leakcanary and blockcanary tool in two ways:

(1) Source download package or jar

(2) is introduced directly through gradle

Both methods can I choose the second, because mainly to add the above tools you need to develop your own use and for other people to use the compiler to distinguish, this summary uses multiple channels are packaged.

4.make compiler is to the customer or the test does not need to compile the above tools need to distinguish.

 

Third, the implementation

Based on the above analysis, we need to do the following work:

A. Create a separate directory for differences in the code (this directory can not be otherwise might be translated into other directories contained within the other code)

I am here mainly to the need to modify the above application files so few documents res, libs, etc. If you use the directory can also be added

1. because the project did not start rewriting application, so a new one, if you have rewritten before you need to add new code based on the original (ensure that the original function)

import android.app.Application;
import android.content.Context;

import com.github.moduth.blockcanary.BlockCanary;
import com.squareup.leakcanary.LeakCanary;


public class LauncherApplication extends Application {

    private static Context mContext;
    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();

        //耗时检测初始化
        BlockCanary.install(this, new AppContext()).start();
        //内存泄漏初始化
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);

    }

    public static Context getAppContext(){
        return mContext;
    }
}

 2. The following is the new file to configure the parameters BlockCanary


import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.android.launcher3.util.RyLogger;
import com.github.moduth.blockcanary.BlockCanaryContext;

public class AppContext extends BlockCanaryContext {
    private static final String TAG = "AppContext";

    @Override
    public String provideQualifier() {
        String qualifier = "";
        try {
            PackageInfo info = LauncherApplication.getAppContext().getPackageManager()
                    .getPackageInfo(LauncherApplication.getAppContext().getPackageName(), 0);
            qualifier += info.versionCode + "_" + info.versionName + "_YYB";
        } catch (PackageManager.NameNotFoundException e) {
            RyLogger.e(TAG, "provideQualifier exception"+e);
        }
        return qualifier;
    }

    @Override
    public int provideBlockThreshold() {
        //阻塞时间超过多少秒发通知
        return 1000;
    }

    @Override
    public boolean displayNotification() {
        return true;
    }

    @Override
    public boolean stopWhenDebugging() {
        return false;
    }
}

3. Finally AndroidManifest.xml file also needs to maintain the existing code and modify the application, the main application file is replaced

 <application
        android:name=".LauncherApplication"
        android:backupAgent=".LauncherBackupAgent"
        android:fullBackupContent="@xml/backupscheme"
        android:fullBackupOnly="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/ic_launcher_home"
        android:label="@string/derived_app_name"
        android:theme="@style/AppTheme"
        android:largeHeap="@bool/config_largeHeap"
        android:networkSecurityConfig="@xml/network_security_config"
        android:restoreAnyVersion="true"
        android:supportsRtl="true">

II. Modify build.gradle

1. The main difference is to add code compiler, the new code

 //新增目录需要编译内容,src,res,libs,manifest
        debug {
            java.srcDirs = ['debug_src']
            jniLibs.srcDirs = ['libs']
            res.srcDirs = ['res']
            manifest.srcFile "debug_src/AndroidManifest.xml"
        }
sourceSets {

        main {
            res.srcDirs = ['res']
            java.srcDirs = ['src']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
            manifest.srcFile 'AndroidManifest-common.xml'
            proto {
                srcDir 'protos/'
                srcDir 'proto_overrides/'
            }
        }

        //新增目录需要编译内容,src,res,libs,manifest
        debug {
            java.srcDirs = ['debug_src']
            jniLibs.srcDirs = ['libs']
            res.srcDirs = ['res']
            manifest.srcFile "debug_src/AndroidManifest.xml"
        }

        androidTest {
            res.srcDirs = ['tests/res']
            java.srcDirs = ['tests/src']
            manifest.srcFile "tests/AndroidManifest-common.xml"
        }

        androidTestDebug {
            manifest.srcFile "tests/AndroidManifest.xml"
        }

        aosp {
            java.srcDirs = ['src_flags', "src_ui_overrides"]
        }


}

2. Add introduced way

Two currently used way jar package when one is introduced Implementation

dependencies {
    implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')

    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.1'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.1'
    // Optional, if you use support library fragments:
    debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.1'

}

Three, make the difference compiled file

1. If you need to add new tools to make the following work needs to be done

include $(CLEAR_VARS)
LOCAL_MODULE :=  blockcanary #别名
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_SRC_FILES := libs/blockcanary.jar #包名
LOCAL_UNINSTALLABLE_MODULE := true
#LOCAL_SDK_VERSION := current
LOCAL_PRIVATE_PLATFORM_APIS:=true
include $(BUILD_PREBUILT)


include $(CLEAR_VARS)
LOCAL_MODULE := libxypatch3
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES_32 := libs/armeabi/libxypatch.so
LOCAL_SRC_FILES_64 := libs/arm64-v8a/libxypatch.so
LOCAL_MULTILIB := both
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_SUFFIX := .so
include $(BUILD_PREBUILT)

LOCAL_STATIC_JAVA_LIBRARIES +=  blockcanary \ libxypatch3 #加入静态库
LOCAL_SRC_FILES := \
    $(call all-java-files-under, src) \
    $(call all-java-files-under, src_ui_overrides) \
    $(call all-java-files-under, src_flags) \
    $(call all-proto-files-under, protos) \
    $(call all-proto-files-under, debug_src/src) #新增


LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/debug_src/res #新增

LOCAL_FULL_LIBS_MANIFEST_FILES := $(LOCAL_PATH)/debug_src/AndroidManifest-common.xml #新增

2. If you need to add to make the compilation

The need to pay attention to the new debug_src directory before compiling the directory that contains If you have already included the need to move to a different directory, to prevent being compiled

 

to sum up:

Differences compiler to note:

1. Is the difference of the directory is not independent of other compilation included

2. The corresponding source code, resources, libs, AndroidManifest.xml, whether to modify the differentiation and arranged inside the build.gradle

3. Does the new features will affect the original function

4. Add some differentiation Write a comment

Published 92 original articles · won praise 27 · views 90000 +

Guess you like

Origin blog.csdn.net/zhuxingchong/article/details/103281509