Problems about ProGuard when build up APK - java

When I build up the APK, I find out the KEYs of JSON changed when I use the Network. I just fund out that it is a problem about ProGuard.
I have added "#SerializedName" to every variables in Request/Response Folder. And added
-keepclassmembernames class com.mygroup.myapp.protocol.response.Responsexx {
public *;}
for every Resquest/Response JAVA program, which took me much time but doesn't work.
My app crashed after I get the response, the logcat says com.b.a.b.w cannot be cast to com.mygroup.myapp.b.g , and I have located the error:
private List<UserInfoModel> mUserList = new ArrayList<>();
private UserInfoModel UserInfo = new UserInfoModel();
if (mUserList.size()>0)
UserInfo = mUserList.get(0); //CRASHED HERE
I am sure that the size of mUserList is 1, I made a toast and saw it.
Please Help Me. Much Thanks.
Moreover, the IDE I am using is Android Studio.

Related

Errors when trying to create Xamarin Android binding Java Library for mapsforge/vtm

I have tried to use the Java map library mapsforge-vtm to show an offline map using a mapsforge file with Xamarin Android.
Within Visual Studio 2019, I created one aar binding project as "LibraryProjectZip" for the file "vtm-android-0.13.0.aar" and one jar binding project as "EmbeddedJar" for the file "vtm-android-0.13.0.jar".
There is also a dependency to "vtm-0.13.0.jar" which you can see in the file "vtm-android-0.13.0.pom".
That "vtm-0.13.0.jar" file seems to cause the problems (e.g. the java interface Gesture which I include further down below).
If you keep following the dependencies (by looking in the pom files), there are also dependencies to "slf4j-api-1.7.28.jar" and "androidsvg-1.4.jar" but those two jars do not seem to cause problems, as far as I can tell.
Regarding the problem file "vtm-0.13.0.jar" I have tried to use it as an "EmbeddedReferenceJar" within both of the above mentioned projects (i.e. the aar binding and the jar binding projects for vtm-android).
Both of those two binding projects can build without errors (when also using "androidsvg-1.4.jar" and "slf4j-api-1.7.28.jar" as "EmbeddedReferenceJar"'s)
However, when trying to reference those projects from a Xamarin Android app, it does not work.
When trying to do that, I can not reference both the aar binding and the jar binding projects because then there is an error message about duplicated versions of for example the class "Org.Oscim.Android.MapView".
But when I use only one of them (i.e. either aar or jar project), then only the types from the main libraries are available (i.e. the "LibraryProjectZip" file for "vtm-android-0.13.0.aar" and "EmbeddedJar" file for "vtm-android-0.13.0.jar").
But I seem to NOT be able to reference any types from the "EmbeddedReferenceJar".
For example, there is a java type "org.oscim.tiling.source.mapfile.MapFileTileSource" in the "vtm-0.13.0.jar" but it does not seem to become available i.e. I can NOT use:
using Org.Oscim.Tiling.Source.Mapfile.MapFileTileSource;
In fact, the only namespace I can use, starting with "Org.Oscim" is "Org.Oscim.Android" e.g. this works:
using Org.Oscim.Android;
Then I tried to create a separate "EmbeddedJar" from the file "vtm-0.13.0.jar", since I was hoping that it then could be referenced from Visual Studio.
(and in that project I added "slf4j-api-1.7.28.jar" as "EmbeddedReferenceJar")
However, the compilation failed with 85 errors.
And I have tried all four combinations of settings for "Android Class Parser" and "Android Codegen Target"
(i.e. the four combinations of "class-parse + XAJavaInterop1" and "jar2xml + XAJavaInterop1" and "class-parse + XamarinAndroid" and "jar2xml + XamarinAndroid")
I have also tried with a couple of different target frameworks (Android 4.4 and Android 9 and Android 10).
50 of those 85 errors are this same error:
Error CS0234 The type or namespace name 'IGesture' does not exist in the namespace 'Org.Oscim.Event' (are you missing an assembly reference?)
vtm-jar ...\obj\Debug\generated\src\Org.Oscim.Event.IGesture.cs
Indeed, when looking in that file "IGesture.cs" there is no interface defined there.
I had expected to find something like "public partial interface IGesture ..." in that file, in a similar way as there is a "public partial interface IGestureListener ..." in the file "IGestureListener.cs" in the same directory.
I suspect that it has something to do with the fact that the java interface "Gesture" seems to be a bit unusal and thus not being nicely supported by Xamarin/Visual Studio.
This is what I am talking about:
Here is a "normal" kind of Java interface (GestureListener) which seems to work without errors in Visual Studio:
https://github.com/mapsforge/vtm/blob/master/vtm/src/org/oscim/event/GestureListener.java
package org.oscim.event;
public interface GestureListener {
boolean onGesture(Gesture g, MotionEvent e);
}
Here is the source code for the problematic interface (Gesture) in "vtm-0.13.0.jar":
https://github.com/mapsforge/vtm/blob/master/vtm/src/org/oscim/event/Gesture.java
package org.oscim.event;
public interface Gesture {
final class Press implements Gesture {
}
final class LongPress implements Gesture {
}
final class Tap implements Gesture {
}
final class DoubleTap implements Gesture {
}
final class TripleTap implements Gesture {
}
final class TwoFingerTap implements Gesture {
}
Gesture PRESS = new Press();
Gesture LONG_PRESS = new LongPress();
Gesture TAP = new Tap();
Gesture DOUBLE_TAP = new DoubleTap();
Gesture TRIPLE_TAP = new TripleTap();
Gesture TWO_FINGER_TAP = new TwoFingerTap();
}
As far as I can remember, I do not think I have ever seen that kind of Java code with classes being defined and instantiated within an interface.
And I suppose that Xamarin/Visual Studio also have a hard time to understand that kind of Java code.
So, is there anything you can do about this (and all other build errors, totally 85, with this vtm library)?
Has anyone been able to implement an Android App with Xamarin Android that uses mapsforge-vtm bindings, including also having been able to put all the pieces together and succeeded to display a map from a mapsforge map file?

java.lang.Exception when implementing method in android activity

This might be a very naive question but I can't get this java.lang.Exception error to go away. I also just started learning java and android so... this might be an easy problem to solve.
So I have a main activity in android and I want to implement a weka ml classifier within the app. Right now I'm just trying to load some data.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ml_classifiers ml_object = new ml_classifiers();
int number = ml_object.loadData();
Trying to load data...
public class ml_classifiers {
public int loadData() {
ConverterUtils.DataSource source = new ConverterUtils.DataSource("C:/Users/Seth/Desktop/iris.arff");
Instances data = source.getDataSet();
int num = data.numInstances();
return num;
}
}
Why does java.lang.exception occur?
Why does java.lang.exception occur?
In the future, when you encounter crashes, use LogCat to examine the Java stack trace associated with the crash. If you do not understand the stack trace or otherwise cannot identify the problem, and you want help here, post the stack trace along with the code. As it stands, we have to guess exactly what is going wrong in your app.
In this case, while you may be crashing elsewhere, you will definitely crash with:
ConverterUtils.DataSource source = new ConverterUtils.DataSource("C:/Users/Seth/Desktop/iris.arff");
assuming that this is code in your Android app.
You are attempting to read data from C:/Users/Seth/Desktop/iris.arff. That is a path to a file on a Windows machine. Android is not Windows. There are ~2 billion Android devices in use, and none of them have access to files on your Windows' machine's desktop.
You need to get this data onto the Android device, such as:
by putting it in the assets/ directory in your module (to package it with your app), then using AssetManager to get an InputStream on that asset, hopefully passing that directly to ConverterUtils.DataSource
downloading the file from the Internet, perhaps into internal storage (e.g., getCacheDir())
expecting the user to copy the file onto their device by hand, such as via external storage

Android Studio 2.2.0.12 designer automatically ads "#+id" prefix before view id and it causes this unusual error

When I type in this id for a GroupBox (the view is irrelevant, as this happens with all Views)
This is what I wrote in the id attribute :
Then after I press ENTER, Android Studio adds a "#+id prefix" :
This, after I compile, gives the following error in R.java:
error: type annotations are not supported in -source 1.7 (use -source
8 or higher to enable type annotations)
It comes from the line that has "#+id" as a prefix.
public static final class id {
public static final int #+id/groupBox=0x7f0c0055; // Error comes from this line
public static final int action0=0x7f0c006e; // ok
public static final int action_bar=0x7f0c0041; // ok
public static final int action_bar_activity_content=0x7f0c0000; // ok
public static final int action_bar_container=0x7f0c0040; // ok
// rest of lines ok
// ...
The project had been started in a previous version of Android Studio, hence the rest of the view names remain unaffected.
I tried manually erasing the prefix but it didn't work, I tried looking in Settings without luck, and tried searching online for a solution until I came to realize this is a new problem and the only hope is Stack Overflow.
========================================================================
I solved the problem this way : The update to Android Studio v2.2.0.12 happened right before the problem with the “#+id” prefix appeared. I simply uninstalled Android Studio from my computer, restarted my computer (important!) then reinstalled Android Studio. I chose the no import from previous version whatsoever option.
Notice: I still had the old Android Studio installer left, but allowed it to update to the latest version. After the update was done, the problem disappeared.
I am not 100% certain the solution will work for everybody, but I had quite a bit of experience with installing/uninstalling software on Windows (am using 8.1), and the software behaving abnormally if used before a restart. I hope this helps whomever is stuck with the same issue.

"android.R.id.content" generates an error with no change to the source code

This code is from an exercise source code from a course I took. It worked fine up until this afternoon. No change was made to it and I re imported it and a similar project and experienced the same new error.
public class TourListAdapter extends ArrayAdapter<Tour> {
Context context;
List<Tour> tours;
public TourListAdapter(Context context, List<Tour> tours) {
super(context, android.R.id.content, tours);
this.context = context;
this.tours = tours;
}
I'm using Android Studio 1.3
android.R.id.content used to work and now generates the error:
Expected resource of type layout.
I've searched stackoverflow and there appears to be a new feature that annotates possible mis-matches. Somehow this android id integer used to be ok and is not any longer. I did not download anything new today to Android Studio.
I have re-built, cleaned and sycn'd. I have exited and rebooted the computer with no resolution.
It's not an error, it's simply a warning.
As you've noted in your question, resource method parameters can now be annotated to indicate what type of resource should be passed to it. Android Studio checks these parameters based upon the name and flags it if it doesn't match the correct type.
However, you've stumbled upon a corner-case that Android Studio does not deal with. As noted by this question, android.R.id.content is a special resource ID used to obtain a root view without knowing the actual ID of the view. Sadly, the Android SDK developers put it as an id resource and not as a layout resource (it should probably have been designated android.R.layout.content, but it's too late to change it now!).
So, to sum up, Android Studio is expecting you to use a R.layout.value here, but you are using a special R.id.value. But you are correct and your App should still build correctly (mine does when I tested it) - hence it is a warning, not an error.

How to make Android app automatically configure w/ debug vs. release values?

I'm working on an Android app, specifically one that uses the Facebook Android SDK. In development mode, I'm working with a test Facebook app that goes by one ID. However, in release mode, the app will be working with second Facebook app with a different ID.
I'm wondering how most Android (or Java might be a suitable enough realm of knowledge) developers here go about having their app automatically build with debug vs. release values. An ideal solution does not involve a manual switch (e.g.: switching public static final DEBUG = false; to true) before building.
It's been a while since you asked but I thought I'd share how I'm doing it.
Like Sebastian hinted, an Ant script can handle that change for you and generate the static final constants that you're looking for. You can configure IntelliJ or Eclipse to make it almost seamless.
I tried to detail the different steps I took over here, let me know if it helps. I know I never have to make any manual changes before releasing, and it's a nice relief!
In eclipse ADT 17.0 and above there is a new feature for this. Check the BuildConfig.DEBUG that is automatically built with your code.
For more information see http://developer.android.com/sdk/eclipse-adt.html
I can't recommend the IMEI method... the main problem with it is that not all Android devices will have IMEIs. A better way is to examine the signature used to sign the .apk.
// See if we're a debug or a release build
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
if (packageInfo.signatures.length>0) {
String signature = new String(packageInfo.signatures[0].toByteArray());
isReleaseBuild = !signature.contains("Android Debug");
}
} catch (NameNotFoundException e1) {
e1.printStackTrace();
}
I use a slightly more mundane method (in case you're still interested in solutions).
At application launch my application checks for the existence of a text file stored in /sdcard/. Each application I have looks for a specific file like "applicationdebug.txt". If the file exists, then the application goes into debug mode and starts being verbose with log statements and using my debug Facebook key, etc.
Then I simply remove (or rename) the file to on the device to see how the application performs in release mode.
Usually you will use 1 or 2 devices for debugging only. So you can set the DEBUG switch based on the Devices? So you can simply use the IMEI.
add a new Application class to your project and have it initialize the field (suspect to put it in a Const class).
In your Applications onCreate method, call Const.setupDebug(getApplicationContext());
Implement the setupDebug like this
public class Const {
private static boolean debug = false;
public static boolean isDebug() {
return debug;
}
private static void setDebug(boolean debug) {
Const.debug = debug;
}
private static String [] DEBUG_DEVICES = new String[] {
"000000000000000", "gfjdhsgfhjsdg" // add ur devices
};
public static void setupDebug(Context context) {
Arrays.sort(DEBUG_DEVICES);
TelephonyManager mTelephonyMgr = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
String imei = mTelephonyMgr.getDeviceId();
if (imei == null) imei = "000000000000000";
if(Arrays.binarySearch(DEBUG_DEVICES, imei) > -1) {
setDebug(true);
}
}
}
Switch from constant field to constant Method.
Const.isDebug()
With Eclipse I create 3 projects in the workspace :
ApplicationProject
It is a library project
Contain all source code
In values/refs.xml I add
<bool name="debug_mode">true</bool>
ApplicationProjectDEBUG
Use ApplicationProject
Overide AndroidManifest and other xml file with
developement specific config
In values/refs.xml I add
<bool name="debug_mode">true</bool>
ApplicationProjectPROD
Use ApplicationProject
Overide AndroidManifest and other xml file with
production specific config
In values/refs.xml I add
<bool name="debug_mode">false</bool>
I signe APK from this project to put on the store

Categories

Resources