当前位置:网站首页>Tinker接入全流程---配置篇

Tinker接入全流程---配置篇

2022-08-11 05:28:00 余生爱静

接入全流程

配置Gradle

1、项目gradle
在这里插入图片描述
2、app moudle

apply plugin: 'com.android.application'

android {
    signingConfigs {
        release {
            keyAlias 'tinker'
            keyPassword 'tinker123456'
            storeFile file('tinkerDemo.jks')
            storePassword 'tinker123456'
        }
    }
    compileSdkVersion 28
    defaultConfig {
        applicationId "smt.com.tinkerdemo"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 0
        versionName "1.1"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
    // 编译选项
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    // recommend
    dexOptions {
        jumboMode = true
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    // 多dex配置
    implementation 'com.android.support:multidex:1.0.2'
    // 本地集成aar方式
    // compile(name: 'bugly_crashreport_upgrade-1.3.6', ext: 'aar')
    // 远程依赖集成方式(推荐)
    implementation 'com.tencent.bugly:crashreport_upgrade:1.3.6'
    // 指定tinker依赖版本(注:应用升级1.3.5版本起,不再内置tinker)
    implementation 'com.tencent.tinker:tinker-android-lib:1.9.9'
}
// 依赖插件脚本
apply from: 'tinker-support.gradle'

3、tinker-support.gradle配置

apply plugin: 'com.tencent.bugly.tinker-support'

def bakPath = file("${buildDir}/bakApk/")

/**
 * 此处填写每次构建生成的基准包目录
 */
def baseApkDir = "app-0716-17-36-08"   [email protected]

/**
 * 对于插件各参数的详细解析请参考
 */
tinkerSupport {

    // 开启tinker-support插件,默认值true
    enable = true

    // 指定归档目录,默认值当前module的子目录tinker
    autoBackupApkDir = "${bakPath}"

    // 是否启用覆盖tinkerPatch配置功能,默认值false
    // 开启后tinkerPatch配置不生效,即无需添加tinkerPatch
    overrideTinkerPatchConfiguration = true      [email protected]

    // 编译补丁包时,必需指定基线版本的apk,默认值为空
    // 如果为空,则表示不是进行补丁包的编译
    // @{link tinkerPatch.oldApk }
    baseApk = "${bakPath}/${baseApkDir}/app-release.apk"

    // 对应tinker插件applyMapping
    baseApkProguardMapping = "${bakPath}/${baseApkDir}/app-release-mapping.txt"

    // 对应tinker插件applyResourceMapping
    baseApkResourceMapping = "${bakPath}/${baseApkDir}/app-release-R.txt"

    // 构建基准包和补丁包都要指定不同的tinkerId,并且必须保证唯一性
    tinkerId = "patch-1.1.0"  ------[email protected]3

    // 构建多渠道补丁时使用
    // buildAllFlavorsDir = "${bakPath}/${baseApkDir}"

    // 是否启用加固模式,默认为false.(tinker-spport 1.0.7起支持)
    // isProtectedApp = true

    // 是否开启反射Application模式
    enableProxyApplication = false              [email protected]

    // 是否支持新增非export的Activity(注意:设置为true才能修改AndroidManifest文件)
    supportHotplugComponent = true

}

/**
 * 一般来说,我们无需对下面的参数做任何的修改
 * 对于各参数的详细介绍请参考:
 * https://github.com/Tencent/tinker/wiki/Tinker-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8D%97
 */
tinkerPatch {                                    --------------------------------------------------- @5
    //oldApk ="${bakPath}/${appName}/app-release.apk"
    ignoreWarning = false
    useSign = true
    dex {
        dexMode = "jar"
        pattern = ["classes*.dex"]
        loader = []
    }
    lib {
        pattern = ["lib/*/*.so"]
    }

    res {
        pattern = ["res/*", "r/*", "assets/*", "resources.arsc", "AndroidManifest.xml"]
        ignoreChange = []
        largeModSize = 100
    }

    packageConfig {
    }
    sevenZip {
        zipArtifact = "com.tencent.mm:SevenZip:1.1.10"
//        path = "/usr/local/bin/7za"
    }
    buildConfig {
        keepDexApply = false
        //tinkerId = "1.0.1-base"
        //applyMapping = "${bakPath}/${appName}/app-release-mapping.txt" //  可选,设置mapping文件,建议保持旧apk的proguard混淆方式
        //applyResourceMapping = "${bakPath}/${appName}/app-release-R.txt" // 可选,设置R.txt文件,通过旧apk文件保持ResId的分配
    }
}

说明

@1:此处为基准包的路径:作用很重要、很重要、很重要,它的作用就是补丁包是以这个路径下的apk文件为基准生成的补丁包

@2:如果设置为false,则@5处的配置不会生效;如果设置为true,则@5处的配置生效

@3:此处一定要保持唯一性,无论是和当前的基准包,还是之前的版本,一定要保持唯一性,唯一性、唯一性、唯一性,每次生成补丁包,每次、每次、每次,都要保持唯一

@4:如果设置为true,我们不需要继承TinkerApplication,只需要继承原生Application即可,Tinker会采用反射,修改Application;如果设置为false,我们需要自定义MyApplicationLike继承DefaultApplicationLikeMyApplication继承TinkerApplication

MyApplicationLike

public class MyApplicationLike extends DefaultApplicationLike {
    public MyApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Bugly.setIsDevelopmentDevice(getApplication(),true);     [email protected]
        // 这里实现SDK初始化,appId替换成你的在Bugly平台申请的appId
        Bugly.init(getApplication(), "98552355", true);
    }


    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);

        // TODO: 安装tinker
        Beta.installTinker(this);
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void registerActivityLifecycleCallback(
            Application.ActivityLifecycleCallbacks callbacks) {
        getApplication().registerActivityLifecycleCallbacks(callbacks);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        Beta.unInit();
    }
}

@6:此配置是设置当前设备为开发设备,对应bugly热更新更新补丁下发范围。**开发设备:**补丁下发只会下发给运行了调用该配置为true的设备;**全量设备:**是补丁分发给所有设备
在这里插入图片描述

MyApplication

public class MyApplication extends TinkerApplication {

    /**
     * current build.
     *
     */
    public MyApplication() {
        super(ShareConstants.TINKER_ENABLE_ALL, "smt.com.tinkerdemo.MyApplicationLike",
                "com.tencent.tinker.loader.TinkerLoader", false);
    }
}
原网站

版权声明
本文为[余生爱静]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u011557841/article/details/96161166