// React Native library build config for @layers/react-native.
//
// Without this file, React Native's autolinking step silently skips the
// `android/` directory and none of the native modules defined under
// LayersReactNativePackage are compiled into consumer apps. That means
// NativeModules.LayersInstallReferrer / LayersAdServices /
// LayersDeviceInfo / LayersAdvertisingId all resolve to undefined
// at runtime, and getInstallReferrer() / getAdServicesToken() / etc. always
// return null — silently breaking Android install referrer attribution.

buildscript {
  repositories {
    google()
    mavenCentral()
  }
  dependencies {
    // Pinned to match React Native 0.73+ toolchain; consumers may override.
    classpath 'com.android.tools.build:gradle:8.1.1'
  }
}

apply plugin: 'com.android.library'

def safeExtGet(prop, fallback) {
  rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

android {
  namespace 'io.layers.sdk.reactnative'
  compileSdkVersion safeExtGet('compileSdkVersion', 34)

  defaultConfig {
    // Fallback minSdkVersion matches React Native's historical documented
    // minimum (21). The consumer app's minSdkVersion wins via safeExtGet,
    // so projects that explicitly set 23+ get their choice; projects that
    // don't override it aren't forced past 21, which would break RN apps
    // still targeting API 21/22 at autolinking time.
    minSdkVersion safeExtGet('minSdkVersion', 21)
    targetSdkVersion safeExtGet('targetSdkVersion', 34)

    // Propagates consumer-rules.pro into every consuming app's R8 run. Both
    // the advertising-ID and install-referrer APIs are reached via
    // Class.forName, which gives R8 no static reference to keep them alive.
    consumerProguardFiles 'consumer-rules.pro'
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }
}

repositories {
  google()
  mavenCentral()
}

dependencies {
  implementation "com.facebook.react:react-native:+"

  // Google Play Install Referrer API — required for LayersInstallReferrerModule
  // to fetch the referrer URL (fbclid, ttclid, gclid, etc.) passed from /c/:appId.
  // Reflection-based lookup in the module means this is technically optional, but
  // without the library the referrer fetch always returns null. Declared here as
  // a first-class dependency so consumers don't have to remember to add it.
  implementation 'com.android.installreferrer:installreferrer:2.2'

  // Google Advertising ID — required for LayersAdvertisingIdModule.
  //
  // WHY THIS IS DECLARED (2026-08-04): it was not, while its sibling above was,
  // and the comment above states exactly the principle that was missed —
  // "Declared here as a first-class dependency so consumers don't have to
  // remember to add it." LayersAdvertisingIdModule reaches AdvertisingIdClient
  // reflectively and swallows ClassNotFoundException, so in a stock RN or Expo
  // app the class was never on the classpath, the GAID resolved to null on
  // every call, and nothing logged.
  //
  // The AAR also declares <uses-permission
  // android:name="com.google.android.gms.permission.AD_ID"/>, which Android
  // 13+ / targetSdk 33+ requires to read the GAID at all, so that permission
  // merges into the host app transitively. Version matches the Kotlin and
  // Flutter SDK pins so one app cannot end up with two Play Services majors.
  implementation 'com.google.android.gms:play-services-ads-identifier:18.1.0'
}
