首先,使用Tinker的命令行工具类生成补丁,这个方式很简便.
在app的gradle中导入依赖:
//可选,用于生成application类 provided 'com.tencent.tinker:tinker-android-anno:1.9.2' //tinker的核心库 compile 'com.tencent.tinker:tinker-android-lib:1.9.2' //分包 compile 'com.android.support:multidex:1.0.1'在Manifest中添加:
<application <meta-data android:name="TINKER_ID" android:value="tinker_id_b168b32"/> </application>
创建TinkerManager--->用来管理添加补丁的操作
public class TinkerManager { /** * 是否安装 */ private static boolean isInstalled = false; private static ApplicationLike app; /** * 安装 */ public static void installTinbker(ApplicationLike applicationLike){ app=applicationLike; //判断是否安装 if(isInstalled){ return; } TinkerInstaller.install(applicationLike); isInstalled=true; } /** * 添加补丁包 * @param path */ public static void addPatch(String path){ if(isInstalled){ TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(),path); } } /** * 从ApplicationLike中拿到上下文 */ public static Context getApplicationContext(){ if(app!=null){ return app.getApplication().getApplicationContext(); } return null; } }
这是通过注解的方式用来生成AppLication的(在上边的东西都配置完后再添加注解,进行编译,别忘了编译,编译完会自动有个Application,这个是看不到的,直接在Manifest中注册)
@DefaultLifeCycle(application = ".MyTinkerApplication",flags = ShareConstants.TINKER_ENABLE_ALL) //别忘了加个点 public class CustomApplication extends DefaultApplicationLike{ public CustomApplication(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) { super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent); } @Override public void onBaseContextAttached(Context base) { super.onBaseContextAttached(base); //开启分包 MultiDex.install(base); //安装 TinkerManager.installTinbker(this); } }
在编译的时候,如果出错,复制如下代码,放入app的gradle中:
defaultConfig { javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } } }MainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private static final String SUFFIX=".apk"; private static final String TAG = "MainActivity"; private String patchPath=""; private Button btnOld; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnOld = findViewById(R.id.btn_old); btnOld.setOnClickListener(this); if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath(); patchPath=absolutePath.concat(File.separator); File file = new File(patchPath); Log.e(TAG, "onCreate: 路径名 "+patchPath); if(!file.exists()){ file.mkdir(); } } } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_old: Toast.makeText(getApplicationContext(),"原有的",Toast.LENGTH_SHORT).show(); TinkerManager.addPatch(getPatchPath()); break; } } /** * 返回补丁路径 * @return */ public String getPatchPath() { String path = patchPath.concat("out").concat(SUFFIX); Log.e(TAG,path); return path; } }
我的这里有俩按钮,打第一次包的时候有一个按钮,再添加一个按钮打更新功能的包,打完包后,用Tiker的命令行工具类生成补丁包,进行修复.
在这里,说到工具类,我也没有,自己下载,我查到的工具类下载是需要Money的,网上有.
做到以上操作,运用命令行更新就OK了,以下是gradle配置更新.