简体   繁体   中英

proguard issue in intellij plugin development

I am trying to use ProGuard for obfuscation of my intellij plugin.

I am adding some internal fileTemplates to IntelliJ for creating new files. <RelatedTemplateName> is the filename I added to resources/fileTemplates/internal/<RelatedTemplateName>.ft

So far so good, except...

In Obfuscated plugin: IntelliJ code couldn't find some resource file inside my plugin.

In non-obfuscated plugin: everything works fine.

I once thought proguard changed my resource files, but I don't think any resource file was changed according to this link , because I don't have such options added to my proguard.pro file

Would anyone help me to find out the root cause of this issue? Thanks

Is it because of proguard changed some other classes related to this?

Other related infomation below

Exception

java.lang.Throwable: Template not found: <RelatedTemplateName>
    at com.intellij.openapi.diagnostic.Logger.error(Logger.java:145)
    at com.intellij.ide.fileTemplates.impl.FileTemplateManagerImpl.getTemplateFromManager(FileTemplateManagerImpl.java:294)
    at com.intellij.ide.fileTemplates.impl.FileTemplateManagerImpl.getJ2eeTemplate(FileTemplateManagerImpl.java:279)
    at com.intellij.ide.fileTemplates.impl.FileTemplateManagerImpl.getInternalTemplate(FileTemplateManagerImpl.java:242)
    at XXX.XXX.XXX.createNewFile(MyNewFileAction.java:104)

my proguard configuration:

related part of build.gradle file:

def getIDEAPath(){
    if(intellij.localPath!=null && !intellij.localPath.isEmpty()){
        return intellij.localPath
    }
    def ideTempPath = file("$gradle.gradleUserHomeDir/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIC/$intellij.version")
    def ideBasePath = ideTempPath;
    ideTempPath.traverse([maxDepth: 2, type: groovy.io.FileType.DIRECTORIES]) {
        it ->
            if (it.absolutePath.contains("lib")) {
                ideBasePath = file(it.absolutePath);
            };
    }
    return ideBasePath.parent
}


task myProguardTask(type: proguard.gradle.ProGuardTask, dependsOn: jar) {
    printmapping "build/mapping.txt"
    configuration 'proguard.pro'
    // Automatically handle the Java version of this build.
    if (System.getProperty('java.version').startsWith('1.')) {
        // Before Java 9, the runtime classes were packaged in a single jar file.
        libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
    } else {
        // As of Java 9, the runtime classes are packaged in modular jmod files.
        libraryjars "${System.getProperty('java.home')}/jmods/java.base.jmod", jarfilter: '!**.jar', filter: '!module-info.class'
        libraryjars "${System.getProperty('java.home')}/jmods/java.sql.jmod", jarfilter: '!**.jar', filter: '!module-info.class'
        //libraryjars "${System.getProperty('java.home')}/jmods/....."
    }
    def ideaPath = getIDEAPath()
    libraryjars fileTree("$ideaPath/plugins/java/lib").filter { !it.name.startsWith("debugger") }.collect()
    libraryjars files("$ideaPath/lib")
    libraryjars files(configurations.compile.collect())

    def original = jar.archiveFile.get().asFile
    def obfuscated = new File(original.parent, "obfuscated.jar")

    injars original
    outjars file(obfuscated.path)
}

prepareSandbox.dependsOn(myProguardTask)

prepareSandbox.doFirst {
    def original = jar.archiveFile.get().asFile
    def obfuscated = new File(original.parent, "obfuscated.jar")
    if (original.exists() && obfuscated.exists()) {
        original.delete()
        obfuscated.renameTo(original)
    } else {
        println "error: some file does not exist, plugin file not obfuscated"
    }
}

related part of my proguard.pro file:

-adaptresourcefilecontents is commented out

-target 1.8
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,LocalVariable*Table,*Annotation*,Synthetic,EnclosingMethod
##-adaptresourcefilecontents **.properties,META-INF/MANIFEST.MF
-verbose

-keepclassmember class * {
    public <init>(***);
}

# Also keep - Enumerations. Keep the special static methods that are required in
# enumeration classes.
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

# Also keep - Swing UI L&F. Keep all extensions of javax.swing.plaf.ComponentUI,
# along with the special 'createUI' method.
-keep class * extends javax.swing.plaf.ComponentUI {
public static javax.swing.plaf.ComponentUI createUI(javax.swing.JComponent);
}

-keepclassmembers class * implements java.io.Serializable {
        static final long serialVersionUID;
        private static final java.io.ObjectStreamField[] serialPersistentFields;
        !static !transient <fields>;
        !private <fields>;
        !private <methods>;
        private void writeObject(java.io.ObjectOutputStream);
        private void readObject(java.io.ObjectInputStream);
        java.lang.Object writeReplace();
        java.lang.Object readResolve();
    }

Oh, I finally find out the solution to this. So in the end, proguard did not change my resource files,but it did change my resource folder

Solution:

I added -keepdirectories to proguard.pro file to keep all the directories inside jar file.

Here is the quote from official website

MissingResourceException or NullPointerException

Your processed code may be unable to find some resource files. ProGuard simply copies resource files over from the input jars to the output jars. Their names and contents remain unchanged, unless you specify the options-adaptresourcefilenames and/or-adaptresourcefilecontents. Furthermore, directory entries in jar files aren't copied, unless you specify the option -keepdirectories. Note that Sun advises against calling Class.getResource() for directories (Sun Bug #4761949]( http://bugs.sun.com/view_bug.do?bug_id=4761949 )).

here is the link for -keepdirectories

quote

By default, directory entries are removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM