Status Update
Comments
cm...@google.com <cm...@google.com>
xa...@google.com <xa...@google.com> #2
Ying, can you take a look?
It seems like when trying to run a class we trigger a build and inject a gradle file via --init-script
?
no...@gmail.com <no...@gmail.com> #3
xa...@google.com <xa...@google.com>
so...@google.com <so...@google.com> #4
There are several issues here:
(1) this is a duplicate of
(2) however, I believe Android modules might not expose the full runtime classpath to the IDE and thus there might still be missing classes in some cases.
so...@google.com <so...@google.com>
ga...@google.com <ga...@google.com> #5
The issue is in the generated init script that's used in the TAPI invocation:
def gradlePath = ':app'
def runAppTaskName = 'Data.main()'
def mainClass = 'com.example.myapplication.Data'
def javaExePath = mapPath('/usr/local/google/home/gavra/.jdks/temurin-17.0.3/bin/java')
def _workingDir = mapPath('/usr/local/google/home/gavra/AndroidStudioProjects/MyApplication86')
def sourceSetName = 'main'
def javaModuleName = null
import org.gradle.util.GradleVersion
allprojects {
afterEvaluate { project ->
if(project.path == gradlePath && project?.convention?.findPlugin(JavaPluginConvention)) {
def overwrite = project.tasks.findByName(runAppTaskName) != null
project.tasks.create(name: runAppTaskName, overwrite: overwrite, type: JavaExec) {
if (javaExePath) executable = javaExePath
main = mainClass
if (_workingDir) workingDir = _workingDir
standardInput = System.in
if (javaModuleName) {
classpath = tasks[sourceSets[sourceSetName].jarTaskName].outputs.files + project.sourceSets[sourceSetName].runtimeClasspath;
if (GradleVersion.current().baseVersion < GradleVersion.version("6.4")) {
doFirst {
jvmArgs += [
'--module-path', classpath.asPath,
'--module', javaModuleName + '/' + mainClass
]
classpath = files()
}
} else {
mainModule = javaModuleName
}
} else {
classpath = project.sourceSets[sourceSetName].runtimeClasspath
}
}
}
}
}
this script is able to handle only java projects.
We should remove the run configurations for main
from the gutter in Android modules, and the recommended way to accomplish #1 is to:
- create a non-Android (i.e. java or kotlin) module and add the
main()
method there - add an Android module that depends on the java/kotlin library
- start using APIs from the library in the Android module.
Bradley can you please take a look at hiding the run configurations?
sm...@google.com <sm...@google.com> #6
Why does this script not work for Android modules? What in 3.6 broke this? Can we generalize the script or make changes to AGP so that it does work?
ga...@google.com <ga...@google.com> #7
The script relies on the Java plugin source sets, jar tasks etc. that are not applicable for Android projects. WRT AS 3.6, I did not verify if it was working in that version nor what changed. Can you please check?
It is definitely possible to trigger full compilation in an Andorid Gradle project, compute the full runtime classpath, and specify the correct args for JavaExec
. Challenge here is writing it in a way that works with all AGP versions, and that is definitely a non-trivial effort. The workaround from #5 seems like a very low-friction solution, so I recommend users to use it as a way to run main
, and we should hide the run configuration from the gutter.
mi...@google.com <mi...@google.com>
mi...@google.com <mi...@google.com>
mi...@google.com <mi...@google.com> #9
I have checked in the fix for this on the JetBrains side. It will take some time for this IJ version to be available to Android. Once it is, I will create the fix for Android and update here.
Description
But after upgrading to AS 3.6, we can no longer run the main method. If you right-click the class and attempt to run its main method, it says this cannot be done —error message shown below. You can still invoke the class methods from the activity, and you can still deploy the app on a device, but you cannot run a main method.
To reproduce, simply right-click app, choose new Java Class, and create a hello class with a main method. Then right click the class and select run class.main()
It seems AS 3.6 stopped supporting the run of a main method. Can you please verify that this feature (which was available for years) was dropped? And if so, what is the easiest way to replace it? We have tons of labs already written assuming this feature exists and the summer term will start in just 3 weeks.
Looking forward to your response (please cc my email when you do).
—>ERROR message when we try to run a Java main method:
4:42:58 PM: Executing task 'Api.main()'...
Executing tasks: [Api.main()] in project /Users/roumani/AndroidStudioProjects/MCalcPro
FAILURE: Build failed with an exception.
* Where:
Initialization script '/private/var/folders/x4/7dxxd4zs72j9jb52w9ppfj480000gn/T/Api_main__.gradle' line: 20
* What went wrong:
A problem occurred configuring project ':app'.
> Could not create task ':app:Api.main()'.
> SourceSet with name 'main' not found.