Skip to content

Instantly share code, notes, and snippets.

@Themaister
Last active January 10, 2019 20:32
Show Gist options
  • Save Themaister/ce3958569362fb9d6c6f52c35590c7c0 to your computer and use it in GitHub Desktop.
Save Themaister/ce3958569362fb9d6c6f52c35590c7c0 to your computer and use it in GitHub Desktop.
Android Gradle basic setup for Vulkan

settings.gradle

include ':android' // Will pull in android/ folder as a subproject. I think you need this file even if you only have one project.

build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
                jcenter()
                google() // Need this to pull in Android plugins for Gradle properly.
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1' // This version is often something Android Studio will nag you about updating to latest version. It should match the Android Studio version you're on.

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}


allprojects {
    repositories {
        jcenter()
        google()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

android/build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26

    defaultConfig {
        minSdkVersion 26 // Can be changed. API 26 is Android O (8.0).
        targetSdkVersion 26 // Can be changed.

        ndk {
            abiFilters 'arm64-v8a' // Add more ABIs if you need.
        }
    }

    buildTypes {
        debug {
            externalNativeBuild {
                cmake {
                    arguments "-DANDROID_TOOLCHAIN=clang", // You can pass custom arguments to cmake here. -DANDROID_STL, -DANDROID_ARM_MODE, -DANDROID_CPP_FEATURES are good to use.
                            "-DANDROID_STL=c++_static",
                            "-DANDROID_ARM_MODE=arm",
                            "-DANDROID_CPP_FEATURES=exceptions",
                            "-DGRANITE_SHADER_COMPILER_OPTIMIZE=OFF",
                            "-DGRANITE_SPIRV_DUMP=OFF",
                            "-DCMAKE_BUILD_TYPE=Debug",
                            "-DANDROID_PLATFORM=android-26",
                            "-DGRANITE_AUDIO=OFF",
                            "-DANDROID_ARM_NEON=ON"

                    targets "myapp" // Which targets to build. Will create libmyapp.so. Will need to match up with AndroidManifest.xml later!
                }
            }
            jniDebuggable true // Makes APK debuggable. Unless you're releasing a signed APK to the public, you want this.
        }
        release {
            externalNativeBuild {
                cmake {
                    arguments "-DANDROID_TOOLCHAIN=clang",
                            "-DANDROID_STL=c++_static",
                            "-DANDROID_ARM_MODE=arm",
                            "-DANDROID_CPP_FEATURES=exceptions",
                            "-DGRANITE_SHADER_COMPILER_OPTIMIZE=OFF",
                            "-DGRANITE_SPIRV_DUMP=OFF",
                            "-DCMAKE_BUILD_TYPE=Release",
                            "-DANDROID_PLATFORM=android-26",
                            "-DGRANITE_AUDIO=OFF",
                            "-DANDROID_ARM_NEON=ON"

                    targets "myapp"
                }
            }
            debuggable true // Default for debug config, but not release config.
            signingConfig signingConfigs.debug // Normally, Gradle will expect
            jniDebuggable true // Make sure you can debug even release APKs.
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml' // Where is the AndroidManifest.xml relative to this file?
            resources.srcDirs = ['res'] // Where is the res/ directory relative to this file?
            res.srcDirs = ['res'] // Where is the res/ directory? (Not sure if this is redundant, but doesn't hurt ...)
            assets.srcDirs = ['../assets'] // Where can you find assets for application relative to this file? These will be packaged in the APK (use AAssetManager).
            jniLibs.srcDirs = ['../../application/platforms/android/renderdoc'] // If you have RenderDoc layer built or other 3rd party libraries, you can bundle it here!
        }
    }

    externalNativeBuild {
        cmake {
            path "../CMakeLists.txt" // Relative path from build.gradle to where your top-level CMakeLists.txt is found.
        }
    }
}

android/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mycompany.mygame"
      android:versionCode="1"
      android:versionName="1.0">

    <!-- Require Vulkan 1.0 -->
    <uses-feature android:name="android.hardware.vulkan.version" android:version="0x400003" android:required="true"/>
    <uses-feature android:name="android.hardware.vulkan.level" android:version="0" android:required="true"/>

    <application android:label="@string/app_name" android:icon="@drawable/icon"> <!-- Icon of the application -->
        <activity android:name="android.app.NativeActivity"
                  android:hasCode="false" <!-- No Java -->
                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen" <!-- Fullscreen app -->
                  android:launchMode="singleTask"
                  android:screenOrientation="landscape"
                  android:label="@string/app_name">
            <meta-data android:name="android.app.lib_name" android:value="myapp" /> <!-- Load libmyapp.so. This library must implement the entry points used for ANativeActivity. See android_app_glue! -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    
    <!-- Various permissions required, can be removed. -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

android/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <string name="app_name">My Vulkan Test</string> <!-- User visible app name on home screen -->
</resources>

android/res/values/drawable-mdpi/icon.png

48x48 PNG icon of your app

android/res/values/drawable-hdpi/icon.png

72x72 PNG icon of your app

android/res/values/drawable-xhdpi/icon.png

96x96 PNG icon of your app

android/res/values/drawable-xxhdpi/icon.png

144x144 PNG icon of your app

android/res/values/drawable-xxxhdpi/icon.png

192x192 PNG icon of your app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment