1818import jadx .core .utils .exceptions .JadxRuntimeException ;
1919import jadx .core .xmlgen .ResContainer ;
2020
21+ import static jadx .core .utils .Utils .safeParseInteger ;
22+
2123public class AndroidManifestParser {
2224 private final Document androidManifest ;
2325 private final @ Nullable Document appStrings ;
@@ -41,8 +43,7 @@ public boolean isManifestFound() {
4143 return androidManifest != null ;
4244 }
4345
44- @ Nullable
45- public static ResourceFile getAndroidManifest (List <ResourceFile > resources ) {
46+ public static @ Nullable ResourceFile getAndroidManifest (List <ResourceFile > resources ) {
4647 return resources .stream ()
4748 .filter (resourceFile -> resourceFile .getType () == ResourceType .MANIFEST )
4849 .findFirst ()
@@ -53,69 +54,59 @@ public ApplicationParams parse() {
5354 if (!isManifestFound ()) {
5455 throw new JadxRuntimeException ("AndroidManifest.xml is missing" );
5556 }
56-
5757 return parseAttributes ();
5858 }
5959
6060 private ApplicationParams parseAttributes () {
61- String applicationLabel = null ;
62- Integer minSdkVersion = null ;
63- Integer targetSdkVersion = null ;
64- Integer compileSdkVersion = null ;
65- Integer versionCode = null ;
66- String versionName = null ;
67- String mainActivity = null ;
68- String application = null ;
61+ ApplicationParams appParams = new ApplicationParams ();
6962
7063 @ Nullable
7164 Element manifest = (Element ) androidManifest .getElementsByTagName ("manifest" ).item (0 );
7265 @ Nullable
7366 Element usesSdk = (Element ) androidManifest .getElementsByTagName ("uses-sdk" ).item (0 );
7467
7568 if (parseAttrs .contains (AppAttribute .APPLICATION_LABEL )) {
76- applicationLabel = getApplicationLabel ();
69+ appParams . setApplicationLabel ( getApplicationLabel () );
7770 }
7871 if (usesSdk != null ) {
7972 if (parseAttrs .contains (AppAttribute .MIN_SDK_VERSION )) {
80- minSdkVersion = Integer . valueOf ( usesSdk .getAttribute ("android:minSdkVersion" ));
73+ appParams . setMinSdkVersion ( safeParseInteger ( usesSdk .getAttribute ("android:minSdkVersion" ) ));
8174 }
8275 if (parseAttrs .contains (AppAttribute .TARGET_SDK_VERSION )) {
8376 String stringTargetSdk = usesSdk .getAttribute ("android:targetSdkVersion" );
8477 if (!stringTargetSdk .isEmpty ()) {
85- targetSdkVersion = Integer . valueOf ( stringTargetSdk );
78+ appParams . setTargetSdkVersion ( safeParseInteger ( stringTargetSdk ) );
8679 } else {
87- if (minSdkVersion == null ) {
88- minSdkVersion = Integer . valueOf ( usesSdk .getAttribute ("android:minSdkVersion" ));
80+ if (appParams . getMinSdkVersion () == null ) {
81+ appParams . setMinSdkVersion ( safeParseInteger ( usesSdk .getAttribute ("android:minSdkVersion" ) ));
8982 }
90- targetSdkVersion = minSdkVersion ;
83+ appParams . setTargetSdkVersion ( appParams . getMinSdkVersion ()) ;
9184 }
9285 }
9386 if (parseAttrs .contains (AppAttribute .COMPILE_SDK_VERSION )) {
9487 String stringCompileSdk = usesSdk .getAttribute ("android:compileSdkVersion" );
9588 if (!stringCompileSdk .isEmpty ()) {
96- compileSdkVersion = Integer . valueOf ( stringCompileSdk );
89+ appParams . setCompileSdkVersion ( safeParseInteger ( stringCompileSdk ) );
9790 } else {
98- compileSdkVersion = targetSdkVersion ;
91+ appParams . setCompileSdkVersion ( appParams . getTargetSdkVersion ()) ;
9992 }
10093 }
10194 }
10295 if (manifest != null ) {
10396 if (parseAttrs .contains (AppAttribute .VERSION_CODE )) {
104- versionCode = Integer . valueOf ( manifest .getAttribute ("android:versionCode" ));
97+ appParams . setVersionCode ( safeParseInteger ( manifest .getAttribute ("android:versionCode" ) ));
10598 }
10699 if (parseAttrs .contains (AppAttribute .VERSION_NAME )) {
107- versionName = manifest .getAttribute ("android:versionName" );
100+ appParams . setVersionName ( manifest .getAttribute ("android:versionName" ) );
108101 }
109102 }
110103 if (parseAttrs .contains (AppAttribute .MAIN_ACTIVITY )) {
111- mainActivity = getMainActivityName ();
104+ appParams . setMainActivity ( getMainActivityName () );
112105 }
113106 if (parseAttrs .contains (AppAttribute .APPLICATION )) {
114- application = getApplicationName ();
107+ appParams . setApplication ( getApplicationName () );
115108 }
116-
117- return new ApplicationParams (applicationLabel , minSdkVersion , targetSdkVersion , compileSdkVersion ,
118- versionCode , versionName , mainActivity , application );
109+ return appParams ;
119110 }
120111
121112 private String getApplicationLabel () {
@@ -143,27 +134,26 @@ private String getApplicationLabel() {
143134 return appLabelName ;
144135 }
145136 }
146-
147137 return "UNKNOWN" ;
148138 }
149139
150- private String getMainActivityName () {
140+ private @ Nullable String getMainActivityName () {
151141 String mainActivityName = getMainActivityNameThroughActivityTag ();
152142 if (mainActivityName == null ) {
153143 mainActivityName = getMainActivityNameThroughActivityAliasTag ();
154144 }
155145 return mainActivityName ;
156146 }
157147
158- private String getApplicationName () {
148+ private @ Nullable String getApplicationName () {
159149 Element application = (Element ) androidManifest .getElementsByTagName ("application" ).item (0 );
160150 if (application .hasAttribute ("android:name" )) {
161151 return application .getAttribute ("android:name" );
162152 }
163153 return null ;
164154 }
165155
166- private String getMainActivityNameThroughActivityAliasTag () {
156+ private @ Nullable String getMainActivityNameThroughActivityAliasTag () {
167157 NodeList activityAliasNodes = androidManifest .getElementsByTagName ("activity-alias" );
168158 for (int i = 0 ; i < activityAliasNodes .getLength (); i ++) {
169159 Element activityElement = (Element ) activityAliasNodes .item (i );
@@ -174,7 +164,7 @@ private String getMainActivityNameThroughActivityAliasTag() {
174164 return null ;
175165 }
176166
177- private String getMainActivityNameThroughActivityTag () {
167+ private @ Nullable String getMainActivityNameThroughActivityTag () {
178168 NodeList activityNodes = androidManifest .getElementsByTagName ("activity" );
179169 for (int i = 0 ; i < activityNodes .getLength (); i ++) {
180170 Element activityElement = (Element ) activityNodes .item (i );
@@ -230,15 +220,15 @@ private Document parseXml(String xmlContent) {
230220 }
231221 }
232222
233- private Document parseAppStrings (ResContainer appStrings ) {
223+ private @ Nullable Document parseAppStrings (@ Nullable ResContainer appStrings ) {
234224 if (appStrings == null ) {
235225 return null ;
236226 }
237227 String content = appStrings .getText ().getCodeStr ();
238228 return parseXml (content );
239229 }
240230
241- private Document parseAndroidManifest (ResourceFile androidManifest ) {
231+ private @ Nullable Document parseAndroidManifest (ResourceFile androidManifest ) {
242232 if (androidManifest == null ) {
243233 return null ;
244234 }
0 commit comments