Android gradle plugin 2.2.0 causes FileNotFoundException while reading from assets - android

After upgrading to Android gradle plugin 2.2.0 (and Android Studio 2.2) I've been having trouble reading from a zip file which is located at app/src/main/assets/magic_info.zip It used to work fine with plugin versions 2.1.3 and prior but now it throws the following exception
java.io.FileNotFoundException: magic_info.zip
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:334)
at android.content.res.AssetManager.open(AssetManager.java:308)
for some reason it can't find the file from assets which is otherwise there.
Here is my code that used to work in the past.
public static ZipInputStream getZipInputStream(Context context)
{
try
{
return new ZipInputStream(context.getAssets().open("magic_info.zip"));
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
Any idea what's wrong with this? Am I missing something?

Replace this:
return new ZipInputStream(context.getAssets().open("magic_info.zip"));
with this:
return new ZipInputStream(getResources().getAssets().open("magic_info.zip"));

OK I've figured it out and it's kind of strange.
As of Android Studio 2.2 and gradle plugin 2.2 you are required to have the android SDK which corresponds to your testing device along with the target SDK installed on your machine if you want things to work properly.
Now I may target API 24 but I test my app to a device running lollipop (API 21). Once I installed SDK 21 the file is being read normally.

Related

App crashes on Launch when made as system application

I developed an application that uses DJI SDK. When installed as an system application it crashes on Launch because its not able to load the libraries. It works fine when installed as normal application.
Steps followed to make system application
1)Rooted the device
2)Copied the .apk file to system/app directory
3)Rebooted the device
4)App was installed as default application but crashed on Launch
After commenting out the following line related to DJI SDK and followed the same steps as above. It worked fine as system app.
Helper.install(context);
The implementation of Helper class which is in DJI SDK is as below:
public static void install(Application app) {
Object var1 = null;
try {
System.loadLibrary("DexHelper_sdk");
if (PPATH != null) {
System.load(PPATH);
}
} catch (Error var3) {
;
}
String app_dataDir = app.getApplicationInfo().dataDir;
DexInstall.install(app, new File(app_dataDir + "/.cache_sdk/sdkclasses.jar"));
}
As far as my understanding the crash is because the system is failing to load the libraries.
My gradle looks similar as in the following link:
https://github.com/dji-sdk/Mobile-SDK-Android/blob/master/Sample%20Code/app/build.gradle
this issue has been fixed since 4.6 version of Android MSDK, pls try to update to the later version, right now the 4.7.1 has been released.

Android things: DP 7 retired PeripheralManagerService and I have issue with PeripheralManager

I have 1 project based on this: https://github.com/Nilhcem/i2cfun-androidthings
https://github.com/neuberfran/SmartDrive5 (the latter is my project)
But, when I change PeripheralManagerService to PeripheralManager (DP 7 and APi level 27)
try {
PeripheralManager manager = PeripheralManager.getInstance();
mDevice = manager.openI2cDevice(I2C_DEVICE_NAME, I2C_ADDRESS);
} catch (IOException e) {
Log.w(TAG, "Unable to access I2C device", e);
}
I have this issue: https://drive.google.com/file/d/1kXfknYcu4RUF7AT1549_sjJiVSG2_Jjo/view?usp=sharing in this line: https://drive.google.com/file/d/1nTXOV0qiQDe5XldzIuLsY26oIdyfs3x3/view?usp=sharing
I know about this: PeripheralManagerService throws NoClassDefFoundError
but not solved.
Can you help me
The class not found exception means that peripheral manager is not on the operating system. You are trying to use a new API on a device that has an older version.
You need to have the Android things library dependency in sync with the version installed on your device.
And as it has been suggested, you should get everything into the stable release.

Error: Could not find an installed version of Gradle either in Android Studio, or on your system to install the gradle wrapper

I develop a hybrid app usin cordova. While I was trying to build the android verion of my App, i got this error:
Error: Could not find an installed version of Gradle either in Android Studio,
or on your system to install the gradle wrapper. Please include gradle
in your path, or install Android Studio.
..although Android Studio is already installed and everything worked so far.
Does anybody know the reason for this issue?
The problem is in the file: \cordova\lib\check_reqs.js
When cordova checks the location of graddle is doing this: var androidPath = path.join(process.env['ProgramFiles'], 'Android') + '/';
If you don't have installed in certain location, cordova can't find the file.
So... edit tcheck_reqs.js and do this (i'm using Windows 10 and cordova 7.0.1):
return 'C:\\Android\\sdk\\gradle\\gradle-3.2\\bin\\gradle';
if (androidStudioPath !== null && fs.existsSync(androidStudioPath)) {
var dirs = fs.readdirSync(androidStudioPath);
if(dirs[0].split('-')[0] == 'gradle') {
return path.join(androidStudioPath, dirs[0], 'bin', 'gradle');
}
} else {
//OK, let's try to check for Gradle!
return forgivingWhichSync('gradle');
}
It's not a real fix, but if you are sure of your gradle location it will be fine, hope cordova fix this.
http://www.bubuko.com/infodetail-2108699.html,
its about your cordova version;

Getting the Android SDK directory within a gradle task

Recently the gradle plugin for android got updated (with android studio), after which the previous way of getting to the SDK directory ceased to work. The expression
${android.plugin.sdkDirectory}
which worked in an older version now returns the error
Error:(42, 0) No such property: sdkDirectory for class: com.android.build.gradle.LibraryPlugin
What would be the proper way of getting the android SDK directory being used, preferably independent of the user's configuration such as plugin and gradle version? The script needs to be shareable with several users.
Since all the previous answers depend on the environment or specific user intervention on top of normal configuration, I'll just post my technically messy fix.
if (android.hasProperty('plugin')) {
if (android.plugin.hasProperty('sdkHandler')) {
androidPath = android.plugin.sdkHandler.sdkFolder
} else {
androidPath = android.plugin.sdkDirectory
}
} else {
androidPath = android.sdkDirectory
}
Unlike all previous methods, this actually works, but it still looks hacky.
In gradle.properties set location sdkdir=/home/user/android-sdk and then in gradle you can use $sdkdir
I'm using Android gradle plugin v1.2.3 and this works fine:
${android.sdkDirectory}
You can use
$System.env.ANDROID_HOME
export ANDROID_HOME=/xxx/xxx/ in shell, then use it by System.env.ANDROID_HOME in gradle file.

Open an xls file with HSSFWorkbook crash my app in run time

When I do this:
try {
workbook = new HSSFWorkbook(new FileInputStream(mInputFile))
} catch (Exception e) {
...
}
My android project work in debug time but crash in run time.
I Use apache POI 3.9
I suspect it could because of the missing java.rmi.UnexpectedException
Thanks Gagravarr..
As metioned on this http://poi.apache.org/changes.html link, "Avoid using RMI based exception from PropertySetFactory, as it's not needed nor helpful" is been fixed in Version 3.10-FINAL (2014-02-08).
Use, 3.10 Final or upper version to get rid of 'java.rmi.UnexpectedException' error.
It looks like a mistake in PropertySetFactory - there's no reason for it to be using a RMI exception, as there's no RMI involved. My hunch is that it's the RMI class that's confusing Android.
I've raised this as bug #55901, and it's fixed as of r1551832. If you grab a nightly build from tomorrow, or do a SVN checkout and build yourself, it ought to now be fixed.

Categories

Resources