mozilla / rust-android-gradle

Apache License 2.0
1.03k stars 67 forks source link

Github action flows fails: NDK is not installed #46

Closed redwarp closed 1 year ago

redwarp commented 3 years ago

I seem to be unable to use github actions with a project that uses the rust-android-gradle plugin.

I have a multi module project, and one of those modules, android-drawable-native, uses rust and the plugin:

https://github.com/redwarp/gifdecoder/blob/fd0983bcaf9220f4213fb9b3e114d3f912255539/android-drawable-native/build.gradle.kts

I set up a github action that installs the NDK, as well as calling gradle tasks: https://github.com/redwarp/gifdecoder/blob/fd0983bcaf9220f4213fb9b3e114d3f912255539/.github/workflows/publish.yml#L23

Installing the NDK seem to proceed fine, but the rest of the action fail, as gradle is unable to configure the android-drawable-native project.

A problem occurred configuring project ':android-drawable-native'.
Use '--warning-mode all' to show the individual deprecation warnings.
> NDK is not installed

See an exemple of a failed action: https://github.com/redwarp/gifdecoder/runs/1652298119?check_suite_focus=true

I temporarily deactivated the plugin in this project as the rust aspect of it is not my current focus (https://github.com/redwarp/gifdecoder/blob/be8f79a7806de604f5f5205fafebb1bf5481baa2/android-drawable-native/build.gradle.kts#L5)

Am I missing something to make it work? Does anyone has an exemple of a github action that works well with a gradle project using the plugin?

Thanks

ncalexan commented 3 years ago

Hi!

I seem to be unable to use github actions with a project that uses the rust-android-gradle plugin.

I have a multi module project, and one of those modules, android-drawable-native, uses rust and the plugin:

https://github.com/redwarp/gifdecoder/blob/fd0983bcaf9220f4213fb9b3e114d3f912255539/android-drawable-native/build.gradle.kts

I set up a github action that installs the NDK, as well as calling gradle tasks: https://github.com/redwarp/gifdecoder/blob/fd0983bcaf9220f4213fb9b3e114d3f912255539/.github/workflows/publish.yml#L23

Installing the NDK seem to proceed fine, but the rest of the action fail, as gradle is unable to configure the android-drawable-native project.

A problem occurred configuring project ':android-drawable-native'.
Use '--warning-mode all' to show the individual deprecation warnings.
> NDK is not installed

See an exemple of a failed action: https://github.com/redwarp/gifdecoder/runs/1652298119?check_suite_focus=true

I temporarily deactivated the plugin in this project as the rust aspect of it is not my current focus (https://github.com/redwarp/gifdecoder/blob/be8f79a7806de604f5f5205fafebb1bf5481baa2/android-drawable-native/build.gradle.kts#L5)

Am I missing something to make it work?

Installing the NDK using sdkmanager doesn't necessarily put it in a place that any other tooling knows about. My guess is that you'll need to configure the NDK location explicitly for some of your tooling. That error message is not directly from rust-android-gradle; it is possible that it comes indirectly from the Android SDK but it could be another plugin or project entirely.

In any case, rust-android-gradle gives you a great deal of flexibility specifying the NDK: see https://github.com/mozilla/rust-android-gradle#specifying-ndk-toolchains. (And also https://github.com/mozilla/rust-android-gradle#prebuilttoolchains, but given that you are installing an NDK, you definitely want prebuilt toolchains.)

Does anyone has an exemple of a github action that works well with a gradle project using the plugin?

I'm sorry, I do not -- Mozilla's automation is bespoke and I'm not really knowledgeable about other consumers. I will flag @MaulingMonkey, who has contributed some helpful commits to this project, in the off chance that they can redirect you.

Good luck!

redwarp commented 3 years ago

My hunch now is that it's not a problem of NDK being installed, but a problem with my gradle.kts file. Outside of the github environment, I put the project on a PC (instead of my work mac), and somehow locally I get the same error. Which is odd, as on the same PC, another project with the rust plugin has no problem compiling and doesn't shout at me "NDK is not installed"

I am trying to compare both, to understand what is the difference. For sure, one of them (the faulty one) specify everything with .kts, but I don't think that should be the issue. So I have to dig deeper (also, other projects like https://github.com/shadowsocks/shadowsocks-android/blob/master/core/build.gradle.kts seem to use kts without trouble)

So, the problem might still be as usual between the keyboard and the seat, I'll update my findings once I find the issue, I am sure this could help somebody else.

ncalexan commented 3 years ago

My hunch now is that it's not a problem of NDK being installed, but a problem with my gradle.kts file. Outside of the github environment, I put the project on a PC (instead of my work mac), and somehow locally I get the same error. Which is odd, as on the same PC, another project with the rust plugin has no problem compiling and doesn't shout at me "NDK is not installed"

It's my recollection that the Android-Gradle plugin looks for the NDK using ndk.dir in local.properties. Perhaps check if those files are present and if their contents differ?

Other than that, run with --stacktrace and --debug to get more information out of Gradle.

lattice0 commented 3 years ago

I get this problem after adding the mozilla plugin. When I take it off, no NDK error.

I tried setting the env variable as says in the readme:

echo $ANDROID_NDK_TOOLCHAIN_DIR
/opt/android-sdk-linux/ndk/22.1.7171670}/toolchains

but I still get an error.

I'd like to not use local.properties because I want my Dockerfile to be responsible for the entire location and NDK version.

Also here https://github.com/mozilla/rust-android-gradle/blob/master/plugin/src/main/kotlin/com/nishtahir/RustAndroidPlugin.kt#L214 it looks like the only place where it looks for the NDK is source.properties

lattice0 commented 3 years ago

Here's the stack trace:

Caused by: org.gradle.api.InvalidUserDataException: NDK is not installed
        at com.android.build.gradle.internal.ndk.NdkInstallStatus.getOrThrow(NdkHandler.kt:61)
        at com.android.build.gradle.internal.SdkComponentsBuildService$ndkDirectoryProvider$1.call(SdkComponents.kt:209)
        at com.android.build.gradle.internal.SdkComponentsBuildService$ndkDirectoryProvider$1.call(SdkComponents.kt:57)
        at org.gradle.api.internal.provider.DefaultProvider.calculateOwnValue(DefaultProvider.java:66)
        at org.gradle.api.internal.provider.AbstractMinimalProvider.calculateValue(AbstractMinimalProvider.java:103)
        at org.gradle.api.internal.provider.TransformBackedProvider.calculateOwnValue(TransformBackedProvider.java:64)
        at org.gradle.api.internal.provider.AbstractMinimalProvider.calculateValue(AbstractMinimalProvider.java:103)
        at org.gradle.api.internal.provider.MappingProvider.calculateOwnValue(MappingProvider.java:55)
        at org.gradle.api.internal.provider.AbstractMinimalProvider.calculateValue(AbstractMinimalProvider.java:103)
        at org.gradle.api.internal.provider.DefaultProperty.calculateValueFrom(DefaultProperty.java:128)
        at org.gradle.api.internal.provider.DefaultProperty.calculateValueFrom(DefaultProperty.java:25)
        at org.gradle.api.internal.provider.AbstractProperty.doCalculateValue(AbstractProperty.java:133)
        at org.gradle.api.internal.provider.AbstractProperty.calculateOwnValue(AbstractProperty.java:127)
        at org.gradle.api.internal.provider.AbstractMinimalProvider.calculateValue(AbstractMinimalProvider.java:103)
        at org.gradle.api.internal.provider.FlatMapProvider.calculateOwnValue(FlatMapProvider.java:50)
        at org.gradle.api.internal.provider.AbstractMinimalProvider.get(AbstractMinimalProvider.java:84)
        at com.android.build.gradle.BaseExtension.getNdkDirectory(BaseExtension.kt:378)
        at com.nishtahir.RustAndroidPlugin$apply$$inlined$with$lambda$1$1.execute(RustAndroidPlugin.kt:356)
        at com.nishtahir.RustAndroidPlugin$apply$$inlined$with$lambda$1$1.execute(RustAndroidPlugin.kt:140)
        at org.gradle.configuration.internal.DefaultUserCodeApplicationContext$CurrentApplication$1.execute(DefaultUserCodeApplicationContext.java:100)
        at org.gradle.api.internal.DefaultCollectionCallbackActionDecorator$BuildOperationEmittingAction$1.run(DefaultCollectionCallbackActionDecorator.java:95)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$1.execute(DefaultBuildOperationRunner.java:29)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$1.execute(DefaultBuildOperationRunner.java:26)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$3.execute(DefaultBuildOperationRunner.java:75)

I couldn't find RustAndroidPlugin.kt:356, I don't know for which version this code is, there's no such line on github now. I'm lost.

ncalexan commented 3 years ago

Hi! First, ANDROID_NDK_TOOLCHAIN_DIR is where toolchains that are built are cached. You want to use a prebuilt toolchain, so that directory isn't relevant to you.

Second, you should be able to figure out which version of the plugin you're running by inspecting your .gradle files and/or ./gradlew dependencies --configuration .... I can't recall how to determine your configuration, but I think a reasonable one for Android projects is debugCompileClasspath. (This will depend on product flavors, etc.)

Third, it sounds like you need to tell the Android-Gradle plugin where to find the NDK. You can do that in a few ways, but the ANDROID_NDK_{HOME,ROOT} environment variables are probably the easiest. (The exact variable depends on versions, IIRC.)

Finally, the path /opt/android-sdk-linux/ndk/22.1.7171670}/toolchains looks like it has an extraneous }. Check it to make sure it wasn't copy-pasted from elsewhere?

Good luck!

lattice0 commented 3 years ago

@ncalexan thanks, fixed:


root@8e8e4cc9bb24:/home/dev/orwell/orwell_flutter_app_2/android# echo $ANDROID_NDK_TOOLCHAIN_DIR
/opt/android-sdk-linux/ndk/22.1.7171670/toolchains
root@8e8e4cc9bb24:/home/dev/orwell/orwell_flutter_app_2/android# ls /opt/android-sdk-linux/ndk/22.1.7171670/toolchains
aarch64-linux-android-4.9  arm-linux-androideabi-4.9  llvm  renderscript  x86-4.9  x86_64-4.9

but the same error appears.

I know that I'm using com.android.tools.build:gradle:4.1.0 but I couldn't find the source for this version online.

I'm also using org.mozilla.rust-android-gradle:plugin:0.8.6 for which the source is https://github.com/mozilla/rust-android-gradle/blob/e2183c1f3f4297d3cd3a580cec96bf72d0f33ce0/plugin/src/main/kotlin/com/nishtahir/RustAndroidPlugin.kt but this doesn't even has 300 lines so I cannot find where the error occurs from the stack trace: (RustAndroidPlugin.kt:356).

Putting ndkVersion '22.1.7171670' on app/build.gradle as described here https://github.com/mozilla/rust-android-gradle/issues/29#issuecomment-593501017 worked but I'm trying to avoid that. Whenever I change the SDK I'd have to change in other places of the project which is bad. Since I'm using a dockerized build system I'd like to be able to control the SDK version from the Dockerfile.

Also ANDROID_NDK_{HOME,ROOT} is deprecated and won't work anymore. If I run ./gradlew assembleDebug --info, one of the lines it prints is

Not considering ANDROID_NDK_HOME because support was removed after deprecation period..

I don't know how it's possible that the plugin is finding the NDK from the gradle file if it tried to find it from local.properties as noted here https://github.com/mozilla/rust-android-gradle/blob/e2183c1f3f4297d3cd3a580cec96bf72d0f33ce0/plugin/src/main/kotlin/com/nishtahir/RustAndroidPlugin.kt#L214. Unless this is not the version on maven. I don't know I'm super confused I know very little about gradle.

ncalexan commented 3 years ago

@ncalexan thanks, fixed:


root@8e8e4cc9bb24:/home/dev/orwell/orwell_flutter_app_2/android# echo $ANDROID_NDK_TOOLCHAIN_DIR
/opt/android-sdk-linux/ndk/22.1.7171670/toolchains
root@8e8e4cc9bb24:/home/dev/orwell/orwell_flutter_app_2/android# ls /opt/android-sdk-linux/ndk/22.1.7171670/toolchains
aarch64-linux-android-4.9  arm-linux-androideabi-4.9  llvm  renderscript  x86-4.9  x86_64-4.9

but the same error appears.

As I said, this isn't what you want. ANDROID_NDK_TOOLCHAIN_DIR is used internally to cache in rust-android-gradle; just leave it unset.

I know that I'm using com.android.tools.build:gradle:4.1.0 but I couldn't find the source for this version online.

I'm also using org.mozilla.rust-android-gradle:plugin:0.8.6 for which the source is https://github.com/mozilla/rust-android-gradle/blob/e2183c1f3f4297d3cd3a580cec96bf72d0f33ce0/plugin/src/main/kotlin/com/nishtahir/RustAndroidPlugin.kt but this doesn't even has 300 lines so I cannot find where the error occurs from the stack trace: (RustAndroidPlugin.kt:356).

I think we're seeing an artifact of the Kotlin compiler here, honestly. Let's leave it for now.

Putting ndkVersion '22.1.7171670' on app/build.gradle as described here #29 (comment) worked but I'm trying to avoid that. Whenever I change the SDK I'd have to change in other places of the project which is bad. Since I'm using a dockerized build system I'd like to be able to control the SDK version from the Dockerfile.

This just isn't how the Android-Gradle plugin works anymore. But you can have the equivalent by using one of the many Gradle mechanisms for taking something from the environment and making it available to your script, i.e., System.env or https://docs.gradle.org/current/userguide/build_environment.html#sec:project_properties.

I'd suggest instead you bake the SDK and NDK versions into your project. You can't expect to upgrade them without changing other things anyway, so don't take them from the environment. It's unsupported for a reason!

lattice0 commented 3 years ago

@ncalexan indeed,

android{
    //...
    ndkVersion System.getenv("ANDROID_NDK_VERSION")
}

worked, thanks.

goebelUB commented 1 year ago

I had a similar issue, adding this here in case it helps someone:

In my case the NDK is not installed error in the configuration step was fixed by

  android {
-       ndkVersion '22.1.7171670'
+       ndkVersion '23.2.8568313'
  }

I didn't explicitly install the Android SDK, so my guess is that the old NDK version was no longer available on Github's runner. Which also explains why my previously working CI suddenly broke without any code change. You can find the NDK releases here.

ncalexan commented 1 year ago

Makes sense. GH publishes their VM details here: https://github.com/actions/virtual-environments, and for example ubuntu-2204 lists:

NDK
21.4.7075529
23.2.8568313 (default)
24.0.8215888

Presumably those are the latest of the 21 and 23 series.

rushiiMachine commented 1 year ago

The thing that seemed to work for me was

android {
    ndkVersion = sdkDirectory.resolve("ndk").listFilesOrdered().last().name
}