BuglyDevTeam / Bugly-Android-Demo

Bugly Android SDK 使用例子
Apache License 2.0
810 stars 312 forks source link

参考该配置可以解决95%以上的接入与更新问题 #373

Open Fomovet opened 1 year ago

Fomovet commented 1 year ago

项目根目录下的build.gradle文件配置如下:

buildscript {
    repositories {
        google()
        mavenCentral()
      //使用要添加阿里云的maven源,如果使用Google默认的maven源将提示不存在
        maven {url 'https://maven.aliyun.com/repository/spring-plugin/'}
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.0'
        classpath "com.tencent.bugly:tinker-support:1.2.3"
    }
}

项目根目录下的gradle/wrapper/gradle-wrapper.properties文件配置如下:

#Mon Aug 15 20:56:15 CST 2022
distributionBase=GRADLE_USER_HOME
//gradle-6.5或gradle-6.7.1都可以
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

App目录下的build.gradle文件配置如下:

apply from: 'tinker-support.gradle'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.xxx.xxx"
        minSdkVersion 19  //这里是重点,填写>=21以上会出现各种问题各种坑
        targetSdkVersion 30
        versionCode 2
        versionName "1.0"
        multiDexEnabled true
}
//这里可以使用latest.release获取最新的
    dependencies {
    implementation 'com.tencent.bugly:crashreport_upgrade:1.6.1'
    implementation 'com.tencent.tinker:tinker-android-lib:1.9.14.24'
    implementation 'com.tencent.bugly:nativecrashreport:3.9.2'
    implementation "androidx.multidex:multidex:2.0.1"
}

}

App目录下的tinker-support.gradle文件配置如下:

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

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

/**
 * 此处填写每次构建生成的基准包目录
 */
def baseApkDir = "app-0819-16-43-40"

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

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

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

    //自动生成tinkerId
    autoGenerateTinkerId = true
    // 是否启用覆盖tinkerPatch配置功能,默认值false
    // 开启后tinkerPatch配置不生效,即无需添加tinkerPatch
    overrideTinkerPatchConfiguration = true

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

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

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

    // 构建基准包和补丁包都要指定不同的tinkerId,并且必须保证唯一性
//    tinkerId = "base-1.0.1"

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

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

    // 是否开启反射Application模式
    enableProxyApplication = false

    // 是否支持新增非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 {
    //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的分配
    }
}

Application和DefaultApplicationLike:

public class App extends TinkerApplication  {

    public App() {
        super(ShareConstants.TINKER_ENABLE_ALL, "com.xxx.xxx.AppLike",//com.xxx.xxx.AppLike改为你自己的AppLike
                "com.tencent.tinker.loader.TinkerLoader", false);
    }
}
public class AppLike extends DefaultApplicationLike {

    public static final String TAG = "Tinker.AppLike";

    public AppLike(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();
        //初始化BuglyId与模式
        Bugly.init(getContext(), Constants.APP_BUGLY_ID, true);
        // 设置开发设备,默认为false,上传补丁如果下发范围指定为“开发设备”,需要调用此接口来标识开发设备
        Bugly.setIsDevelopmentDevice(getApplication(), true);
        // 补丁回调接口
        Beta.betaPatchListener = new BetaPatchListener() {
            @Override
            public void onPatchReceived(String patchFile) {
                //Toast.makeText(getApplication(), "补丁下载地址" + patchFile, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDownloadReceived(long savedLength, long totalLength) {
//                Toast.makeText(getApplication(),
//                        String.format(Locale.getDefault(), "%s %d%%",
//                                Beta.strNotificationDownloading,
//                                (int) (totalLength == 0 ? 0 : savedLength * 100 / totalLength)),
//                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDownloadSuccess(String msg) {
                Toasty.success(getApplication(), "补丁包下载成功").show();
            }

            @Override
            public void onDownloadFailure(String msg) {
                Toasty.error(getApplication(), "补丁下载失败").show();
            }

            @Override
            public void onApplySuccess(String msg) {
                Toasty.success(getApplication(), "补丁应用成功").show();
            }

            @Override
            public void onApplyFailure(String msg) {
                Toasty.error(getApplication(), "补丁应用失败").show();
            }

            @Override
            public void onPatchRollback() {

            }
        };
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onBaseContextAttached(Context context) {
        super.onBaseContextAttached(context);
        MultiDex.install(context);
        Beta.installTinker(this);
    }

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

AndroidManifest.xml

 <activity
            android:name="com.tencent.bugly.beta.ui.BetaActivity"
            android:configChanges="keyboardHidden|orientation|screenSize|locale"
            android:theme="@android:style/Theme.Translucent" />
               <provider
                    android:name="com.tencent.bugly.beta.utils.BuglyFileProvider"
                    android:authorities="${applicationId}.fileProvider"
                    android:exported="false"
                    android:grantUriPermissions="true"
                    tools:replace="name,authorities,exported,grantUriPermissions">
                    <meta-data
                        android:name="android.support.FILE_PROVIDER_PATHS"
                        android:resource="@xml/provider_paths"
                        tools:replace="name,resource" />
                </provider>
YxmCute commented 1 year ago

AGP版本大于4.0以上我这边会找不到TinkerId

spysoos commented 7 months ago

不能开启反射Application模式了吗?