Modify build.gradle files for Android Studio

This guide describes how to modify build.gradle files from AGDE projects so you can open them in Android Studio. It is mainly intended for build engineers who manage AGDE projects.

After the changes in this guide, you should be able to:

  • Build with Gradle from the command-line and Android Studio.
  • Build multi-ABI APKs and App Bundles.
  • Edit sources with full language service support (go-to definition, etc.) in Android Studio.
  • Use Android Studio debuggers to debug native and mixed processes.

This AGDE feature is built on the experimental Android Gradle plugin feature to support Ninja as a build system.

Modify the project-level build.gradle file

Change the project-level build.gradle to refer to Android Gradle plugin version 7.3.0-alpha02 or later. For example:

buildscript {
    repositories {
       google()
       mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0-alpha02'
    }
}

After this change, you should still be able to build your project in Visual Studio. We recommend that you try that now since it will be easier to debug before making the changes below.

Copy run-msbuild.bat into your project

The Teapot sample that ships with the latest versions of AGDE has a script called run-msbuild.bat. This script will be called from AGP and is responsible for locating and invoking MSBuild.

Copy run-msbuild.bat to the folder that contains the app-level build.gradle file.

Modify the app-level build.gradle file

The main goals of this step are to configure the call to run-msbuild.bat and to remove references to MSBUILD_* variables.

Set ndkVersion

Change ndkVersion to a specific NDK version. For example:

  android {
      ndkVersion "22.1.7171670"
  }

Set minSdkVersion

Change minSdkVersion to a specific minimum SDK version. For example:

  android {
      defaultConfig {
          minSdkVersion 30
      }
  }

Invoke run-msbuild.bat

  1. Add a section to invoke run-msbuild.bat on the solution file for the project.

       android {
           defaultConfig {
               externalNativeBuild {
                   experimentalProperties["ninja.abiFilters"] = [ "x86", "arm64-v8a" ]
                   experimentalProperties["ninja.path"] = "Teapot.sln"
                   experimentalProperties["ninja.configure"] = "run-msbuild"
                   experimentalProperties["ninja.arguments"] = [
                           "\${ndk.moduleMakeFile}",
                           "-p:Configuration=\${ndk.variantName}",
                           "-p:Platform=Android-\${ndk.abi}",
                           "-p:NinjaBuildLocation=\${ndk.buildRoot}",
                           "-p:NinjaProject=GameApplication",
                           "-p:RequireAndroidNdkVersion=\${ndk.moduleNdkVersion}",
                           "-p:RequireMinSdkVersion=\${ndk.minPlatform}",
                           "-t:GenerateBuildNinja"
                    ]
               }
           }
       }
    
  2. Delete any uses of MSBUILD_JNI_LIBS_SRC_DIR, MSBUILD_ANDROID_OUTPUT_APK_NAME, and MSBUILD_ANDROID_GRADLE_BUILD_OUTPUT_DIR.

    Typically, the following blocks in the app-level build.gradle file can be deleted altogether.

       sourceSets {
           main {
               jniLibs.srcDirs = [MSBUILD_JNI_LIBS_SRC_DIR]
           }
       }
    
       applicationVariants.all { variant ->
           variant.outputs.all {
               outputFileName = MSBUILD_ANDROID_OUTPUT_APK_NAME
           }
       }
    
       buildDir = MSBUILD_ANDROID_GRADLE_BUILD_OUTPUT_DIR
    

Feedback

This feature is experimental, so feedback is appreciated. Here's how to provide it:

  • For general comments and feedback, you can add a comment to this bug.
  • To report a bug, open Android Studio, and click Help/Submit Feedback. Be sure to reference "Custom C/C++ Build Systems" to help direct the bug.
  • Click this link if you don't have Android Studio installed.