一点问题
6.0以上读写权限需要动态申请,但是AndroidManifest.xml文件中也必须写,要不然申请权限会报错。
增量更新
一般我们增量更新的使用方法是:
1.服务器生成差分包
2.客户端下载差分包
3.客户端进行差分包与旧版apk合并
4.提醒用户安装合并完成的新版apk
旧版apk路径:
开始写代码:
1.Binary diff/patch utility
http://www.daemonology.net/bsdiff/
点击here开始下载bsdiff-4.3.tar.gz
解压:
由于只在手机端做合并,所以拷贝bspatch.c到androidstudio的cpp目录下
2.下载bzip2
http://www.bzip.org/downloads.html
下载bzip2-1.0.6.tar.gz,解压:
有很多......,我们拷贝其中以.c和.h结尾的文件到cpp文件下
3.
拷贝完成后,大概是这个样子,拷贝完成后只发现了一个错
将#include 改为#include "bzlib.h"就可以了
4.修改CMakeList.txt
cmake_minimum_required(VERSION 3.4.1)
#添加多个目录的思路 指定一个变量 添加的时候使用变量值(my_c_path)
file(GLOB my_c_path src/main/cpp/*.c)
add_library( # Sets the name of the library.
patch-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${my_c_path}
)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_link_libraries( # Specifies the target library.
patch-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
我们将生成的so文件的名字改名为patch-lib,并将所有的.c文件都添加进来。
5.
将所有.c文件中的main函数的名字全部改掉,包括bspatch.c中的,
在MainActivity中写个native方法直接调用bspatch.c中修改后的main方法。
在MainActivity中添加本地方法:
public native int patch(String oldPath,String newPath,String diffPath);
在bspatch.c中填写对应的jni方法:
JNIEXPORT jint JNICALL
Java_com_example_huozhenpeng_diffandpatch_MainActivity_patch(JNIEnv *env, jobject instance,
jstring oldPath_, jstring newPath_,
jstring diffPath_) {
int ret=-1;
const char *oldPath = (*env)->GetStringUTFChars(env, oldPath_, 0);
const char *newPath = (*env)->GetStringUTFChars(env, newPath_, 0);
const char *diffPath = (*env)->GetStringUTFChars(env, diffPath_, 0);
int argc = 4;
char *argv[4];
argv[0] = "miduoPatch";
argv[1] = oldPath;
argv[2] = newPath;
argv[3] = diffPath;
//如果成功ret等于0
ret = bspatch_main(argc,argv);
(*env)->ReleaseStringUTFChars(env, oldPath_, oldPath);
(*env)->ReleaseStringUTFChars(env, newPath_, newPath);
(*env)->ReleaseStringUTFChars(env, diffPath_, diffPath);
return ret;
}
6.
package com.example.huozhenpeng.diffandpatch;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.EventLog;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView tv_patch;
private TextView tv_show;
private TextView tv_getpermission;
static {
System.loadLibrary("patch-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_show = (TextView) findViewById(R.id.sample_text);
tv_show.setText("旧版本版本应用");
tv_patch= (TextView) findViewById(R.id.tv_patch);
tv_patch.setOnClickListener(this);
tv_getpermission= (TextView) findViewById(R.id.tv_getpermission);
tv_getpermission.setOnClickListener(this);
}
public int getVersionCode (Context context, String packageName) {
PackageManager pm = context.getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(packageName, 0);
return info.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取已安装apk文件的原apk文件
* 如:/data/app/_.apk
*
* @param context
* @param packageName
* @return
*/
public static String getSourceApkPath(Context context, String packageName) {
if (TextUtils.isEmpty(packageName))
return null;
try {
ApplicationInfo appInfo = context.getPackageManager()
.getApplicationInfo(packageName, 0);
return appInfo.sourceDir;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
private Handler handler=new Handler()
{
@Override
public void handleMessage(Message msg) {
switch (msg.what)
{
case 0x01:
Toast.makeText(MainActivity.this,"合并完成",Toast.LENGTH_LONG).show();
installApk(MainActivity.this,newPath);
break;
case 0x02:
Toast.makeText(MainActivity.this,"合并失败",Toast.LENGTH_LONG).show();
break;
}
}
};
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native int patch(String oldPath,String newPath,String diffPath);
private String oldPath;
private String newPath;
private String diffPath;
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.tv_getpermission:
getPermission();
break;
case R.id.tv_patch:
if(getVersionCode(MainActivity.this,getPackageName().toString())==2)
{
Toast.makeText(MainActivity.this,"已经是最新版",Toast.LENGTH_LONG).show();
}
else
{
//获取差分包,合并差分包,我们把差分包放在sd卡下面
new Thread(new Runnable() {
@Override
public void run() {
oldPath=getSourceApkPath(MainActivity.this,getPackageName().toString());
newPath= Environment.getExternalStorageDirectory()+ File.separator+"patch.apk";
diffPath=Environment.getExternalStorageDirectory()+ File.separator+"diff.patch";
int result=patch(oldPath,newPath,diffPath);
if(result==0)
{
handler.sendEmptyMessage(0x01);
}
else
{
handler.sendEmptyMessage(0x02);
}
}
}).start();
}
break;
}
}
/**
* 安装Apk
*
* @param context
* @param apkPath
*/
public static void installApk(Context context, String apkPath) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + apkPath),
"application/vnd.android.package-archive");
context.startActivity(intent);
}
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
public void getPermission() {
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
}
测试结果
1.将build.gradle中的versionCode改为2,tv_show设置为“新版本版本应用”appnew.apk。
2.将build.gradle中的versionCode改为1,tv_show设置为“旧版本版本应用”appold.apk。
3.同上节,生成差分文件diff.patch
4.将diff.patch文件放入sd卡
5.将旧版本应用appold.apk安装到手机上(记住先获取读写权限)
6.点击合并差分包
可以查看合并后的新包
合并完成后自动安装: