-
Notifications
You must be signed in to change notification settings - Fork 73
/
build.gradle
112 lines (100 loc) · 4.27 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
buildscript {
ext.agp_version = '8.7.1'
repositories {
google()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.android.tools.build:gradle:${agp_version}"
classpath 'org.mozilla.rust-android-gradle:rust-android:+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'org.mozilla.rust-android-gradle.rust-android'
android {
namespace "com.nishtahir.androidrust"
defaultConfig {
applicationId "com.nishtahir.androidrust"
minSdkVersion 21
targetSdkVersion 35
compileSdk 35
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
ndkVersion "27.1.12297006"
sourceSets {
test.resources.srcDirs += "$buildDir/rustJniLibs/desktop"
}
}
cargo {
module = "../rust"
targets = ["x86_64", "linux-x86-64"] // "x86", "x86_64", "arm64"]
// Needed for build on arm MAC
// targets = ["x86_64", "darwin-aarch64"] // "x86", "x86_64", "arm64"]
libname = "rust"
}
repositories {
google()
mavenCentral()
maven {
url "https://mvnrepository.com"
}
}
configurations {
// There's an interaction between Gradle's resolution of dependencies with different types
// (@jar, @aar) for `implementation` and `testImplementation` and with Android Studio's built-in
// JUnit test runner. The runtime classpath in the built-in JUnit test runner gets the
// dependency from the `implementation`, which is type @aar, and therefore the JNA dependency
// doesn't provide the JNI dispatch libraries in the correct Java resource directories. I think
// what's happening is that @aar type in `implementation` resolves to the @jar type in
// `testImplementation`, and that it wins the dependency resolution battle.
//
// A workaround is to add a new configuration which depends on the @jar type and to reference
// the underlying JAR file directly in `testImplementation`. This JAR file doesn't resolve to
// the @aar type in `implementation`. This works when invoked via `gradle`, but also sets the
// correct runtime classpath when invoked with Android Studio's built-in JUnit test runner.
// Success!
jnaForTest
}
dependencies {
jnaForTest "net.java.dev.jna:jna:5.15.0@jar"
implementation "net.java.dev.jna:jna:5.15.0@aar"
androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
testImplementation 'junit:junit:4.13.2'
// For reasons unknown, resolving the jnaForTest configuration directly
// trips a nasty issue with the Android-Gradle plugin 3.2.1, like `Cannot
// change attributes of configuration ':PROJECT:kapt' after it has been
// resolved`. I think that the configuration is being made a
// super-configuration of the testImplementation and then the `.files` is
// causing it to be resolved. Cloning first dissociates the configuration,
// avoiding other configurations from being resolved. Tricky!
testImplementation files(configurations.jnaForTest.copyRecursive().files)
// testImplementation "androidx.test.ext:junit:$versions.androidx_junit"
testImplementation "org.robolectric:robolectric:4.14.1"
}
afterEvaluate {
// The `cargoBuild` task isn't available until after evaluation.
android.applicationVariants.all { variant ->
def productFlavor = ""
variant.productFlavors.each {
productFlavor += "${it.name.capitalize()}"
}
def buildType = "${variant.buildType.name.capitalize()}"
tasks["generate${productFlavor}${buildType}Assets"].dependsOn(tasks["cargoBuild"])
// Don't merge the jni lib folders until after the Rust libraries have been built.
tasks["merge${productFlavor}${buildType}JniLibFolders"].dependsOn(tasks["cargoBuild"])
// For unit tests.
tasks["process${productFlavor}${buildType}UnitTestJavaRes"].dependsOn(tasks["cargoBuild"])
}
}