[JAVA] [Note] Basic information setting (gradle)

Back

gradle settings

It seems that the basic setting information of the application is managed by gradle and manifests. It's okay to stick to manifests, but it seems better to use gradle

Set value in gradle.properties

In the flow

  1. Add value to gradle.properties (project property)
  2. Describe to get the value of properties in build.gradle (module: app) By doing this, it will generate the necessary values at build time.

Try adding version information of the application itself and the internal DB

gradle.properties


   #Basic information of the app
   version_major=1
   version_minor=0
   db_name=DB_name
   db_version=1

It looks like this image.png

Get value with build.gradle

To receive the value added to gradle.properties above, write like this. image.png

build.gradle


        versionName = project.property("version_major") + "." + project.property("version_minor");
        archivesBaseName = "samplepack-" + defaultConfig.versionName

In the line above, combine the values of version_major and version_minor Generates the versionName of the app. The next line uses versionName to generate the archivesBaseName. This value is used for the file name when the app is built. By the way, it seems that the defaultConfig part where these two lines are written can only register the value to the variable originally defined in the application.

Next, for buildTypes,

build.gradle


    buildTypes {
        debug {
            buildConfigField "String", "dbName", "\"${project.property("db_name")}_DEV\""
            buildConfigField "String", "dbVersion", "\"${project.property("db_version")}\""
            buildConfigField "String", "env", "\"DEV\""
        }
        release {
            buildConfigField "String", "dbName", "\"${project.property("db_name")}_PRD\""
            buildConfigField "String", "dbVersion", "\"${project.property("db_version")}\""
            buildConfigField "String", "env", "\"PRD\""
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

Here, unlike defaultConfig, you can freely create values. Especially grateful is the ability to generate different values for debug and release respectively. In this case, the description of _DEV / _PRD is added after the DB name, The contents of the variable called env are switched by DEV / PRD. This makes it easier to connect to different data sources and URLs for development and operation. *** Can be managed by one source ***.

Generate BuildConfig.java

With the settings up to this point, ~ \ app \ build \ generated \ source \ buildConfig \ ~` `` A file called `` `BuildConfig.java is generated in

BuildConfig.java


/**
 * Automatically generated file. DO NOT MODIFY
 */
package sample.pack;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "sample.pack";
  public static final String BUILD_TYPE = "debug";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
  // Fields from build type: debug
  public static final String dbName = "DB_name_DEV";
  public static final String dbVersion = "1";
  public static final String env = "DEV";
}

This value can be retrieved anywhere with new.

MainActivity.java


        //App information display
        BuildConfig conf = new BuildConfig();

        Log.d("package name",conf.APPLICATION_ID);
        Log.d("version name",conf.VERSION_NAME);
        Log.d("DB name",conf.dbName);
        Log.d("DB version",conf.dbVersion);
        Log.d("environment",conf.env);

When you output the log, it looks like this image.png

Back

Recommended Posts

[Note] Basic information setting (gradle)
[Devise] rails memo basic setting initial setting
Note: setting javaMail on Raspberry Pi