我正在尝试使用proguard混淆使用JAXB的Java应用程序。该代码(使用公共库)是

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.cansas.cansas1d.SASdataType;
import org.cansas.cansas1d.SASentryType;
import org.cansas.cansas1d.SASentryType.Run;
import org.cansas.cansas1d.SASrootType;


public class CansasReader {

    private static final String RES_DIR = "/Users/paul/Documents/sample data/ZZ Non-2D formats/canSAS/";
    private static final String JAXB_CONTEXT = "org.cansas.cansas1d";

    private JAXBContext jc;
    private JAXBElement<SASrootType> xmlJavaData;

    /**
     * Open a cansas1d 1D file
     *
     * @param xmlFile
     * @return SasRootType object (saves a second method call)
     * @throws JAXBException
     * @throws FileNotFoundException
     */
    @SuppressWarnings( "unchecked" )
    public SASrootType loadXML(String xmlFile) throws JAXBException, FileNotFoundException {
        jc = JAXBContext.newInstance(JAXB_CONTEXT);      // reference the namespace:
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        InputStream in = new FileInputStream(new File(xmlFile));
        xmlJavaData = (JAXBElement<SASrootType>) unmarshaller.unmarshal(in);
        return xmlJavaData.getValue();
    }

    /**
     * Describe the XML data in more detail than toString() method
     * and print to stdout.
     */
    public void full_report(SASrootType srt) {
        for ( SASentryType entry : srt.getSASentry() ) {
            System.out.println("SASentry");
            System.out.printf("Title:\t%s\n", entry.getTitle());
            List<SASentryType.Run> runs = entry.getRun();
            System.out.printf("#Runs:\t%d\n", runs.size());
            for ( Run run : runs ) {
                System.out.printf("Run@name:\t%s\n", run.getName());
                System.out.printf("Run:\t%s\n", run.getValue());
            }
            List<SASdataType> datasets = entry.getSASdata();
            System.out.printf("#SASdata:\t%d\n", entry.getSASdata().size());
            for ( SASdataType sdt : datasets ) {
                System.out.printf("SASdata@name:\t%s\n", sdt.getName());
                System.out.printf("#points:\t%d\n", sdt.getIdata().size());
            }
            System.out.println();
        }
    }



    /**
     * simple representation of data in memory
     */
    public String toString(SASrootType sasRoot) {
        return "SASentry elements: " + sasRoot.getSASentry().size();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("class: " + CansasReader.class.getCanonicalName());
        String[] fileList = {
            RES_DIR + "cs_collagen.xml",
            RES_DIR + "1998spheres.xml",
            "cannot_find_this.xml"
        };
        for (String xmlFile : fileList) {
            System.out.println("\n\nFile: " + xmlFile);
            try {
                CansasReader rdr = new CansasReader();
                SASrootType srt = rdr.loadXML(xmlFile);
                System.out.println(rdr.toString(srt));
                rdr.full_report(srt);
                System.out.println("the end.");

            } catch (FileNotFoundException e) {
                System.out.println("File not found:" +  xmlFile);
            } catch (JAXBException e) {
                System.out.println("Could not open (unmarshall) XML file" +  xmlFile);
            }
        }
    }

}


Proguard配置文件是

-injars  CansasReader.jar
-outjars CansasReader_ob.jar
-libraryjars <java.home>/lib/rt.jar
-printmapping CansasReader.map
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable, *Annotation*, EnclosingMethod, Signature
-keep public class * {
    public protected *;
}
-repackageclasses ''
-overloadaggressively
-defaultpackage ''
-allowaccessmodification
-keeppackagenames org.cansas.cansas1d
-keepparameternames
-keep,includedescriptorclasses public  class org.cansas.cansas1d.package-info
-keep,includedescriptorclasses public class org.cansas.cansas1d.FloatUnitType
(and the same for every other class in the package)
-keep,includedescriptorclasses public class CansasReader


混淆之前,输出是预期的。混淆后,我没有任何异常,但预期的数据字段为null。例如,输出


  文件:/ Users / paul / Documents / sample data / ZZ Non-2D
  format / canSAS / cs_collagen.xml SASentry元素:1个SASentry
  标题:干燥的雏鸡胶原蛋白,d = 673 A,6531 eV,X6B
  
  运行:1运行@名称:运行:1994年9月19日上午01:41:02
  
  SASdata:1个SASdata @ name:
  
  积分:125


混淆后变为:


  文件:/ Users / paul / Documents / sample data / ZZ Non-2D
  format / canSAS / cs_collagen.xml SASentry元素:1个SASentry
  标题:null
  
  运行次数:0
  
  SAS数据:0


请注意,按照此列表上的其他建议,我确实在配置文件中同时包含了签名和* Annotation *。这样可以防止我之前遇到的类型转换异常,但是xml文件仍未正确读取。任何建议表示赞赏!

最佳答案

-keep public class your.package.path.to.jaxb.classes.** {
  public protected private *;
}


这应该有所帮助。我想您已经找到了解决方案。最好的祝福。拉尔夫

09-30 17:23