intellij插件开发中的proguard问题
proguard issue in intellij plugin development
我正在尝试使用 ProGuard 来混淆我的 intellij 插件。
我正在向 IntelliJ 添加一些内部文件模板以创建新文件。 <RelatedTemplateName>
是我添加到 resources/fileTemplates/internal/<RelatedTemplateName>.ft
的文件名
到目前为止还不错,除了...
在混淆插件中:
IntelliJ 代码在我的插件中找不到一些资源文件。
在非混淆插件中:
一切正常。
我曾经以为proguard改变了我的资源文件,但我认为没有任何资源文件是根据这个改变的link
,因为我没有将此类选项添加到我的 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并没有改变我的资源文件,但是它确实改变了我的资源文件夹
解决方案:
我将 -keepdirectories
添加到 proguard.pro 文件以将所有目录保留在 jar 文件中。
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)).
这里是link -keepdirectories
引用
By default, directory entries are removed.
我正在尝试使用 ProGuard 来混淆我的 intellij 插件。
我正在向 IntelliJ 添加一些内部文件模板以创建新文件。 <RelatedTemplateName>
是我添加到 resources/fileTemplates/internal/<RelatedTemplateName>.ft
到目前为止还不错,除了...
在混淆插件中: IntelliJ 代码在我的插件中找不到一些资源文件。
在非混淆插件中: 一切正常。
我曾经以为proguard改变了我的资源文件,但我认为没有任何资源文件是根据这个改变的link ,因为我没有将此类选项添加到我的 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并没有改变我的资源文件,但是它确实改变了我的资源文件夹
解决方案:
我将 -keepdirectories
添加到 proguard.pro 文件以将所有目录保留在 jar 文件中。
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)).
这里是link -keepdirectories
引用
By default, directory entries are removed.