I am trying to create an Android Library containing a simple service. For example:
public class BasicService extends Service {
public BasicService() {
Log.d("I", "work");
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
I start the service the following way:
startService(new Intent(this, BasicService.class));
The service is compiled into an AAR file and after adding that to an application I get a ClassNotFoundException.
Caused by: java.lang.ClassNotFoundException: Didn't find class "nl.company.example.library.Services.BasicService" on path: DexPathList[[zip file "/data/app/nl.example.aartest-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
I don't need to bind a service as the application does not have to communicate with the service. I also don't need an aidl interface as I don't want to share the service with other apps. I know I need to declare the service at the application side (not library) so I did that the following way:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.company.example.exampleApp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="19" />
<application
android:settings... >
<service
android:name="nl.company.example.library.Services.BasicService"
android:enabled="true"
android:exported="true" />
</application>
</manifest>
This is the manifest of my library:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.company.example.library" >
<application/>
</manifest>
I've done some research at Stackoverflow/Google and I wasn't able to find an answer to my problem.
Android service in library
Also tried different ways of starting the service (on action etc.) but no good so far.
All help is welcome and my apologies if I made an evil duplicate question.
The BaseService was implementing an interface from another library which was not packaged with the aar file. This caused the application to crash right after starting on a device, because it couldn't resolve the dependency.
public class BaseService implements OtherLibraryInterface {
...
}
Instead of throwing a ClassNotFoundException for the OtherLibraryInterface, Android would throw exactly that exception for the BaseService. Which is very confusing and lead us to believe that there is something wrong with the Service.
Adding the OtherLibrary dependency explicitly as a dependency to the main application solved this issue.
Related
I am in a world of trouble. In a position of inheriting an Android App in Java with a team of C# .Net developers.
I am getting an (Access Denied) error to the projects root app folder C:\Users\user.name\Development\theproject\app
The project builds, cleans and rebuilds no problem but on debug or run, it fails at this step app:transformDexWithInstantRunDependenciesApkForDebug.
In the research so far I am getting lost in setting application permissions in the manifest.xml file but this feels more like a local machine folder permission issue more than an application issue.
I have tried running Android Studio in Adminstrator mode and both it and the emulator appear to be running under my username. I have administration priviliges to my machine.
Any help greatly appreciated. I had never heard of gradle until yesterday.
This is done as you have used multilibrary and the methods exceed the limits.
Modify your build.gradle:
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
include this in application class :
public class YouApplication extends Application {
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
Add this class name to the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.myapplication">
<application
...
android:name="android.support.multidex.MultiDexApplication">
...
</application>
</manifest>
I want to use an external project that is implemented as Android service. The service is used by adding it to the manifest:
<service android:name="com.my.ext.service"/>
But when I addthis line, I get the error:
Unresolved class 'TimerService' less... (⌘F1)
Validates resource references inside Android XML files.
And when I add the layout in my project:
<com.my.ext.service
android:id="#+id/myview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
I get the error that
the class could not be found
I have imported the external service as an extra project into my Android Studio. How can I use the other project as a service for this project?
You can not use Your service class as XML layout.
You can use that service using
<service android:name="com.my.ext.service"/>
and execute that service from Main activity of your app using:
private Intent OtherService;
OtherService = new Intent((Context)this, (Class)YOURSERVICE.class);
MainActivity.this.startService(MainActivity.this.OtherService);
I have added a module within my project and created an Application class which was added to the module's manifest.
public class App extends Application {
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
#Override
public void onCreate() {
super.onCreate();
Fabric.with(this, new Crashlytics());;
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mymodule">
<application
android:allowBackup="true"
android:label="#string/app_name"
android:name="com.example.mymodule.App">
</application>
</manifest>
The problem is that I get no suggestions when trying to set the name of my application class within the Manifest. Any ideas why?
EDIT: I already have an Application class in my app module. The reason why I'm doing this is because I want to add a library in that module. How can I achieve this?
You need to type . (dot) and then you will get the suggestion. Like below
android:name=".App"
If you application class is within any package then the name will be
.[package name].[Application class]. If it is outside the any package the just the Application name.If still the problem exist then restart the studio sync,rebuild and clean it and check it once again.Hope this will help you.
Hi I faced that when I rewrite application in androidTest manifest file, it does not work. This is my AndroidManifest.xml file in androidTest folder:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="ru.app"
android:installLocation="auto">
<application
tools:replace="android:name"
android:name=".app.ApplicationAndroidTest" />
</manifest>
This is part of original AndroidManifest.xml from main folder:
<application
android:name=".app.Application"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:largeHeap="true"
android:theme="#style/Theme">
...
</application>
In fact I debugged it, and called getApplication() from breakpoint in activity under test, it returns .app.Application instead of ApplicationAndroidTest instance.
Do you have any ideas why android manifest file from androidTest is ignored?
As A workaround I used custom test runner class:
public class UiTestsRunner extends AndroidJUnitRunner {
#Override
#NonNull
public Application newApplication(#NonNull ClassLoader cl, #NonNull String className, #NonNull Context context)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return Instrumentation.newApplication(ApplicationAndroidTest.class, context);
}
}
It seams ok for me. Hope it helps someone.
Changing application class at androidTest xml doesn't affect the app itself (tested app) but the additional android test apk that ships to the device for enabling tests. I'm not sure what is the exact meaning of application if any to android test apk.
anyhow busylee workaround is the available solution.
Important note: when defining custom instrumentation runner, it is required to run the tests with the custom runner, that can be done at Android Studio by editing run configuration of test run, under AndroidTests section with a test selected, there is 'Specific instrumentation runner (optional)' option.
I am building a HelloWorld-project called 'HelloGlass' using the GDK.
I have a HelloGlassActivity class, which works perfectly fine when launched and compiled. I then try adding another class, HelloGlassService, which extends android.app.Service and implements one abstract function. The moment I add this class, the project suddenly stops compiling with and throws the following error:
Android Dex: [HelloGlass] Unable to execute DX Android Dex:
[HelloGlass] java.nio.BufferOverflowException Android Dex:
[HelloGlass] at java.nio.Buffer.nextPutIndex(Buffer.java:499)
Now, Google and StackOverflow tell me that this error can be resolved by making sure that the AndroidManifest SDK version matches the one my project is dependent on. However, all my dependencies are JDK 1.6 and the GDK Sneak Peek.
As in Google's own Stopwatch example, my project does not include a project.properties file (I removed it after creating the default Android app template provided by IntelliJ and setting it to the GDK Sneak Peek SDK); and that's what my AndroidManifest.xml looks like:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arik.HelloGlass"
android:versionCode="2"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:label="Hello Glass App" >
<activity
android:name="com.arik.HelloGlass.HelloGlassActivity"
android:label="Hello Glass Activity"
android:enabled="true" >
</activity>
</application>
</manifest>
As you can see, I did not even include that Service Class in the manifest, and yet it crashes. The Service class looks like this:
package com.arik.HelloGlass;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class HelloService extends Service{
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
And here are my module dependencies:
What am I doing wrong?