安全 发布相关问题

1,401 阅读2分钟

解析包时出现错误

  • 可能问题:包名有大写
  • 可能问题:下载下来的apk与服务器的apk大小不一样
/** 
 * @param path下载地址 
 * @param filePath存储路径 
 * @param progressDialog进度条 
 * @return 
 * @throws Exception 
 */  
public static File getFile(String path,String filePath,ProgressDialog progressDialog) throws Exception{
		URL url = new URL(path);  
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
    connection.setConnectTimeout(2000);  
    connection.setRequestMethod("GET");  
    if(connection.getResponseCode() == 200){  
    		int total = connection.getContentLength();  
        progressDialog.setMax(total);
        
        InputStream is = connection.getInputStream();//获得socket输入流  
        File file = new File(filePath);  
        FileOutputStream fos = new FileOutputStream(file);//file输出流  
        byte[] buffer = new byte[1024];  
        int len;  
        int progress = 0;  
        while((len = is.read(buffer)) != -1){  
        		fos.write(buffer);  
            progress += len;  
            progressDialog.setProgress(progress);  
        }  
        fos.flush();  
        is.close();  
        fos.close();  
        connection.disconnect();  
        return file;  
    }
    return null;  
}

解决:

byte[] buffer = new byte[1024];

这行代码,把1024改成了128,又改成了64,结果就没问题了。

  • 可能问题:已经安装完毕,点“返回“报错是代码中在安装完毕后加了回调 startActivityForResult..

解决:在安装完毕后加 return

// 安装apk
private void installApk(File file) {
	Intent intent = new Intent(Intent.ACTION_VIEW);
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
		intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
		Uri contentUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".updateProvider", file);
		intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
		//兼容8.0-26
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			AppUtils.installApp(file, BuildConfig.APPLICATION_ID + ".updateProvider");
			//兼容8.0以上的安装完毕需要加return,否则有安装成功但是弹出“解析包失败”的页面来误导用户
			return;
		}
	} else {
		intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	}
	activity.startActivityForResult(intent, 999);
}

安装失败

  • 失败原因:应用组件的命名与已安装应用有冲突

    • 可能是包名问题(已安装 app 有相同包名)

      解决:需要重新定一个包名,同时在相关第三方平台上要改掉包名

    • 可能是provider的authorities问题(已安装的 app 有相同的 authorities)

    解决:正在安装的 app 的 authorities 改另一个名称(随便改,可以带上 applicationId)

AS打包卡在app:transformClassesAndResourcesWithR8ForRelease

卡在app:transformClassesAndResourcesWithR8ForRelease很长时间一直不能生成包,有时候产生java.lang.OutOfMemoryError: GC overhead limit exceeded错误。而且编译打包时偶尔会报Error:java.lang.OutOfMemoryError

解决(在 gradle.properties中添加):

# 编译时使用守护进程
org.gradle.daemon=true
#JVM 最大允许分配的堆内存,按需分配
org.gradle.jvmargs=-Xmx16896m -XX:MaxPermSize=4096m  -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# 使用并行编译
org.gradle.parallel=true
org.gradle.configureondemand=true
#启用新一代Dex编译器D8
android.enableD8=true
#启用gradle缓存
org.gradle.caching=true

运行时报警告:

WARNING: The following project options are deprecated and have been removed:
android.enableAapt2
This property has no effect, AAPT2 is now always used.

解决方案: 移除gradle.propertiesandroid.enableAapt2=true

升级到 Android9.0遇到的问题

java.lang.VerifyError: Verifier rejected class d.w.b.f.a: java.lang.String d.w.b.f.a.a(java.lang.String) failed to verify: java.lang.String d.w.b.f.a.a(java.lang.String): [0x36] 'this' argument 'Precise Reference: org.apache.commons.codec.binary.Base64' not instance of 'Reference: org.apache.commons.codec.binary.BaseNCodec' (declaration of 'd.w.b.f.a' appears in /data/app/app.laidianyi.yyldy-naDf47h1LBlsvDDqMvd1rw==/base.apk!classes3.dex)
        at d.w.b.f.a.a(AESHelper.java:39)
        at d.w.b.b.b.a(RemoteClient.java:119)
        at b.a.b.a.b(RequestApi.java:2739)
        at b.a.i.a.k.b(IsExistIMAccount.java:58)
        at app.laidianyi.view.login.WelcomeActivity.onCreate(WelcomeActivity.java:95)
        at android.app.Activity.performCreate(Activity.java:7458)
        at android.app.Activity.performCreate(Activity.java:7448)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3409)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

解决:

public static String AESEncrypt(String plainText) {
    String encryptedValue = null;
    try {
        Key key = new SecretKeySpec(keyWord.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        byte[] byteContent = plainText.getBytes("utf-8");
        try {
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(_key1));
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        byte[] encValue = cipher.doFinal(byteContent);
        //把上面这句注释掉,改成底下那句
        //encryptedValue = new String(new Base64().encode(encValue));
        encryptedValue = new String(Base64.encodeBase64(encValue));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encryptedValue;
}