You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:能否将AppLovin SDK密钥存于JSON文件而非manifest.xml?

分离AppLovin SDK密钥并多构建配置管理的实现方案

下面是几种经过验证的可行方案,既能将敏感密钥与Manifest文件分离,也能方便不同构建环境的密钥管理:

方案一:local.properties + Gradle构建占位符

这是最常用的本地开发方案,local.properties默认被Git忽略,不会提交到版本库:

  1. 在项目根目录的local.properties中添加密钥配置:
    APPLOVIN_DEBUG_KEY=你的测试环境密钥
    APPLOVIN_RELEASE_KEY=你的正式环境密钥
    
  2. 在模块级build.gradle(或build.gradle.kts)中读取配置并传入Manifest占位符:
    Groovy版本:
    android {
        buildTypes {
            debug {
                def debugKey = properties.getProperty("APPLOVIN_DEBUG_KEY")
                manifestPlaceholders = [APPLOVIN_SDK_KEY: debugKey]
            }
            release {
                def releaseKey = properties.getProperty("APPLOVIN_RELEASE_KEY")
                manifestPlaceholders = [APPLOVIN_SDK_KEY: releaseKey]
            }
        }
    }
    
    Kotlin DSL版本:
    android {
        buildTypes {
            debug {
                val debugKey = properties.getProperty("APPLOVIN_DEBUG_KEY")
                manifestPlaceholders["APPLOVIN_SDK_KEY"] = debugKey ?: ""
            }
            release {
                val releaseKey = properties.getProperty("APPLOVIN_RELEASE_KEY")
                manifestPlaceholders["APPLOVIN_SDK_KEY"] = releaseKey ?: ""
            }
        }
    }
    
  3. 在AndroidManifest.xml中引用占位符:
    <meta-data
        android:name="com.applovin.sdk.ApiKey"
        android:value="${APPLOVIN_SDK_KEY}" />
    

方案二:系统环境变量 + Gradle

适合CI/CD环境或不想在本地文件存储密钥的场景:

  1. 在系统环境变量中配置APPLOVIN_DEBUG_KEY和APPLOVIN_RELEASE_KEY
  2. 在模块级Gradle文件中读取环境变量:
    Groovy版本:
    android {
        buildTypes {
            debug {
                manifestPlaceholders = [APPLOVIN_SDK_KEY: System.getenv("APPLOVIN_DEBUG_KEY")]
            }
            release {
                manifestPlaceholders = [APPLOVIN_SDK_KEY: System.getenv("APPLOVIN_RELEASE_KEY")]
            }
        }
    }
    
  3. 同样在Manifest中引用${APPLOVIN_SDK_KEY}占位符即可

方案三:自定义Gradle属性文件

如果需要统一管理多个敏感配置,可以创建专属属性文件:

  1. 在项目根目录创建gradle-local.properties,加入密钥配置,并将该文件添加到.gitignore
  2. 在settings.gradle中加载该属性文件:
    def localPropsFile = file("gradle-local.properties")
    if (localPropsFile.exists()) {
        def props = new Properties()
        props.load(localPropsFile.newReader("UTF-8"))
        props.each { k, v -> ext[k] = v }
    }
    
  3. 在模块级Gradle中使用ext.APPLOVIN_DEBUG_KEY获取值,再配置Manifest占位符

注意事项

  • 确保所有存储敏感密钥的文件都被加入.gitignore,避免密钥泄露
  • 对于正式环境的安全性,建议结合CI/CD的环境变量管理,不要在本地存储生产密钥
  • 如果需要运行时动态获取密钥(而非Manifest配置),可以使用Android Keystore存储,但AppLovin SDK的ApiKey通常只需构建时注入Manifest即可

内容的提问来源于stack exchange,提问作者kamal zawa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 23:17:31