I am quite a .NET guy, developing in Xamarin. However, I got my hands on a piece of Java code for Android I would like to implement in C#. The problem is that the project was developed in Eclipse, and then ported to Android Studio and now cannot be compiled.
I got through all the issues with Gradle, but now I am stuck with some generated annotations and .put() and .get() methods, that cannot be referenced. There are many things similar to the following:
A normal interface like MySharedPreferences has its generated sealed (final) class counterpart MySharedPreferences_. This is then used in the code:
import com.someproject.MySharedPreferences_;
...
public class SomeAndroidClass {
public MySharedPreferences_ prefs;
public SomeMethod() {
String x = prefs.someValue().get;
...
prefs.someValue().put("abc");
}
}
Now, this cannot be compiled, because the MySharedPreferences_class is not generated by Android studio. I tried to get rid of the underscore and use the MySharedPreferences interface instead. But then I had problem referencing the .get() and .put() methods. Please, can somebody help me how to deal with this problem?
EDIT
Adding the build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
repositories {
maven { url "http://dl.bintray.com/populov/maven" }
mavenCentral()
mavenLocal()
}
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '4.0.0'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.testproject.smartconfig"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'fr.avianey.com.viewpagerindicator:library:2.4.1.1#aar'
}
apt {
arguments {
androidManifestFile variant.outputs[0]?.processResources?.manifestFile
}
}
MySharedPreferences_ is most likely class generated by Android Annotations library - androidannotations.org, check if Your build.gradle contains needed config: https://github.com/excilys/androidannotations/wiki/Building-Project-Gradle
Then reimport Your project using gradle.
Related
I am trying to import Dagger 2 into a brand new project on Android Studio and having a look at the various guides and documentation, I am unable to use DaggerAppComponent
My Gradle settings are as follows:
build (Project)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
build (Module: app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.raywenderlich.todolist"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
//Dagger 2
compile 'com.google.dagger:dagger:2.11'
compile 'com.google.dagger:dagger-android:2.11'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
}
The in my TodolistApplication.java class: I have this:
#Override
public void onCreate() {
super.onCreate();
DaggerAppComponent
.builder()
.application(this)
.build()
.inject(this);
}
However Android Studio shows the following error:
Error:(21, 5) error: cannot find symbol variable DaggerAppComponent
I have tried rebuilding the project and importing various dagger files, however nothing seems to have worked.
Error:(21, 5) error: cannot find symbol variable DaggerAppComponent
The above error is not a dependency error.
AppComponent must be first created before using it like below
#Module public class ApplicationModule {
//all dependency provides here
}
#Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
}
Check this reference for complete understanding https://medium.com/#isoron/a-friendly-introduction-to-dagger-2-part-1-dbdf2f3fb17b
I could not able to run the local unit tests in Module which is using android Data binding library.
First let me tell about the project structure how its configured.
project
| app
-MainLauncherActivity
| myLibrary
-CommonModuleActivity
I have created a new project, after that have added a new module "myLibrary".
the main "app" depends on "myLibrary" module. I have added one activity in "myLibrary" it supports databinding. I called module specific activity from main "app" activity on button click in it. it just works, could able to run the app.
But, Getting the below errors when i add a test case for the Module Activity.
AndroidStudio : 2.3
Gradle build tools version 2.3.0 -->
Error:java.lang.NoClassDefFoundError: android/databinding/DataBinderMapper
Gradle build tools version 2.2.3 -->
Error:java.lang.NoClassDefFoundError: android/databinding/ViewDataBinding
PROJECT IDE SCREENSHOT
project root gradle file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
//classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
below is "app" build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.bindingtest"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled true
}
}
dependencies {
compile 'com.android.support:appcompat-v7:25.1.0'
//compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
testCompile 'junit:junit:4.12'
compile project(':mylibrary')
}
below myLibrary build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.10.19"
}
LibraryActivity:
public class MyLibraryActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMyLibraryBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_my_library);
//set data to binding
}
}
Corresponding test case can be found in the attached screenshot.
Could some one tell me what am i doing wrong here to get it tested.
APP IS WORKING FINE, ONLY UNIT-TESTS ARE FAILING!!!
I know it's too late to answer that question , But I say to friends who may face this problem.
Any time You used to dataBinding and at unit test faced with NoClassDefFoundError
first add this lines in build.gradle(Module)
testOptions {
unitTests{
isIncludeAndroidResources=true
}
}
then add this library to the dependencies at build.gradle(Module)
kaptTest("androidx.databinding:databinding-compiler:7.0.4")
I think this is a known issue which you can review here. I've been tracking this issue since February. Doesn't seem to be fixed yet.
added android.enableExperimentalFeatureDatabinding=true in gradle.properties . According to the name of this property, I think it could work for sometimes but not always, you can try.
Dagger2 is not generating any component classes in android studio i know its a known problem while i have gone through almost all ways to implement in my android studio and have tried on various tutorials but every time i got struck here, it fails to build the daggercomponent class . I have also tried to rebuild ,clean gradles and invalidate caches but it does not help .
Here is my one of sample project build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.example.g.daggerillkillu"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories{
maven{url "https://jetpack.io"}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile 'com.google.dagger:dagger:2.0.2'
apt 'com.google.dagger:dagger-compiler:2.0.2'
provided 'javax.annotation:jsr250-api:1.0'
}
vehiclemodule.java
#Module
public class vehiclemodule {
#Provides
#Singleton
Motor providesMotor(){
return new Motor();
}
#Provides
#Singleton
Vehicle provideVehicle(){
return new Vehicle(new Motor());
}
}
vehicleComponent.java
#Singleton
#Component(modules = {vehiclemodule.class})
public interface VehicleComponent {
Vehicle provideVehicle();
}
Is there any problem in the android studio or i am doing something wrong?
Is there any problem in the android studio or i am doing something wrong?
If nothing is being generated then you most likely do not have annotation processing enabled:
You also need to have modules, and build them.
Please check this full tutorial
https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2
I'm new to Android and I can't get my first app to run. Whenever I try, it's throwing this error:
Android gradle :app:packageDebug FAILED: class org.bouncycastle.asn1.ASN1Primitive overrides final method equals.
I googled it and I found many questions there. As many said, this problem is probably due to the fact that I have multiple dependencies on BouncyCastle in my project. I tryed to remove the .gradle folder both in my project and in my local folder so I could download again the jars from the repository but nothing has changed. I also double checked that my JAVA_HOME is set and it's pointing to a 1.7 version. I tryed to gradle :app:clean and gradle :app:dependencies to see if I was missing something but still... I can't see any dependency from BouncyCastle... Can anybody help me?
Also, this problem occured without me changing any code... The app successfully worked yesterday and now is giving me this error... I don't get it. Thank you.
EDIT
gradle.build
Top-level:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Module app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.aurasphere.provaactionbar"
minSdkVersion 11
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:7.0.0'
compile project(':volley')
compile files('libs/gson-2.2.2.jar')
}
Module volley:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.+'
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 19
buildToolsVersion = '21.1.0'
}
apply from: 'rules.gradle'
rules.gradle for module volley:
apply plugin: 'com.android.library'
I've solved this problem myself just by changing the JDK I was using from x32 to x64 and now is compiling correctly.
I'm trying to use the new Activity transitions in the new SDK.
I tried this line:
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
But the problem is that Window doesn't include FEATURE_CONTENT_TRANSITIONS.
I also tried this line:
getWindow().setExitTransition(new Explode());
And Explode class doesn't exist...
I already set my project to be compiled with L SDK (android-L) and use the new SDK tools (20.0.0)
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 'android-L'
buildToolsVersion '20.0.0'
defaultConfig {
applicationId 'com.tester'
minSdkVersion 'L'
targetSdkVersion 'L'
versionCode 1
versionName '1.0'
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.android.support:support-v4:+"
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
seems like you are not including the L SDK library in your build path.
Are the libraries listed as dependency of your project?
Try File -> Invalidate Caches / Restart.
If that doesn't work, you can try deleting ~/.AndroidStudioBeta (or ~/.AndroidStudioPreview, whichever you are using). It will clear all your settings so be prepared for that, but this solved the issue in my case.
For Mac, clear:
~/Library/Application Support/AndroidStudioBeta
~/Library/Caches/AndroidStudioBeta
~/Library/Logs/AndroidStudioBeta
~/Library/Preferences/AndroidStudioBeta