# Building Android APK on intellij



## marcelbonnet (Mar 29, 2019)

I've installed java/intellij and the following Android SDK:


```
$  find Android-SDK-Linux/ -type d -depth 2
Android-SDK-Linux/tools/proguard
Android-SDK-Linux/tools/lib
Android-SDK-Linux/tools/bin
Android-SDK-Linux/tools/support
Android-SDK-Linux/platforms/android-26
Android-SDK-Linux/platforms/android-28
Android-SDK-Linux/build-tools/android-9
Android-SDK-Linux/extras/android
Android-SDK-Linux/extras/google
Android-SDK-Linux/extras/m2repository
Android-SDK-Linux/sources/android-28
Android-SDK-Linux/patcher/v4
Android-SDK-Linux/platform-tools/api
Android-SDK-Linux/platform-tools/lib64
Android-SDK-Linux/platform-tools/systrace
```

Note about the sources: in case anyone struggled with that ('cos the sdkmanager neither the intellij are abble to get them)  I downloaded manually from https://androidsdkmanager.azurewebsites.net/ .

So, I tried to Build Project:


```
29/03/19
15:31    Gradle sync started
15:31    Project setup started
15:31    Gradle sync finished in 2s 110ms (from cached state)
15:34    Executing tasks: [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:compileDebugAndroidTestSources, :app:compileDebugUnitTestSources, :app:compileDebugSources]
15:34    Gradle build finished in 6 s 343 ms
15:34    All files are up-to-date
```

But, I'm stuck with Run Tasks:




```
Caused by: No server to serve request. Check logs for details.
...
at com.android.builder.internal.aapt.v2.QueueableAapt2.lambda$compile$0(QueueableAapt2.java:136)
```

I verified that aapt is branded as Linux, obviously as I downloaded the Linux SDK. So I'm wondering if I can compiile it or replace it...


```
$  file /home/marcelbonnet/.local/Android-SDK-Linux/build-tools/28.0.3/aapt2
/home/marcelbonnet/.local/Android-SDK-Linux/build-tools/28.0.3/aapt2: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.24, not stripped
```

Or, perhaps, update glib in compat linux libs? It owns glib 2.12 .


```
[@machine 28.0.3]$  ./aapt2
./aapt2: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by ./aapt2)
./aapt2: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by /usr/home/marcelbonnet/.local/Android-SDK-Linux/build-tools/28.0.3/lib64/libc++.so)
```

So, is there a solution for it?


----------



## marcelbonnet (Mar 29, 2019)

One problem solved:

Upgrading from emulators/linux_base-c6 to emulators/linux_base-c7 solved the library problem. `aapt2` is working.

Now I'm focused on why the compiler cannot create a new directory under the project:

```
error: failed to create directory '/usr/home/marcelbonnet/devel/workspace-android/TwoActivities/app/build/generated/source/r/debug/localhost/marcelbonnet/twoactivities'.
```

In fact, the compile process delete the path if I create it manually. Then cannot create it again!
And, if I `sudo chflags -R schg localhost` , it claims its not enough to find the path there, it needs to delete the path before compiling ... and now it really can't.


----------



## marcelbonnet (Mar 29, 2019)

Episode III: a new hope.
I decided to use the command line (https://developer.android.com/studio/command-line/aapt2) .

Intellij seemed to compile the intermediate files. So I passed to the next phase: link them to an apk file.


```
$ alias aapt2=/home/marcelbonnet/.local/Android-SDK-Linux/build-tools/28.0.3/aapt2
$ aapt2 link -o /tmp/myapp.apk -I ~/.local/Android-SDK-Linux/platforms/android-28/android.jar `find . -type f -name '*.flat'` --manifest app/src/main/AndroidManifest.xml
```

It worked.
But, the smartphone said "the app was not installed".


----------



## marcelbonnet (Apr 1, 2019)

marcelbonnet said:


> But, the smartphone said "the app was not installed".



I miss configured the sdkversion , so I fix it to one supported by the target smartphone. app/build.gradle :


```
android {
    compileSdkVersion 25    //API to compile with
    buildToolsVersion "28.0.3"
    defaultConfig {
        applicationId "localhost.marcelbonnet.twoactivities"
        minSdkVersion 23
        targetSdkVersion 25 //tested up to this version
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    
    implementation 'com.android.support:appcompat-v7:25.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
```


Then, I woke up in a new hell:


```
"cannot resolve symbol R"
```

I entered:

```
import android.R;
```
 in my java files, and now I noticed the error:


```
"cannont resolve symbol R.layout.[someID] or R.id.[someID]"
```
. Which means, the designer do not map the layout xml files.

So, it seems aapt2 compile all intermediate files, except from those Java files, which rely on R to map IDs bettwen source and XML layout files.


```
$  gradle --stacktrace build
> Task :app:preBuild UP-TO-DATE                                                                                       
> Task :app:preDebugBuild UP-TO-DATE                                                                                           
> Task :app:compileDebugAidl UP-TO-DATE                                                                                                                     
> Task :app:compileDebugRenderscript UP-TO-DATE                                                                                                                                               
> Task :app:checkDebugManifest UP-TO-DATE                                                                                     
> Task :app:generateDebugBuildConfig UP-TO-DATE                                                                                                                                               
> Task :app:prepareLintJar UP-TO-DATE                                                                                                                                                         
> Task :app:mainApkListPersistenceDebug UP-TO-DATE                                                                                               
> Task :app:generateDebugResValues UP-TO-DATE                                                                                                                                                 
> Task :app:generateDebugResources UP-TO-DATE                                                                                   
> Task :app:mergeDebugResources UP-TO-DATE                                                                                             
> Task :app:createDebugCompatibleScreenManifests UP-TO-DATE                                                       
> Task :app:processDebugManifest UP-TO-DATE                                                                                                                                                   
> Task :app:splitsDiscoveryTaskDebug UP-TO-DATE                                                                                                                                               
error: failed to create directory '/usr/home/marcelbonnet/devel/workspace-android/TwoActivities/app/build/generated/source/r/debug/localhost/marcelbonnet/twoactivities'.
> Task :app:processDebugResources FAILED                                                                                                                                                       
                                                                                                                                                                                              
FAILURE: Build failed with an exception.                                                                                                                                                       
                                                                                                                                                                                              
* What went wrong:                                                                                                                                 
Execution failed for task ':app:processDebugResources'.                                                                                             
> Failed to process resources, see aapt output above for details.                                                                     
                                                                                                                        
* Try:                                                                                                                           
Run with --info or --debug option to get more log output. Run with --scan to get full insights.                         
                                                                                                                                                            
* Exception is:
(...)
    Caused by: org.gradle.tooling.BuildException: Failed to process resources, see aapt output above for details.
(...)
```


----------



## quanquan (Apr 5, 2019)

marcelbonnet said:


> One problem solved:
> 
> Upgrading from emulators/linux_base-c6 to emulators/linux_base-c7 solved the library problem. `aapt2` is working.
> 
> ...


aapt2 is working?


----------



## marcelbonnet (Apr 10, 2019)

quanquan said:


> aapt2 is working?


I quit. I can't waste more time trying. I'm developing using a bhyve virtual machine (window$ 7).


----------

