Android学习——打包

来源:互联网 发布:js将数据导出成excel 编辑:程序博客网 时间:2024/05/16 09:53

1.字符串资源多国语言版本的出错问题

"Description    Resource    Path    Location    Type

"your_string_name" is not translated in af, am, ar, be, bg, ca, cs, da, de, el, en-rGB, es, es-rUS, et, fa, fi, fr, hi, hr, hu, in, it, iw, ja, ko, lt, lv, ms, nb, nl, pl, pt, pt-rPT, ro, ru, sk, sl, sr, sv, sw, th, tl, tr, uk, vi, zh-rTW, zu    strings.xml    /projectname/res/values    line 44    Android Lint Problem

     如果你遇到类似上面的错误,那你要么想办法编制提示到的所有语言版本,一个不漏地将所有字符串资源翻译一遍,要么就看看调整项目属性项怎么解决。

    其实当这个错误发生时一般都会出现这个警告对话框,仔细看这个对话框的信息,不难发现,我们要检查preference页,可我也是初学,不太熟,找了好一会儿才找到。

    你右击左侧栏项目名称,在弹出菜单中选最后一项"Properties"打开。在下面页面中选择 “Android Lint Preference”,然后将 “MissingTranslation” 从 “fatal” 改为 “warning"或“warning"以下的选项。


    当然,在发布版本之前最好还是用胶严厉的选项检查一下比较好。

    另外,在“Lint warnings” 信息框里,会有更多类似下面的提示,可以让你更好的控制多国语言问题。


Issue: Checks for incomplete translations where not all strings are translated
Id: MissingTranslation

If an application has more than one locale, then all the strings declared in one language should also be translated in all other languages.

If the string should not be translated, you can add the attribute translatable="false" on the <string> element, or you can define all your non-translatable strings in a resource file called donottranslate.xml. Or, you can ignore the issue with a tools:ignore="MissingTranslation" attribute.

By default this detector allows regions of a language to just provide a subset of the strings and fall back to the standard language strings. You can require all regions to provide a full translation by setting the environment variable ANDROID_LINT_COMPLETE_REGIONS.
2.proguard-project.txt  如果发现项目中只有这个,而没有proguard.cfg。其实这两个是一样的,都可以在里面添加和修改内容,所有不需要纠结的手动加上一个progurad.cfg。

3.在引入第三方包可能会报一些错误

引入android-support-v4.jar时出现以下错误

修改此错误的方式为在proguard.cfg中加上

[java] view plaincopy
  1. -dontwarn android.support.v4.**  
如果是其他的版本,修改对应的版本号


4.

这是在工程中的proguard-project.txt中发现的

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

 
从脚本中可以看到,混淆中保留了继承自Activity、Service、Application、BroadcastReceiver、ContentProvider等基本组件以及com.android.vending.licensing.ILicensingService,
并保留了所有的Native变量名及类名,所有类中部分以设定了固定参数格式的构造函数,枚举等等。(详细信息请参考<proguard_path>/examples中的例子及注释。)
让proguard.cfg起作用的做法很简单,就是在eclipse自动生成的default.properties文件中加上一句“proguard.config=proguard.cfg”就可以了
完整的default.properties文件应该如下:
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.

# Project target.
target=android-15
proguard.config=proguard.cfg


Android代码混淆,如何过滤掉反射的R文件及第三方包?
解决方案:在Proguard.cfg方件中添加以下设定:

  • 过滤R文件的混淆:

-keep class **.R$* {   *;  }

  • 过滤第三方包的混淆:

-keep class packagename.** {*;}(其中packagename为第三方包的包名)

Android导入第三方jar包,proguard混淆脚本(屏蔽警告,不混淆第三方包)
最近1个项目中 需要导入移动MM的第三方计费包,混淆时用到了如下脚本,可屏蔽警告,不混淆第三方包指定内容。
非常有效

proguard.cfg 文件

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-ignorewarnings //这1句是屏蔽警告,脚本中把这行注释去掉
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
//这1句是导入第三方的类库,防止混淆时候读取包内容出错,脚本中把这行注释去掉
-libraryjars libs/mmbilling.jar 

-dontwarn  //dontwarn去掉警告
-dontskipnonpubliclibraryclassmembers

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
    native <methods>;
}
-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

//这4句是不混淆第三方包中的指定内容,脚本中把这行注释去掉 -keep class com.ccit.** {*; }   
-keep class ccit.** { *; }
-keep class com.aspire.**
-keep class mm.vending.**

ttttt

ProGuard是一个免费的java类文件压缩,优化,混淆器.它探测并删除没有使用的类,字段,方法和属性.它删除没有用的说明并使用字节码得到最大优化.它使用无意义的名字来重命名类,字段和方法.
 
ProGuard的使用是为了:
 
1.创建紧凑的代码文档是为了更快的网络传输,快速装载和更小的内存占用.
2.创建的程序和程序库很难使用反向工程.
3.所以它能删除来自源文件中的没有调用的代码
4.充分利用java6的快速加载的优点来提前检测和返回java6中存在的类文件.
 
参数:
 
-include {filename}    从给定的文件中读取配置参数
 
-basedirectory {directoryname}    指定基础目录为以后相对的档案名称
 
-injars {class_path}    指定要处理的应用程序jar,war,ear和目录
 
-outjars {class_path}    指定处理完后要输出的jar,war,ear和目录的名称
 
-libraryjars {classpath}    指定要处理的应用程序jar,war,ear和目录所需要的程序库文件
 
-dontskipnonpubliclibraryclasses    指定不去忽略非公共的库类。
 
-dontskipnonpubliclibraryclassmembers    指定不去忽略包可见的库类的成员。
 
 
保留选项
-keep {Modifier} {class_specification}    保护指定的类文件和类的成员
 
-keepclassmembers {modifier} {class_specification}    保护指定类的成员,如果此类受到保护他们会保护的更好
 
-keepclasseswithmembers {class_specification}    保护指定的类和类的成员,但条件是所有指定的类和类成员是要存在。
 
-keepnames {class_specification}    保护指定的类和类的成员的名称(如果他们不会压缩步骤中删除)
 
-keepclassmembernames {class_specification}    保护指定的类的成员的名称(如果他们不会压缩步骤中删除)
 
-keepclasseswithmembernames {class_specification}    保护指定的类和类的成员的名称,如果所有指定的类成员出席(在压缩步骤之后)
 
-printseeds {filename}    列出类和类的成员-keep选项的清单,标准输出到给定的文件
 
压缩
-dontshrink    不压缩输入的类文件
 
-printusage {filename}
 
-whyareyoukeeping {class_specification}    
 
优化
-dontoptimize    不优化输入的类文件
 
-assumenosideeffects {class_specification}    优化时假设指定的方法,没有任何副作用
 
-allowaccessmodification    优化时允许访问并修改有修饰符的类和类的成员
 
混淆
-dontobfuscate    不混淆输入的类文件
 
-printmapping {filename}
 
-applymapping {filename}    重用映射增加混淆
 
-obfuscationdictionary {filename}    使用给定文件中的关键字作为要混淆方法的名称
 
-overloadaggressively    混淆时应用侵入式重载
 
-useuniqueclassmembernames    确定统一的混淆类的成员名称来增加混淆
 
-flattenpackagehierarchy {package_name}    重新包装所有重命名的包并放在给定的单一包中
 
-repackageclass {package_name}    重新包装所有重命名的类文件中放在给定的单一包中
 
-dontusemixedcaseclassnames    混淆时不会产生形形色色的类名
 
-keepattributes {attribute_name,...}    保护给定的可选属性,例如LineNumberTable, LocalVariableTable, SourceFile, Deprecated, Synthetic, Signature, and InnerClasses.
 
-renamesourcefileattribute {string}    设置源文件中给定的字符串常量

因为我们开发的是webwork+spring+hibernate的架构的项目,所有需要很详细的配置。(经过n次失败后总结)

Example:

    -injars <project>.jar

    -outjars <project>_out.jar

    -libraryjars <java.home>/lib/rt.jar

    -libraryjars <project.home>/webroot/WEB-INF/lib/webwork.jar

    .......

    # 保留实现Action接口类中的公有的,友好的,私有的属性 和 公有的,友好的方法。其它的全部压缩,优化,混淆。

    # 因为配置文件中的类名是一个完整的类名,如果经过处理后就有可能找不到这个类。

    # 属性是jsp页面所需要的,如果经过处理jsp页面就无法得到action中的数据。

    -keep public class * implements com.opensymphony.xwork.Action{

        public protected private <fields>;

        public protected <methods>;

    }

 

    # 保留实现了Serializable接口类中的公有的,友好的,私有的成员(属性和方法)

    # 这个配置主要是对应实体类的配置。

    -keep public class * implements java.io.Serializable{

        public protected private *;

    }

    ......

   # 关于配置的解释已说了很多了,下面的配置自已可以尝试。

然后在命行端输入:proguard @配置文件

(proguard是我自已写的批处理,你也可以自已写一下。)

 

总结:

    proguard在处理web application方面还行,但不是很完美。如果的框架设计的不是很好的话配置加多不说,处理效果也是很好。

原创粉丝点击