今天在添加了admob广告包进行打包时,出现了个Exception
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.lllegalArgumentException:already added La,查了好久,重要找到了篇靠谱的外文,解决了问题。
简单点说呢,就是admob包在打包的时候进行了混淆,但是没有去自定义package name,在你又添加了另外一个jar包,类里面有冲突的名称。我觉得和引用库工程和主干版本的资源重复差不多吧(这里会有一个默认优先级比较高,不会报错)。解决办法如下:在混淆的代码中添加上
-repackageclasses 'custom.package.here'
一个简单的打混淆的target
<!--Execute proguard class flies-->
<target name="optimize">
<jar basedir="${outdir-classes}" destfile="temp.jar"/>
<java jar="${proguard-home}/proguard.jar" fork="true" failonerror="true">
<jvmarg value="-Dmaximum.inlined.code.length=32"/>
<arg value="-injars temp.jar"/>
<arg value="-outjars optimized.jar"/>
<arg value="-libraryjars ${android-jar}"/>
<!-- <arg value="-libraryjars ${external-libs}/*.jar"/>-->
<arg value="-dontpreverify"/>
<arg value="-dontoptimize"/>
<arg value="-dontusemixedcaseclassnames"/>
<arg value="-repackageclasses 'packagename' "/>
<arg value="-allowaccessmodification"/>
<!--<arg value="-keep public class ${exclude-activity}"/>-->
<!--<arg value="-keep public class ${exclude-provider}"/>-->
<arg value="-keep public class * extends android.app.Activity"/>
<arg value="-keep public class * extends android.content.ContentProvider"/>
<arg value="-keep public class * extends android.view.View"/>
<arg value="-keep public class * extends android.preference.Preference"/> <arg value="-optimizationpasses 7"/>
<arg value="-verbose"/>
<arg value="-dontskipnonpubliclibraryclasses"/>
<arg value="-dontskipnonpubliclibraryclassmembers"/>
</java>
<delete file="temp.jar"/>
<delete dir="${outdir-classes}"/>
<mkdir dir="${outdir-classes}"/>
<unzip src="optimized.jar" dest="${outdir-classes}"/>
<delete file="optimized.jar"/>
</target>
下面是引用的外文:
One of our team members (thanks Shalin!) recently discovered that our Android library didn't play well with AdMob. As soon as our library and AdMob's library were added to the same project, this error showed up:
---------------------------------------------------------------------------------
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: ...
....
Conversion to Dalvik format failed with error 1
---------------------------------------------------------------------------------
Here is a stack overflow post that describes other users experiencing similar issues with other jar files. There are many suggested solutions, but none of them worked. After I poked around the AdMob Android library, the real issue became clear -- AdMob's library uses ProGuard to obfuscate its classes, but fails to declare a custom package name. Proguard will change your classes and its imports to look something like:
import A;
import B;
import C
etc
This means if you add other external libraries that are also guilty of this (like ours!), the classes will have conflicting names and you won't be build your project. We have since fixed our Android library with one line of (configuration) code, and we invite Google to do the same.
If you are developing a library, and use ProGuard to obfuscate your classes, then
Add this line to your proguard.cfg file:
-repackageclasses 'custom.package.here'
This will change your classes to custom.package.here.A, custom.package.here.B, etc. Now they'll no longer conflict!
参考:http://blog.crittercism.com/admob-unfriendly-jar-on-android