aboutsummaryrefslogtreecommitdiffstats
path: root/support/android/apk/build.gradle
blob: aa429b3da22400083cd0b54ea8ebf6c02399a584 (plain) (blame)
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
import org.apache.tools.ant.taskdefs.condition.Os

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
    }
}

allprojects {
    repositories {
        jcenter()
        flatDir {
            dirs rootDir.absolutePath + "/../../../target/android/aar"
        }
        google()
    }
}

// Utility methods
String getTargetDir(boolean debug, String arch) {
    def basePath = project.rootDir.getParentFile().getParentFile().getParentFile().absolutePath
    return basePath + '/target/android/' + getSubTargetDir(debug, arch)
}

String getSubTargetDir(boolean debug, String arch) {
    return getRustTarget(arch) + '/' + (debug ? 'debug' : 'release')
}

String getJniLibsPath(boolean debug, String arch) {
    return getTargetDir(debug, arch) + '/apk/jniLibs'
}

static String getRustTarget(String arch) {
    switch (arch.toLowerCase()) {
        case 'arm' : return 'arm-linux-androideabi'
        case 'armv7' : return 'armv7-linux-androideabi'
        case 'arm64' : return 'aarch64-linux-android'
        case 'x86' : return 'i686-linux-android'
        default: throw new GradleException("Invalid target architecture " + arch)
    }
}

static String getNDKAbi(String arch) {
    switch (arch.toLowerCase()) {
        case 'arm' : return 'armeabi'
        case 'armv7' : return 'armeabi-v7a'
        case 'arm64' : return 'arm64-v8a'
        case 'x86' : return 'x86'
        default: throw new GradleException("Invalid target architecture " + arch)
    }
}

String getNdkDir() {
    // Read environment variable used in rust build system
    String ndkDir = System.getenv('ANDROID_NDK')
    if (ndkDir == null) {
        ndkDir = System.getenv('ANDROID_NDK_HOME')
    }
    if (ndkDir == null) {
        ndkDir = System.getenv('ANDROID_NDK_ROOT')
    }
    if (ndkDir == null) {
        // Fallback to ndkDir in local.properties
        def rootDir = project.rootDir
        def localProperties = new File(rootDir, "local.properties")
        Properties properties = new Properties()
        localProperties.withInputStream { instr ->
            properties.load(instr)
        }

        ndkDir = properties.getProperty('ndk.dir')
    }

    def cmd = Os.isFamily(Os.FAMILY_WINDOWS) ? 'ndk-build.cmd' : 'ndk-build'
    def ndkbuild = new File(ndkDir + '/' + cmd)
    if (!ndkbuild.exists()) {
        throw new GradleException("Please set a valid NDK_HOME environment variable" +
                "or ndk.dir path in local.properties file");
    }
    return ndkbuild.absolutePath
}