Remove JRE entry from External Library for Android Studio project

Answered

Hi,

I am building a project for a specific environment which must no rely on the standard JRE. Standard Java APIs are provided by other project dependencies. Therefore I would like to remove the JRE entry from the External Dependencies.

I can remove it by deleting the following line from the .iml file:

<orderEntry type="inheritedJdk" />

but it is added back as soon as I reload the Gradle project.

Is there a way to remove it permanently ?

 

Regards

0
4 comments

Register listener com.intellij.openapi.externalSystem.service.project.manage.ProjectDataImportListener and modify IDE-side model accordingly (https://plugins.jetbrains.com/docs/intellij/sdk.html)

0

Hi,

Thanks for your answer.

I tried to use the listener ProjectDataImportListener by creating this class:

class MyProjectDataImportListener : ProjectDataImportListener {
override fun onImportFinished(projectPath: String?) {
val Log: Logger = Logger.getInstance("logger")
Log.error("#### onImportFinished ...")
}

override fun onImportFailed(projectPath: String?) {
super.onImportFailed(projectPath)
}
}

and adding it in the plugin.xml file:

 <applicationListeners>
   <listener class="com.company.application.MyProjectDataImportListener" topic="com.intellij.openapi.externalSystem.service.project.manage.ProjectDataImportListener"/>
</applicationListeners>

But it does not seem to be executed.

I tried the same thing with the listener ProjectManagerListener, it is well called:

override fun projectOpened(project: Project) {
val Log = Logger.getInstance("logger")
Log.info("#### MyProjectManagerListener ...")

val r = Runnable {
val projectSdk: Sdk? = ProjectRootManager.getInstance(project).projectSdk
Log.info("#### Name = " + projectSdk?.name)
ProjectRootManager.getInstance(project).projectSdk = null
}
WriteCommandAction.runWriteCommandAction(project, r);
}

Not sure it is the right way to remove the SDK but the SDK does not appear anymore in the External Libraries. The remaining problem is that it appears again as soon as I reload the Gradle project (right-click on the project in the Gradle view > Reload Gradle project).

Would the use of ProjectDataImportListener listener solve this problem ? Should I use another listener ?

 

Regards

0

I found a way to have the JRE entry removed even after a Gradle project reload by using the extension com.intellij.openapi.roots.ProjectExtension:

internal class MyProjectExtension(private val myProject: Project) : ProjectExtension() {

  override fun projectSdkChanged(sdk: Sdk?) {
    if(sdk != null) {
      ProjectRootManager.getInstance(myProject).projectSdk = null

    }
  }

  ...
}
<extensions defaultExtensionNs="com.intellij">
   <projectExtension implementation="com.company.application.MyProjectExtension"></projectExtension>
</extensions>

The drawback is that this extension is an internal API. But I didn't find a better way so far.

Also I would like to be able to do the same thing but as the module level : in a project, a module uses the JRE as a dependency and another one does not. I tried to use the ModuleManager API but did not find a way to remove the JRE entry. Is this even possible ?

 

Regards

0

It seems your ProjectDataImportListener doesn't have Project CTOR parameter, then it should work.

 

class MyListener(val project: Project) : ProjectDataImportListener {...}
0

Please sign in to leave a comment.