Aus öffentlichen Paketdaten erstellte Orientierung.
flutter_env_native ist ein Flutter-Plugin, das die Verwendung von Kompilierzeitvariablen für native Android- und iOS-Plattformen ermöglicht. Es erlaubt Entwicklern, umgebungsbezogene Werte – wie API-Schlüssel oder Konfigurationsflags – sicher direkt in den nativen Code der App während der Kompilierung einzubetten. Dieser Ansatz erhöht die Sicherheit, indem die Exposition sensibler Daten zur Laufzeit vermieden wird. Das Paket lässt sich nahtlos in bestehende Flutter-Projekte integrieren und unterstützt plattformspezifische Konfigurationen über dedizierte Build-Mechanismen.
Anwendungsfälle
Sicheres Verwalten von API-Schlüsseln in Native Builds
Umgebungsbezogene Einstellungen zur Kompilierzeit konfigurieren
Integration mit plattformspezifischer Native-Logik
Verbesserung der App-Sicherheit durch Reduzierung von Laufzeitgeheimnissen
Bereitstellung über mehrere Umgebungen vereinfachen
Hauptmerkmale
Variableninjektion zur Kompilierzeit
Unterstützung für Android- und iOS-Plattformen
Verbesserte Sicherheit durch reduzierte Laufzeit-Geheimnisse
Nahtlose Integration in den Flutter-Build-Prozess
Plattformspezifische Konfigurationsverwaltung
Geeignet für
Entwickler, die der App-Sicherheit Priorität einräumen
Teams, die mehrere Bereitstellungsumgebungen verwalten
Projekte, die native Konfiguration erfordern
Apps, die sensible Anmeldeinformationen verwenden
Build-Pipelines mit deterministischer Konfiguration erstellen
Hinweise
Benötigt native Build-Konfiguration
Nur auf Werte zur Kompilierzeit beschränkt
Nicht geeignet für dynamische Laufzeitänderungen
Kann die Baugewichtung erhöhen
Abhängig von der korrekten Einrichtung in den nativen Projektdateien
Original-README
Originaler README-Schnappschuss aus dem verknüpften Quell-Repository. Aktuelle Inhalte finden Sie im Repository.
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