Android: Cant find .html file in res/raw after obfuscation - android

I have some html files in res/raw which I open in WebView. But after obfuscation they are unable to load.

I ran into exactly this same issue. I have my help html file in raw and after obfuscation I run my app and get an error that the file could not be found.
Here is my HelpActivity class:
public class HelpActivity extends BaseActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// requesting to turn the title OFF
//requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.help);
setTitle(getString(R.string.help_title));
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("file:///android_" + getString(R.raw.how_to_play_zeewee));
}
}
I fixed this issue by adding the following to my proguard.cfg file:
-keepclassmembers class **.R$* {public static <fields>;}
-keep class **.R$*
You probably already have the first line, but this does not prevent the issue. Adding the second line eliminated the issue completly. I don't think the first line is still needed, but I have not tested that yet -- and since it currently works... ;).

Suggested in an answer in this question -
-keep class **.R$*
isn't the most elegant solution since it instructs ProGuard to preserve all of the R classes regardless of the package they are in.
Having the same problem with WebView, the error I see in my Logcat:
... E/AndroidProtocolHandler: Unable to open resource URL:file:///android_res/raw/$MISSING_RESOURCE_NAME.css
java.lang.ClassNotFoundException: Didn't find class
"my.app.package.R$raw" on path: DexPathList[[...
The instruction with maximum restrictions I added to my proguard-rules.pro file:
-keepnames class my.app.package.R$raw { public static <fields>; }
Obviously Since R class contains only fields and all of these fields have public static type, in practice, there should no be the difference between the one above and
-keepnames class my.app.package.R$raw { *; }
However, here I'm
NOT disabling shrinking and obfuscation for all of the rest inner classes in R other than raw.
targeting R in one specific package only.
That approach should be better in case if you have more than one module in your project that provides its own resources that may not be needed for the one particular APK you are building (having, let's say, more than one android_application modules – APK sources – in your project as well).
To understand the difference between -keepnames and -keep, please, refer to the following.
Distinguishing between the different ProGuard -keep directives - Tue May 29 04:10:50 MSK 2018

Create a file in raw folder
keep.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="#raw/*">
</resources>
That's it! It will keep all the files in raw folder, If you want to restrict it to only one particular file you can use
tools:keep="#raw/fileName"
Note that you should not add file extension in the end so it shouldn't be filename.html

Related

R8 and Proguard Rules

I am using https://github.com/amalChandran/trail-android this library in my android project. The animation was working perfectly fine. But, after enabling R8, the animation is not working. The library does not have any Proguard suggestion. I added the following block in one of my method,
googleMap.setOnMapLoadedCallback(() -> {
Route normalOverlayPolyline = new Route.Builder(mRouteOverlayView)
.setRouteType(RouteType.PATH)
.setCameraPosition(mMap.getCameraPosition())
.setProjection(mMap.getProjection())
.setLatLngs(mRoute)
.setBottomLayerColor(Color.YELLOW)
.setTopLayerColor(Color.RED)
.create();
map.setOnCameraMoveListener(() -> mRouteOverlayView.onCameraMove(map.getProjection(), map.getCameraPosition()));
Where I have two global variables defined,
public Route route;
public RouteOverlayView mRouteOverlayView;
Now, I have some details in my usage.txt
public void onCameramove(com.google.android.gms.maps.GoogleMap,com.obhai.polyline.trail.RouteOverlayView)
Is there any way to write something in proguard-rules.pro so that R8 doesn't remove this part?
Thanks to #sgjesse for his hint. I finally got my narrowed down proguard rule to keep my feature functioning.
-keep class com.amalbit.trail.AnimationRouteHelper { *;}
adding this rule served my purpose.

Error searching for native library (libjnidispatch.so) using JNA to access .so file

Please note that this question is a follow-up question to another question I asked here some time ago.
What would I like to achieve?
I would like to use JNA (version 4.5.2 with Android Studio) to achieve the following:
First I would like to get a list of all callable functions from a given .so file.
Once I have this list, I would like to call a specific function
What did I do so far?
So far, the code of the file "MainActivita.java" of my test app looks as follows:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String MyLibraryString = MyLibrary.Instance.toString();
}
public interface MyLibrary extends Library
{
MyLibrary Instance = (MyLibrary ) Native.loadLibrary("nameOfMyLibraryWithoutSoExtension", MyLibrary.class);
}
}
What issues am I facing?
I used to get an error message that my library is not found on my Android device but after putting it in the "jniLibs" subfolder my app seems to be able to detect it. Unfortunately, I now get the error message
java.lang.UnsatisfiedLinkError: Native library
(com/sun/jna/android-aarch64/libjnidispatch.so) not found in resource
path (.)
whenever I try to start the app on my device. I already tried to extract the file "libjnidispatch.so" from the JNA.jar file and put it in its respective subfolders (as described here) but that doesn't work.
The folder structure of the subfolder "app\src\main\jniLibs" of my app looks as follows:
app\src\main\jniLibs\arm64-v8a\myLibrary.so
...
app\src\main\jniLibs\arm64-v8a\libjnidispatch.so
app\src\main\jniLibs\armeabi\myLibrary.so
...
app\src\main\jniLibs\armeabi\libjnidispatch.so
app\src\main\jniLibs\armeabi-v7a\myLibrary.so
...
app\src\main\jniLibs\armeabi-v7a\libjnidispatch.so
app\src\main\jniLibs\mips\myLibrary.so
...
app\src\main\jniLibs\mips\libjnidispatch.so
app\src\main\jniLibs\mips64\myLibrary.so
...
app\src\main\jniLibs\mips64\libjnidispatch.so
app\src\main\jniLibs\x86\myLibrary.so
...
app\src\main\jniLibs\x86\libjnidispatch.so
app\src\main\jniLibs\x86_64\myLibrary.so
...
app\src\main\jniLibs\x86_64\libjnidispatch.so
Any help would be highly appreciated.

Error: Content not allowed in prolog

I know this question has been asked before but I can't find the solution in previous solutions. The error points to the file Game.java. The code for that file is very simple:
package framework;
public interface Game {
public Input getInput();
public FileIO getFileIO();
public Graphics getGraphics();
public Audio getAudio();
public void setScreen(Screen screen);
public Screen getCurrentScreen();
public Screen getStartScreen();
}
I have used the file several times before without problems. I have updated the manifest file. I have updated Android Studio and all the plug-ins (in fact I have re-installed them). The project was created using Android Studio instead of being imported. I have tried cleaning the project. I just can't see what the problem is. Can you please help?
The location of the problem is
G:\AndroidStudioProjects\Marvelous2\app\src\main\res\framework\Game.java
You shouldn't place Java files under the res folder, those belong under src/main/java. Correct directory for Game.java is src/main/java/framework/Game.java.

chromecast button not showing up after proguard configuration

After configuring progaurd, chromecast button doesn't show up and instead text like "PLAY ON.." appears in the Action bar.
I have tried adding below line in config file but no luck
-keep class com.google.sample.castcompanionlibrary.** {*;}
please let me know if i am missing anything.
Thanks.

Resources$NotFoundException when trying to load animation

Our app on Google Play market has a weird exception being thrown on some devices. I see the following stack trace:
android.content.res.Resources$NotFoundException: File res/anim/ani_in_fade.xml from xml type anim resource ID #0x7f040000
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2185)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2140)
at android.content.res.Resources.getAnimation(Resources.java:894)
at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:71)
....
Basically, all the animations are in place in the res/anim folder. Most of the devices load and display them without a problem.
Any ideas what may be the reason for this? One of the ideas I have is that activity is being (or has been) destroyed at the point we try to load the resource, however the context is not null at that point of time...
Thanks,
Just in case anyone hits this problem... You need to update your proguard.cfg file and add the following lines:
-keepclassmembers class **.R$* {
public static <fields>;
}

Categories

Resources