diff --git a/protoc_plugin/lib/src/file_generator.dart b/protoc_plugin/lib/src/file_generator.dart
index a41ad93df..825018bcb 100644
--- a/protoc_plugin/lib/src/file_generator.dart
+++ b/protoc_plugin/lib/src/file_generator.dart
@@ -270,6 +270,10 @@ class FileGenerator extends ProtobufContainer {
 
     writeMainHeader(out, config);
 
+    if (package.isNotEmpty) {
+      out.println("const \$core.String kPackageName = '$package';\n");
+    }
+
     // Generate code.
     for (final m in messageGenerators) {
       m.generate(out);
diff --git a/protoc_plugin/test/file_generator_test.dart b/protoc_plugin/test/file_generator_test.dart
index f739cbc52..ff0d842b1 100644
--- a/protoc_plugin/test/file_generator_test.dart
+++ b/protoc_plugin/test/file_generator_test.dart
@@ -14,7 +14,9 @@ import 'golden_file.dart';
 
 FileDescriptorProto buildFileDescriptor(
     {bool phoneNumber = true, bool topLevelEnum = false}) {
-  final fd = FileDescriptorProto()..name = 'test';
+  final fd = FileDescriptorProto()
+    ..name = 'test'
+    ..package = 'testPackage';
 
   if (topLevelEnum) {
     fd.enumType.add(EnumDescriptorProto()
diff --git a/protoc_plugin/test/goldens/oneMessage.pb b/protoc_plugin/test/goldens/oneMessage.pb
index 4bde34492..37aed85ec 100644
--- a/protoc_plugin/test/goldens/oneMessage.pb
+++ b/protoc_plugin/test/goldens/oneMessage.pb
@@ -13,13 +13,15 @@ import 'dart:core' as $core;
 
 import 'package:protobuf/protobuf.dart' as $pb;
 
+const $core.String kPackageName = 'testPackage';
+
 class PhoneNumber extends $pb.GeneratedMessage {
   factory PhoneNumber() => create();
   PhoneNumber._() : super();
   factory PhoneNumber.fromBuffer($core.List<$core.int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromBuffer(i, r);
   factory PhoneNumber.fromJson($core.String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) => create()..mergeFromJson(i, r);
 
-  static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'PhoneNumber', createEmptyInstance: create)
+  static final $pb.BuilderInfo _i = $pb.BuilderInfo(_omitMessageNames ? '' : 'PhoneNumber', package: const $pb.PackageName(_omitMessageNames ? '' : 'testPackage'), createEmptyInstance: create)
     ..aQS(1, _omitFieldNames ? '' : 'number')
     ..a<$core.int>(2, _omitFieldNames ? '' : 'type', $pb.PbFieldType.O3)
     ..a<$core.String>(3, _omitFieldNames ? '' : 'name', $pb.PbFieldType.OS, defaultOrMaker: '\$')
diff --git a/protoc_plugin/test/goldens/oneMessage.pb.meta b/protoc_plugin/test/goldens/oneMessage.pb.meta
index fec2f3576..87c4e3098 100644
--- a/protoc_plugin/test/goldens/oneMessage.pb.meta
+++ b/protoc_plugin/test/goldens/oneMessage.pb.meta
@@ -2,15 +2,15 @@ annotation: {
   path: 4
   path: 0
   sourceFile: test
-  begin: 434
-  end: 445
+  begin: 484
+  end: 495
 }
 annotation: {
   path: 4
   path: 0
   sourceFile: test
-  begin: 516
-  end: 527
+  begin: 566
+  end: 577
 }
 annotation: {
   path: 4
@@ -18,8 +18,8 @@ annotation: {
   path: 2
   path: 0
   sourceFile: test
-  begin: 2236
-  end: 2242
+  begin: 2358
+  end: 2364
 }
 annotation: {
   path: 4
@@ -27,8 +27,8 @@ annotation: {
   path: 2
   path: 0
   sourceFile: test
-  begin: 2284
-  end: 2290
+  begin: 2406
+  end: 2412
 }
 annotation: {
   path: 4
@@ -36,8 +36,8 @@ annotation: {
   path: 2
   path: 0
   sourceFile: test
-  begin: 2363
-  end: 2372
+  begin: 2485
+  end: 2494
 }
 annotation: {
   path: 4
@@ -45,8 +45,8 @@ annotation: {
   path: 2
   path: 0
   sourceFile: test
-  begin: 2415
-  end: 2426
+  begin: 2537
+  end: 2548
 }
 annotation: {
   path: 4
@@ -54,8 +54,8 @@ annotation: {
   path: 2
   path: 1
   sourceFile: test
-  begin: 2484
-  end: 2488
+  begin: 2606
+  end: 2610
 }
 annotation: {
   path: 4
@@ -63,8 +63,8 @@ annotation: {
   path: 2
   path: 1
   sourceFile: test
-  begin: 2530
-  end: 2534
+  begin: 2652
+  end: 2656
 }
 annotation: {
   path: 4
@@ -72,8 +72,8 @@ annotation: {
   path: 2
   path: 1
   sourceFile: test
-  begin: 2609
-  end: 2616
+  begin: 2731
+  end: 2738
 }
 annotation: {
   path: 4
@@ -81,8 +81,8 @@ annotation: {
   path: 2
   path: 1
   sourceFile: test
-  begin: 2659
-  end: 2668
+  begin: 2781
+  end: 2790
 }
 annotation: {
   path: 4
@@ -90,8 +90,8 @@ annotation: {
   path: 2
   path: 2
   sourceFile: test
-  begin: 2729
-  end: 2733
+  begin: 2851
+  end: 2855
 }
 annotation: {
   path: 4
@@ -99,8 +99,8 @@ annotation: {
   path: 2
   path: 2
   sourceFile: test
-  begin: 2780
-  end: 2784
+  begin: 2902
+  end: 2906
 }
 annotation: {
   path: 4
@@ -108,8 +108,8 @@ annotation: {
   path: 2
   path: 2
   sourceFile: test
-  begin: 2857
-  end: 2864
+  begin: 2979
+  end: 2986
 }
 annotation: {
   path: 4
@@ -117,6 +117,6 @@ annotation: {
   path: 2
   path: 2
   sourceFile: test
-  begin: 2907
-  end: 2916
+  begin: 3029
+  end: 3038
 }
diff --git a/protoc_plugin/test/goldens/topLevelEnum.pb b/protoc_plugin/test/goldens/topLevelEnum.pb
index 1edf66280..bc5e0eacf 100644
--- a/protoc_plugin/test/goldens/topLevelEnum.pb
+++ b/protoc_plugin/test/goldens/topLevelEnum.pb
@@ -13,3 +13,5 @@ import 'dart:core' as $core;
 
 export 'test.pbenum.dart';
 
+const $core.String kPackageName = 'testPackage';
+