Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
J
jadx
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
jadx
Commits
6116a750
Commit
6116a750
authored
Mar 12, 2019
by
Skylot
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: rename R fields using resource names (#465)
parent
7243ab5c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
11 deletions
+49
-11
AndroidResourcesUtils.java
...n/java/jadx/core/utils/android/AndroidResourcesUtils.java
+49
-11
No files found.
jadx-core/src/main/java/jadx/core/utils/android/AndroidResourcesUtils.java
View file @
6116a750
package
jadx
.
core
.
utils
.
android
;
package
jadx
.
core
.
utils
.
android
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.TreeMap
;
import
java.util.TreeMap
;
import
com.android.dx.rop.code.AccessFlags
;
import
com.android.dx.rop.code.AccessFlags
;
import
org.jetbrains.annotations.NotNull
;
import
org.jetbrains.annotations.Nullable
;
import
org.jetbrains.annotations.Nullable
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
jadx.core.codegen.ClassGen
;
import
jadx.core.codegen.ClassGen
;
import
jadx.core.codegen.CodeWriter
;
import
jadx.core.codegen.CodeWriter
;
import
jadx.core.deobf.NameMapper
;
import
jadx.core.dex.attributes.AType
;
import
jadx.core.dex.attributes.AType
;
import
jadx.core.dex.info.AccessInfo
;
import
jadx.core.dex.info.ClassInfo
;
import
jadx.core.dex.info.ClassInfo
;
import
jadx.core.dex.info.ConstStorage
;
import
jadx.core.dex.info.FieldInfo
;
import
jadx.core.dex.info.FieldInfo
;
import
jadx.core.dex.instructions.args.ArgType
;
import
jadx.core.dex.instructions.args.ArgType
;
import
jadx.core.dex.nodes.ClassNode
;
import
jadx.core.dex.nodes.ClassNode
;
...
@@ -86,6 +91,7 @@ public class AndroidResourcesUtils {
...
@@ -86,6 +91,7 @@ public class AndroidResourcesUtils {
}
}
private
static
void
addResourceFields
(
ClassNode
resCls
,
ResourceStorage
resStorage
,
boolean
rClsExists
)
{
private
static
void
addResourceFields
(
ClassNode
resCls
,
ResourceStorage
resStorage
,
boolean
rClsExists
)
{
Map
<
Integer
,
FieldNode
>
resFieldsMap
=
fillResFieldsMap
(
resCls
);
Map
<
String
,
ClassNode
>
innerClsMap
=
new
TreeMap
<>();
Map
<
String
,
ClassNode
>
innerClsMap
=
new
TreeMap
<>();
if
(
rClsExists
)
{
if
(
rClsExists
)
{
for
(
ClassNode
innerClass
:
resCls
.
getInnerClasses
())
{
for
(
ClassNode
innerClass
:
resCls
.
getInnerClasses
())
{
...
@@ -93,18 +99,14 @@ public class AndroidResourcesUtils {
...
@@ -93,18 +99,14 @@ public class AndroidResourcesUtils {
}
}
}
}
for
(
ResourceEntry
resource
:
resStorage
.
getResources
())
{
for
(
ResourceEntry
resource
:
resStorage
.
getResources
())
{
ClassNode
typeCls
=
innerClsMap
.
computeIfAbsent
(
resource
.
getTypeName
(),
name
->
{
ClassNode
typeCls
=
innerClsMap
.
computeIfAbsent
(
ClassNode
newTypeCls
=
new
ClassNode
(
resCls
.
dex
(),
resCls
.
getFullName
()
+
"$"
+
name
,
resource
.
getTypeName
(),
AccessFlags
.
ACC_PUBLIC
|
AccessFlags
.
ACC_STATIC
|
AccessFlags
.
ACC_FINAL
);
name
->
addClassForResType
(
resCls
,
rClsExists
,
name
)
resCls
.
addInnerClass
(
newTypeCls
);
);
if
(
rClsExists
)
{
String
resName
=
resource
.
getKeyName
();
newTypeCls
.
addAttr
(
AType
.
COMMENTS
,
"added by JADX"
);
FieldNode
rField
=
typeCls
.
searchFieldByName
(
resName
);
}
return
newTypeCls
;
});
FieldNode
rField
=
typeCls
.
searchFieldByName
(
resource
.
getKeyName
());
if
(
rField
==
null
)
{
if
(
rField
==
null
)
{
FieldInfo
rFieldInfo
=
FieldInfo
.
from
(
typeCls
.
dex
(),
typeCls
.
getClassInfo
(),
res
ource
.
getKeyName
()
,
ArgType
.
INT
);
FieldInfo
rFieldInfo
=
FieldInfo
.
from
(
typeCls
.
dex
(),
typeCls
.
getClassInfo
(),
res
Name
,
ArgType
.
INT
);
rField
=
new
FieldNode
(
typeCls
,
rFieldInfo
,
AccessFlags
.
ACC_PUBLIC
|
AccessFlags
.
ACC_STATIC
|
AccessFlags
.
ACC_FINAL
);
rField
=
new
FieldNode
(
typeCls
,
rFieldInfo
,
AccessFlags
.
ACC_PUBLIC
|
AccessFlags
.
ACC_STATIC
|
AccessFlags
.
ACC_FINAL
);
rField
.
addAttr
(
FieldInitAttr
.
constValue
(
resource
.
getId
()));
rField
.
addAttr
(
FieldInitAttr
.
constValue
(
resource
.
getId
()));
typeCls
.
getFields
().
add
(
rField
);
typeCls
.
getFields
().
add
(
rField
);
...
@@ -112,6 +114,42 @@ public class AndroidResourcesUtils {
...
@@ -112,6 +114,42 @@ public class AndroidResourcesUtils {
rField
.
addAttr
(
AType
.
COMMENTS
,
"added by JADX"
);
rField
.
addAttr
(
AType
.
COMMENTS
,
"added by JADX"
);
}
}
}
}
FieldNode
fieldNode
=
resFieldsMap
.
get
(
resource
.
getId
());
if
(
fieldNode
!=
null
&&
!
fieldNode
.
getName
().
equals
(
resName
)
&&
NameMapper
.
isValidIdentifier
(
resName
))
{
fieldNode
.
getFieldInfo
().
setAlias
(
resName
);
}
}
}
@NotNull
private
static
ClassNode
addClassForResType
(
ClassNode
resCls
,
boolean
rClsExists
,
String
typeName
)
{
ClassNode
newTypeCls
=
new
ClassNode
(
resCls
.
dex
(),
resCls
.
getFullName
()
+
"$"
+
typeName
,
AccessFlags
.
ACC_PUBLIC
|
AccessFlags
.
ACC_STATIC
|
AccessFlags
.
ACC_FINAL
);
resCls
.
addInnerClass
(
newTypeCls
);
if
(
rClsExists
)
{
newTypeCls
.
addAttr
(
AType
.
COMMENTS
,
"added by JADX"
);
}
return
newTypeCls
;
}
@NotNull
private
static
Map
<
Integer
,
FieldNode
>
fillResFieldsMap
(
ClassNode
resCls
)
{
Map
<
Integer
,
FieldNode
>
resFieldsMap
=
new
HashMap
<>();
ConstStorage
constStorage
=
resCls
.
root
().
getConstValues
();
Map
<
Object
,
FieldNode
>
constFields
=
constStorage
.
getGlobalConstFields
();
for
(
Map
.
Entry
<
Object
,
FieldNode
>
entry
:
constFields
.
entrySet
())
{
Object
key
=
entry
.
getKey
();
FieldNode
field
=
entry
.
getValue
();
AccessInfo
accessFlags
=
field
.
getAccessFlags
();
if
(
field
.
getType
().
equals
(
ArgType
.
INT
)
&&
accessFlags
.
isStatic
()
&&
accessFlags
.
isFinal
()
&&
key
instanceof
Integer
)
{
resFieldsMap
.
put
((
Integer
)
key
,
field
);
}
}
}
return
resFieldsMap
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment