Skip to content

[jnigen] Support build.gradle.kts #2184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions pkgs/jni/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ android {
// to bump the version in their app.
compileSdkVersion 31

// Bumping the plugin ndkVersion requires all clients of this plugin to bump
// the version in their app and to download a newer version of the NDK.
// Note(MaheshH) - Flutter seems to download minimum NDK of flutter when
// below line is commented out.
// How about leaving it?
// ndkVersion "21.1.6352462"
ndkVersion flutter.ndkVersion

// Invoke the shared CMake build with the Android Gradle Plugin.
externalNativeBuild {
Expand Down
2 changes: 2 additions & 0 deletions pkgs/jnigen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package bindings will need to delete them once for it to start working.
- Added the ability to generate classes in Java SDK (`java.core`) module without
providing the class path.
- Added gradle support for new Flutter projects that use `build.gradle.kts`
instead of `build.gradle`.

## 0.14.1

Expand Down
93 changes: 85 additions & 8 deletions pkgs/jnigen/lib/src/tools/android_sdk_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ task $_gradleGetClasspathTaskName(type: Copy) {
}
}
}
''';

static const _gradleGetClasspathStubKt = '''
// Gradle stub for listing dependencies in jnigen. If found in
// android/build.gradle.kts, please delete the following function.
tasks.register<DefaultTask>("$_gradleGetClasspathTaskName") {
doLast {
try {
val app = project(":app")
val android = app.android
val classPaths = mutableListOf(android.bootClasspath.first()) // Access the first element directly
for (variant in android.applicationVariants) {
if (variant.name == "release") {
val javaCompile = variant.javaCompileProvider.get()
classPaths.addAll(javaCompile.classpath.files)
}
}
for (classPath in classPaths) {
println(classPath)
}
} catch (e: Exception) {
System.err.println("$_gradleCannotFindJars")
throw e
}
}
System.err.println("$_leftOverStubWarning")
}
''';

static const _gradleGetSourcesTaskName = 'getSources';
Expand Down Expand Up @@ -129,6 +156,42 @@ task $_gradleGetSourcesTaskName(type: Copy) {
}
System.err.println("$_leftOverStubWarning")
}
''';

static const _gradleGetSourcesStubKt = '''
// Gradle stub for fetching source dependencies in jnigen. If found in
// android/build.gradle.kts, please delete the following function.

tasks.register<DefaultTask>("$_gradleGetSourcesTaskName") {
doLast {
val app = project(":app")
val releaseCompileClasspath = app.configurations.getByName("releaseCompileClasspath")

val componentIds =
releaseCompileClasspath.incoming.resolutionResult.allDependencies.map { it.from.id }

val result = dependencies.createArtifactResolutionQuery()
.forComponents(componentIds)
.withArtifacts(JvmLibrary::class, SourcesArtifact::class)
.execute()

val sourceArtifacts = mutableListOf<File>()

for (component in result.resolvedComponents) {
val sourcesArtifactsResult = component.getArtifacts(SourcesArtifact::class)
for (artifactResult in sourcesArtifactsResult) {
if (artifactResult is org.gradle.api.artifacts.result.ResolvedArtifactResult) {
sourceArtifacts.add(artifactResult.file)
}
}
}
for (sourceArtifact in sourceArtifacts) {
println(sourceArtifact)
}
}
System.err.println("$_leftOverStubWarning")
}

''';

/// Get release compile classpath used by Gradle for android build.
Expand All @@ -142,8 +205,7 @@ task $_gradleGetSourcesTaskName(type: Copy) {
static List<String> getGradleClasspaths(
{Uri? configRoot, String androidProject = '.'}) =>
_runGradleStub(
stubName: _gradleGetClasspathTaskName,
stubCode: _gradleGetClasspathStub,
isSource: false,
androidProject: androidProject,
configRoot: configRoot,
);
Expand All @@ -156,8 +218,7 @@ task $_gradleGetSourcesTaskName(type: Copy) {
static List<String> getGradleSources(
{Uri? configRoot, String androidProject = '.'}) {
return _runGradleStub(
stubName: _gradleGetSourcesTaskName,
stubCode: _gradleGetSourcesStub,
isSource: true,
androidProject: androidProject,
configRoot: configRoot,
);
Expand All @@ -168,18 +229,34 @@ task $_gradleGetSourcesTaskName(type: Copy) {
}

static List<String> _runGradleStub({
required String stubName,
required String stubCode,
required bool isSource,
Uri? configRoot,
String androidProject = '.',
}) {
final stubName =
isSource ? _gradleGetSourcesTaskName : _gradleGetClasspathTaskName;
log.info('trying to obtain gradle dependencies [$stubName]...');
if (configRoot != null) {
androidProject = configRoot.resolve(androidProject).toFilePath();
}
final android = join(androidProject, 'android');
final buildGradle = join(android, 'build.gradle');
final buildGradleOld = join(android, 'build.gradle.old');
var buildGradle = join(android, 'build.gradle');
final usesKotlinScript = !File.fromUri(Uri.file(buildGradle)).existsSync();
if (usesKotlinScript) {
// The Kotlin version of the script is injected to `app/build.gradle.kts`
// instead of `build.gradle`.
buildGradle = join(android, 'app', 'build.gradle.kts');
}
final String stubCode;
if (isSource) {
stubCode =
usesKotlinScript ? _gradleGetSourcesStubKt : _gradleGetSourcesStub;
} else {
stubCode = usesKotlinScript
? _gradleGetClasspathStubKt
: _gradleGetClasspathStub;
}
final buildGradleOld = '$buildGradle.old';
final origBuild = File(buildGradle);
final script = origBuild.readAsStringSync();
origBuild.renameSync(buildGradleOld);
Expand Down
Loading