Skip to content

Commit 04ac104

Browse files
[jnigen] Support build.gradle.kts (#2184)
1 parent 4e74330 commit 04ac104

File tree

3 files changed

+88
-14
lines changed

3 files changed

+88
-14
lines changed

pkgs/jni/android/build.gradle

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ android {
4949
// to bump the version in their app.
5050
compileSdkVersion 31
5151

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

5954
// Invoke the shared CMake build with the Android Gradle Plugin.
6055
externalNativeBuild {

pkgs/jnigen/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package bindings will need to delete them once for it to start working.
66
- Added the ability to generate classes in Java SDK (`java.core`) module without
77
providing the class path.
8+
- Added gradle support for new Flutter projects that use `build.gradle.kts`
9+
instead of `build.gradle`.
810

911
## 0.14.1
1012

pkgs/jnigen/lib/src/tools/android_sdk_tools.dart

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,33 @@ task $_gradleGetClasspathTaskName(type: Copy) {
9595
}
9696
}
9797
}
98+
''';
99+
100+
static const _gradleGetClasspathStubKt = '''
101+
// Gradle stub for listing dependencies in jnigen. If found in
102+
// android/build.gradle.kts, please delete the following function.
103+
tasks.register<DefaultTask>("$_gradleGetClasspathTaskName") {
104+
doLast {
105+
try {
106+
val app = project(":app")
107+
val android = app.android
108+
val classPaths = mutableListOf(android.bootClasspath.first()) // Access the first element directly
109+
for (variant in android.applicationVariants) {
110+
if (variant.name == "release") {
111+
val javaCompile = variant.javaCompileProvider.get()
112+
classPaths.addAll(javaCompile.classpath.files)
113+
}
114+
}
115+
for (classPath in classPaths) {
116+
println(classPath)
117+
}
118+
} catch (e: Exception) {
119+
System.err.println("$_gradleCannotFindJars")
120+
throw e
121+
}
122+
}
123+
System.err.println("$_leftOverStubWarning")
124+
}
98125
''';
99126

100127
static const _gradleGetSourcesTaskName = 'getSources';
@@ -129,6 +156,42 @@ task $_gradleGetSourcesTaskName(type: Copy) {
129156
}
130157
System.err.println("$_leftOverStubWarning")
131158
}
159+
''';
160+
161+
static const _gradleGetSourcesStubKt = '''
162+
// Gradle stub for fetching source dependencies in jnigen. If found in
163+
// android/build.gradle.kts, please delete the following function.
164+
165+
tasks.register<DefaultTask>("$_gradleGetSourcesTaskName") {
166+
doLast {
167+
val app = project(":app")
168+
val releaseCompileClasspath = app.configurations.getByName("releaseCompileClasspath")
169+
170+
val componentIds =
171+
releaseCompileClasspath.incoming.resolutionResult.allDependencies.map { it.from.id }
172+
173+
val result = dependencies.createArtifactResolutionQuery()
174+
.forComponents(componentIds)
175+
.withArtifacts(JvmLibrary::class, SourcesArtifact::class)
176+
.execute()
177+
178+
val sourceArtifacts = mutableListOf<File>()
179+
180+
for (component in result.resolvedComponents) {
181+
val sourcesArtifactsResult = component.getArtifacts(SourcesArtifact::class)
182+
for (artifactResult in sourcesArtifactsResult) {
183+
if (artifactResult is org.gradle.api.artifacts.result.ResolvedArtifactResult) {
184+
sourceArtifacts.add(artifactResult.file)
185+
}
186+
}
187+
}
188+
for (sourceArtifact in sourceArtifacts) {
189+
println(sourceArtifact)
190+
}
191+
}
192+
System.err.println("$_leftOverStubWarning")
193+
}
194+
132195
''';
133196

134197
/// Get release compile classpath used by Gradle for android build.
@@ -142,8 +205,7 @@ task $_gradleGetSourcesTaskName(type: Copy) {
142205
static List<String> getGradleClasspaths(
143206
{Uri? configRoot, String androidProject = '.'}) =>
144207
_runGradleStub(
145-
stubName: _gradleGetClasspathTaskName,
146-
stubCode: _gradleGetClasspathStub,
208+
isSource: false,
147209
androidProject: androidProject,
148210
configRoot: configRoot,
149211
);
@@ -156,8 +218,7 @@ task $_gradleGetSourcesTaskName(type: Copy) {
156218
static List<String> getGradleSources(
157219
{Uri? configRoot, String androidProject = '.'}) {
158220
return _runGradleStub(
159-
stubName: _gradleGetSourcesTaskName,
160-
stubCode: _gradleGetSourcesStub,
221+
isSource: true,
161222
androidProject: androidProject,
162223
configRoot: configRoot,
163224
);
@@ -168,18 +229,34 @@ task $_gradleGetSourcesTaskName(type: Copy) {
168229
}
169230

170231
static List<String> _runGradleStub({
171-
required String stubName,
172-
required String stubCode,
232+
required bool isSource,
173233
Uri? configRoot,
174234
String androidProject = '.',
175235
}) {
236+
final stubName =
237+
isSource ? _gradleGetSourcesTaskName : _gradleGetClasspathTaskName;
176238
log.info('trying to obtain gradle dependencies [$stubName]...');
177239
if (configRoot != null) {
178240
androidProject = configRoot.resolve(androidProject).toFilePath();
179241
}
180242
final android = join(androidProject, 'android');
181-
final buildGradle = join(android, 'build.gradle');
182-
final buildGradleOld = join(android, 'build.gradle.old');
243+
var buildGradle = join(android, 'build.gradle');
244+
final usesKotlinScript = !File.fromUri(Uri.file(buildGradle)).existsSync();
245+
if (usesKotlinScript) {
246+
// The Kotlin version of the script is injected to `app/build.gradle.kts`
247+
// instead of `build.gradle`.
248+
buildGradle = join(android, 'app', 'build.gradle.kts');
249+
}
250+
final String stubCode;
251+
if (isSource) {
252+
stubCode =
253+
usesKotlinScript ? _gradleGetSourcesStubKt : _gradleGetSourcesStub;
254+
} else {
255+
stubCode = usesKotlinScript
256+
? _gradleGetClasspathStubKt
257+
: _gradleGetClasspathStub;
258+
}
259+
final buildGradleOld = '$buildGradle.old';
183260
final origBuild = File(buildGradle);
184261
final script = origBuild.readAsStringSync();
185262
origBuild.renameSync(buildGradleOld);

0 commit comments

Comments
 (0)