flutter_env_native는 네이티브 Android 및 iOS 플랫폼용 컴파일 시 변수 사용을 가능하게 하는 Flutter 플러그인입니다. 개발자는 API 키나 구성 플래그와 같은 환경별 값을 컴파일 시 앱의 네이티브 코드에 안전하게 내장할 수 있습니다. 이 접근 방식은 민감한 데이터가 런타임에 노출되는 것을 방지하여 보안을 강화합니다. 이 패키지는 기존의 Flutter 프로젝트와 원활하게 통합되며, 전용 빌드 시간 메커니즘을 통해 플랫폼별 구성 설정을 지원합니다.
사용 사례
네이티브 빌드에서 API 키를 안전하게 관리하기
컴파일 시 환경별 설정 구성
네이티브 플랫폼 고유 로직과의 통합
런타임 시크릿을 줄여 앱 보안 강화
다중 환경에 걸친 배포 간소화
주요 기능
컴파일 시점 변수 주입
Android 및 iOS 플랫폼 지원
실행 시 비밀 감소를 통한 보안 향상
Flutter 빌드 프로세스와의 원활한 통합
플랫폼별 구성 처리
적합한 대상
앱 보안을 우선시하는 개발자
여러 배포 환경을 관리하는 팀
네이티브 수준의 구성이 필요한 프로젝트
민감한 자격 증명을 사용하는 앱
결정적 구성이 필요한 파이프라인 빌드
고려 사항
네이티브 빌드 구성이 필요합니다
컴파일 시점에만 가능한 값
동적 런타임 변경에 적합하지 않습니다
빌드 복잡성을 증가시킬 수 있습니다
네이티브 프로젝트 파일의 올바른 설정에 따라 다릅니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
flutter_env_native
A plugin/utility that provides compile-time variables for native platform.
This plugin/utility makes env variables set in --dart-define and --dart-define-from-file available to the native platforms.
Pass .env file to flutter run/build via --dart-define-from-file=.env
[!WARNING]
In iOS *.xcconfig has some limitations when it comes to variable values it treats the sequence // as a comment delimiter, then for example, https://example.com, will be https:// in the code, everything after // will be ignored. For solve omit the https:// while specifying the env variable and prepend the https:// in code, read more about the solution here.
Android Installation
:open_file_folder: android/app/build.gradle
:exclamation: DO NOT OMIT ANY OF THE FOLLOWING CHANGES :exclamation:
:exclamation: DO NOT OMIT ANY OF THE FOLLOWING CHANGES :exclamation:
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "tech.mastersam.flutter_env_example"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutterVersionCode.toInteger()
versionName = flutterVersionName
+ resValue "string", "app_name", APP_NAME ?: 'default_name'
}
The above create a string resource with variable app_name set to the value of APP_NAME
:exclamation: DO NOT OMIT ANY OF THE FOLLOWING CHANGES :exclamation:
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId = "tech.mastersam.flutter_env_example"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutterVersionCode.toInteger()
versionName = flutterVersionName
+ buildConfigField "String", "APP_NAME", "\"${APP_NAME ?: 'default_name'}\""
}
The above creates the BuildConfig field APP_NAME and sets its value to the env variable APP_NAME where available and defaults it to 'default_name'
:open_file_folder: android/.../MainActivity.kt:
:exclamation: DO NOT OMIT ANY OF THE FOLLOWING CHANGES :exclamation:
package tech.mastersam.flutter_env_example
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Access BuildConfig values
+ val appName = BuildConfig.APP_NAME
// Sample usage to display a toast message
Toast.makeText(this, "APP_NAME: $appName", Toast.LENGTH_LONG).show()
}
}
iOS Installation
:open_file_folder: ios/Podfile
:exclamation: DO NOT OMIT ANY OF THE FOLLOWING CHANGES :exclamation:
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
end
+ flutter_env_plugin_path = File.join(Dir.pwd, '.symlinks', 'plugins', 'flutter_env_native', 'ios', 'setup_env.sh')+ root_dir = File.expand_path('..', Dir.pwd)+ # Run the setup_env.sh script from the plugin+ system("sh \"#{flutter_env_plugin_path}\" \"#{root_dir}\"")
end
Usage in plist file
:open_file_folder: info.plist:
Reference the env variable directly as defined in the .env file