From 0f6dae86541473d232213034c828d88a9a14f0cb Mon Sep 17 00:00:00 2001 From: Steve Hannah Date: Sat, 2 Mar 2024 12:25:57 -0800 Subject: [PATCH] fix: Kotlin support when using gradle 8 When using gradle 8, kotlin is included automatically so it needs to be stripped from user classes if they are provided. --- .../builders/AndroidGradleBuilder.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/AndroidGradleBuilder.java b/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/AndroidGradleBuilder.java index de5d801bed..76a949cba9 100644 --- a/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/AndroidGradleBuilder.java +++ b/maven/codenameone-maven-plugin/src/main/java/com/codename1/builders/AndroidGradleBuilder.java @@ -88,6 +88,11 @@ public class AndroidGradleBuilder extends Executor { private boolean useGradle8 = true; + // Flag to indicate whether we should strip kotlin from user classes + // Necessary for using gradle 8 because kotlin seems to be included by default, + // so we get duplicate class errors. + private boolean stripKotlinFromUserClasses = true; + private boolean extendAppCompatActivity = false; private boolean useJava8SourceLevel = true; @@ -477,6 +482,8 @@ public boolean build(File sourceZip, final BuildRequest request) throws BuildExc newFirebaseMessaging = request.getArg("android.newFirebaseMessaging", "true").equals("true"); useGradle8 = request.getArg("android.useGradle8", ""+(useGradle8 || newFirebaseMessaging || facebookSupported)).equals("true"); extendAppCompatActivity = request.getArg("android.extendAppCompatActivity", "false").equals("true"); + // When using gradle 8 we need to strip kotlin files from user classes otherwise we get duplicate class errors + stripKotlinFromUserClasses = useGradle8; useJava8SourceLevel = request.getArg("android.java8", ""+useJava8SourceLevel).equals("true"); if (useGradle8) { getGradleJavaHome(); // will throw build exception if JAVA17_HOME is not set @@ -976,6 +983,9 @@ public boolean build(File sourceZip, final BuildRequest request) throws BuildExc } catch (Exception ex) { throw new BuildException("Failed to extract source zip "+sourceZip, ex); } + if (stripKotlinFromUserClasses) { + stripKotlin(dummyClassesDir); + } File appDir = buildToolsVersionInt >= 27 ? new File(srcDir.getParentFile(), "app") : @@ -4445,4 +4455,19 @@ private void initPlayServiceVersions(BuildRequest request) { } } } + + private void stripKotlin(File dummyClassesDir) { + String[] skipDirectories = new String[] { + "org" + File.separator + "jetbrains" + File.separator + "annotations", + "org" + File.separator + "intellij" + File.separator + "lang" + File.separator + "annotations", + "kotlin" + }; + for (String path : skipDirectories) { + File directory = new File(dummyClassesDir, path); + if (directory.isDirectory()) { + log("Deleting directory " + directory); + delTree(directory, true); + } + } + } }