簡體   English   中英

intellij插件開發中的proguard問題

[英]proguard issue in intellij plugin development

我正在嘗試使用 ProGuard 來混淆我的 intellij 插件。

我正在向 IntelliJ 添加一些內部文件模板以創建新文件。 <RelatedTemplateName>是我添加到resources/fileTemplates/internal/<RelatedTemplateName>.ft的文件名

到目前為止一切順利,除了...

在混淆插件中:IntelliJ 代碼在我的插件中找不到某些資源文件。

在非混淆插件中:一切正常。

我曾經以為 proguard 更改了我的資源文件,但我不認為根據此鏈接更改了任何資源文件,因為我的 proguard.pro 文件中沒有添加此類選項

有人能幫我找出這個問題的根本原因嗎? 謝謝

是不是因為 proguard 改變了一些與此相關的其他類?

其他相關信息如下

例外

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)

我的proguard配置:

build.gradle 文件的相關部分:

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"
    }
}

我的 proguard.pro 文件的相關部分:

-adaptresourcefilecontents 被注釋掉

-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();
    }

哦,我終於找到了解決方案。 所以最后proguard並沒有改變我的資源文件,但它確實改變了我的資源文件夾

解決方案:

我在 proguard.pro 文件中添加了-keepdirectories以將所有目錄保留在 jar 文件中。

這是官方網站的報價

MissingResourceException 或 NullPointerException

您處理的代碼可能無法找到某些資源文件。 ProGuard 只是將資源文件從輸入 jars 復制到 output jars。 它們的名稱和內容保持不變,除非您指定 options-adaptresourcefilenames 和/或-adaptresourcefilecontents。 此外,不會復制 jar 文件中的目錄條目,除非您指定選項 -keepdirectories。 請注意,Sun 建議不要為目錄調用 Class.getResource()(Sun 錯誤 #4761949]( http://bugs.sun.com/view_bug.do?bug_id=4761949 ))。

這是-keepdirectories鏈接

引用

默認情況下,目錄條目被刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM