Skip to content

Commit

Permalink
fix: Kotlin support when using gradle 8 (#3792)
Browse files Browse the repository at this point in the history
When using gradle 8, kotlin is included automatically so it needs to be stripped from user classes if they are provided.
  • Loading branch information
shannah authored Mar 2, 2024
1 parent 50cbaf4 commit 892fa4c
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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") :
Expand Down Expand Up @@ -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);
}
}
}
}

0 comments on commit 892fa4c

Please sign in to comment.