Skip to content

Commit b300cec

Browse files
committed
lib support
1 parent 344a936 commit b300cec

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

README.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Gradle implementation
1616
buildscript {
1717
1818
ext {
19-
stringcare_version = '0.7'
19+
stringcare_version = '0.8'
2020
}
2121
2222
repositories {
@@ -46,11 +46,17 @@ dependencies {
4646

4747
Setup
4848
-----
49-
Initialize the library:
49+
StringCare library needs the global application's `Context` for access to `PackageManager` and get signatures.
50+
In your `app` (or main) module the package name is obtained from `Context`:
5051
```java
5152
SC.init(getApplicationContext());
5253
```
5354

55+
In the rest of modules (or libraries) you must pass an `Object` in order to obtain its package name:
56+
```java
57+
SC.initForLib(getApplicationContext(), this);
58+
```
59+
5460

5561
#### Encrypt
5662
The plugin will encrypt all string tags with `hidden="true"` as attribute.
@@ -106,6 +112,44 @@ message += "\n\nFor Metal Gear lovers:\n\n\"Snake, the password is " +
106112

107113
<p align="center"><img width="40%" vspace="20" src="https://raw.githubusercontent.com/efraespada/AndroidStringObfuscator/master/sample.png"></p>
108114

115+
Library Sample
116+
--------------
117+
If
118+
```java
119+
public class YourApplication extends Application {
120+
121+
@Override
122+
public void onCreate() {
123+
super.onCreate();
124+
Library.init(this);
125+
String secretPass = Library.getPassword(); // should return -> =^UCrE4zR#}kpCu~
126+
}
127+
128+
}
129+
```
130+
In library package:
131+
```java
132+
public class Library {
133+
134+
private Library() {
135+
// ..
136+
}
137+
138+
public static void init(Context context) {
139+
SC.initForLib(context, new Library());
140+
}
141+
142+
public static String getPassword() {
143+
return SC.getString(your.lib.module.R.string.pass);
144+
}
145+
146+
}
147+
```
148+
```xml
149+
<resources>
150+
<string name="pass" hidden="true">=^UCrE4zR#}kpCu~</string>
151+
</resources>
152+
```
109153

110154
Configuration
111155
-----------------------------

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22
apply plugin: 'com.github.dcendents.android-maven'
33
apply plugin: 'com.jfrog.bintray'
44

5-
version = "0.7"
5+
version = "0.8"
66

77
android {
88
compileSdkVersion 25
@@ -11,7 +11,7 @@ android {
1111
defaultConfig {
1212
minSdkVersion 9
1313
targetSdkVersion 25
14-
versionCode 2
14+
versionCode 3
1515
versionName version
1616

1717
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

library/src/main/java/com/stringcare/library/SC.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
public class SC {
3333

3434
private static final int LENGTH = 16;
35+
private static String libPackage;
3536
private static final String CODIFICATION = "UTF-8";
3637
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
3738
private static final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
@@ -42,9 +43,18 @@ public static void init(Context c) {
4243
context = c;
4344
}
4445

46+
public static void initForLib(Context c, Object object) {
47+
context = c;
48+
libPackage = object.getClass().getPackage().getName();
49+
}
50+
4551
private static String getCertificateSHA1Fingerprint() {
46-
PackageManager pm = context.getPackageManager();
4752
String packageName = context.getPackageName();
53+
return getCertificateSHA1Fingerprint(libPackage != null ? libPackage : packageName);
54+
}
55+
56+
private static String getCertificateSHA1Fingerprint(String packageName) {
57+
PackageManager pm = context.getPackageManager();
4858
int flags = PackageManager.GET_SIGNATURES;
4959
PackageInfo packageInfo = null;
5060
try {

sample/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ dependencies {
3636
})
3737
implementation 'com.android.support:appcompat-v7:26.1.0'
3838
testImplementation 'junit:junit:4.12'
39-
// implementation project(path: ':library')
40-
implementation "com.stringcare:library:$stringcare_version"
39+
implementation project(path: ':library')
40+
// implementation "com.stringcare:library:$stringcare_version"
4141
}
4242

4343

sample/src/main/java/com/efraespada/stringobfuscator/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ protected void onCreate(Bundle savedInstanceState) {
1414
setContentView(R.layout.activity_main);
1515

1616
SC.init(getApplicationContext());
17+
// SC.initForLib(getApplicationContext(), this);
1718

1819
int stringId = R.string.hello;
1920

0 commit comments

Comments
 (0)